nginx 配置
安全
https
如果使用Let' Encrypt 的免费 https 证书, 推荐使用 certbot,使用简单易用。
yum install certbot安装 certbotcertboy certonly使用 certbot 生成证书,后续选项建议选择 1 如果当前 80 端口被占用,可以先暂时关闭- 获取生成整证书和数字证书的路径,如
/etc/letsencrypt/live后续配置ssl_certificate与ssl_certificate_key - Let's Encrypt 签发的证书只有 90 天有效期,官方将证书有效期定为 90 天一方面是为了更安全,更重要的是鼓励用户采用自动化部署方案。
- 下面是自动化的脚本
#!/bin/bash
# /root/tasks/auto_update_ssl.sh
# 关闭nginx
nginx -s quit
# 证书更新完毕后,将关闭的nginx启动
certbot renew --renew-hook "nginx" >/root/log/auto_update_ssl.log 2>&1 &
- 结合上面的脚本配置
crontab -e, 在最后一行加入0 0 1 * * /root/tasks/auto_update_ssl.sh, 这样以后证书每个月都会自动更新,一劳永逸。
配置
以下是该站点的完整配置
# 设置运行 nginx 的角色, 如果不为 root, 可能会出现 403
user root;
# worker 节点数量进程数量 http://nginx.org/en/docs/ngx_core_module.html#worker_processes
worker_processes 2;
#进程可打开最大文件数
worker_rlimit_nofile 65535;
# 日志文件地址, 日志级别
error_log logs/error.log notice;
events {
accept_mutex off;
use epoll;
worker_connections 8192;
}
http {
include mime.types;
default_type application/octet-stream;
log_format combinedio '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" $request_length $request_time $upstream_response_time';
access_log logs/access.log combinedio;
sendfile on;
gzip on;
tcp_nopush on;
tcp_nodelay on;
# 如果使用了 http2 这里不能设置为 0 或者 1
keepalive_timeout 72;
client_body_timeout 10;
client_header_timeout 10;
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
output_buffers 2 32k;
client_max_body_size 64m;
client_body_buffer_size 256k;
server_tokens off;
include http.d/*.conf;
server {
# 启用http2, ssl
listen 443 http2 ssl; //
add_header Strict-Transport-Security "max-age=31536000";
# 字符设置
charset utf-8;
# 证书
ssl_certificate /etc/letsencrypt/live/mkooo.cn/fullchain.pem;
#私钥
ssl_certificate_key /etc/letsencrypt/live/mkooo.cn/privkey.pem;
# ssl_prefer_server_ciphers on;
# SSL 使用协议版本
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
# session 缓存方式, shared,所有 worker 共享
ssl_session_cache shared:SSL:50m;
# session超时时间
ssl_session_timeout 5d;
# 用于开启浏览器 ticket 缓存
ssl_session_tickets on;
#Enables or disables stapling of OCSP responses by the server.
ssl_stapling on;
ssl_stapling_verify on;
http2_max_concurrent_streams 10000;
resolver 182.254.116.116 valid=300s;
#resolver_timeout 10s;
server_name mkooo.cn;
location / {
root /root/website;
index index.html index.htm;
# 如果不存在页面,则返回 index.html
try_files $uri $uri/ /index.html;
}
#location /api {
# proxy_pass 127.0.0.1:7001;
#}
location /server-status {
# stub_status on;
allow 127.0.0.1;
deny all;
}
location /status {
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
allow 127.0.0.1;
deny all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name mkooo.cn;
rewrite ^(.*)$ https://${server_name}$1 permanent;
}
}
错误处理
net::ERR_HTTP2_SERVER_REFUSED_STREAM
如果在浏览器访问的时候发现有些资源加载失败,控制台报错,可能是由于最大连接数超过了限制导致的。 可以参考下面的解决方案
- 如果开启了 HTTP2, 那么 keepalive_timeout 不能设置为 0 和 1
- 调整http2-max-concurrent-streams至更大值(默认128)。具体操作,请参见http2-max-concurrent-streams。
ngx_http_upstream_modulecan not be used when making a PIE object; recompile with -fPIC
collect2: error id returned 1
新增 nginx 模块编译 nginx 的时候如果碰到, ngx_***_module can not be used when making a PIE object; recompile with -fPIC
- 在
make阶段新增参数make CC='gcc -fPIC'进行编译,然后make install
附录
linux 安装编译 nginx
- 从官网下载 nginx 安装包
tar -zxvf ****/*.tar.gzcd [path]
# 执行以下指令,编译 `nginx` 的模块,可以根据需要的功能自行选择
# 其中`--prefix` 是 `nginx` 的安装路径, 你想安装 nginx 到哪个为止
./configure --prefix=/usr/local/****/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_perl_module --with-http_auth_request_module --with-pcre --with-pcre-jit --with-http_v2_module
make install- 安装好了之后
cd进入目录, 可以在.bashrc里面把nginx加入PATHexport PATH=/usr/local/**/nginx/sbin:$PATH, 完成之后记得source ~/.bashrc - 这个时候就可以尝试用
nginx启动服务了
nginx 常用指令
# 查看修改的配置文件是否正确, 每次修改完配置之后如果不自信,可以使用这个指令来验证是否合法
nginx -t
# 根据需要选择
nginx -s [stop, quit, reopen, reload]
DNS
{
"ServerName": "阿里DNS",
"DNS1": "223.5.5.5",
"DNS2": "223.6.6.6"
},
{
"ServerName": "腾讯DNS",
"DNS1": "119.29.29.29",
"DNS2": "182.254.116.116"
},
{
"ServerName": "百度DNS",
"DNS1": "180.76.76.76",
"DNS2": ""
},
{
"ServerName": "DNS派",
"DNS1": "101.226.4.6",
"DNS2": "218.30.118.6"
},
{
"ServerName": "114DNS",
"DNS1": "114.114.114.114",
"DNS2": "114.114.115.115"
},
{
"ServerName": "ONE DNS纯净版",
"DNS1": "117.50.10.10",
"DNS2": "52.80.52.52"
},
{
"ServerName": "ONE DNS拦截版",
"DNS1": "117.50.11.11",
"DNS2": "52.80.66.66"
},
{
"ServerName": "BAI DNS",
"DNS1": "223.113.97.99",
"DNS2": ""
},
{
"ServerName": "PdoMo-DNS",
"DNS1": "101.132.183.99",
"DNS2": "193.112.15.186"
},
{
"ServerName": "TUNA DNS",
"DNS1": "101.6.6.6",
"DNS2": ""
},
{
"ServerName": "中国互联网中心",
"DNS1": "210.2.4.8",
"DNS2": ""
},
{
"ServerName": "谷歌DNS",
"DNS1": "8.8.8.8",
"DNS2": "8.8.4.4"
},
{
"ServerName": "IBM Quad9",
"DNS1": "9.9.9.9",
"DNS2": ""
},
{
"ServerName": "Open DNS",
"DNS1": "208.67.222.222",
"DNS2": "208.67.220.220"
}