1. Nginx配置清单
Nginx 是一个高性能的 HTTP 和反向代理 web 服务器,同时也提供了 IMAP/POP3/SMTP 服务,其因丰富的功能集、稳定性、示例配置文件和低系统资源的消耗受到了开发者的欢迎。
1.1. 侦听端口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| server { # Standard HTTP Protocol listen 80;
# Standard HTTPS Protocol listen 443 ssl;
# For http2 listen 443 ssl http2;
# Listen on 80 using IPv6 listen [::]:80;
# Listen only on using IPv6 listen [::]:80 ipv6only=on; }
|
1.2. 访问日志
1 2 3 4 5 6 7
| server { # Relative or full path to log file access_log /path/to/file.log;
# Turn 'on' or 'off' access_log on; }
|
1.3. 域名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| server { # Listen to yourdomain.com server_name yourdomain.com;
# Listen to multiple domains server_name yourdomain.com www.yourdomain.com;
# Listen to all domains server_name *.yourdomain.com;
# Listen to all top-level domains server_name yourdomain.*;
# Listen to unspecified Hostnames (Listens to IP address itself) server_name "";
}
|
1.4. 静态资产
1 2 3 4 5 6 7 8
| server { listen 80; server_name yourdomain.com;
location / { root /path/to/website; } }
|
1.5. 重定向
1 2 3 4 5 6 7 8 9 10 11 12 13
| server { listen 80; server_name www.yourdomain.com; return 301 http://yourdomain.com$request_uri; } server { listen 80; server_name www.yourdomain.com;
location /redirect-url { return 301 http://otherdomain.com; } }
|
1.6. 反向代理
1 2 3 4 5 6 7 8 9 10
| server { listen 80; server_name yourdomain.com;
location / { proxy_pass http://0.0.0.0:3000; # where 0.0.0.0:3000 is your application server (Ex: node.js) bound on 0.0.0.0 listening on port 3000 }
}
|
1.7. 负载均衡
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| upstream node_js { server 0.0.0.0:3000; server 0.0.0.0:4000; server 123.131.121.122; }
server { listen 80; server_name yourdomain.com;
location / { proxy_pass http://node_js; } }
|
1.8. SSL 协议
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| server { listen 443 ssl; server_name yourdomain.com;
ssl on;
ssl_certificate /path/to/cert.pem; ssl_certificate_key /path/to/privatekey.pem;
ssl_stapling on; ssl_stapling_verify on; ssl_trusted_certificate /path/to/fullchain.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_session_timeout 1h; ssl_session_cache shared:SSL:50m; add_header Strict-Transport-Security max-age=15768000; }
# Permanent Redirect for HTTP to HTTPS server { listen 80; server_name yourdomain.com; return 301 https://$host$request_uri; }
|
1.9. 禁止IP访问
禁止ip访问是为了避免其他人把未备案的域名解析到自己的服务器IP,而导致服务器被断网,我们可以通过禁止使用ip访问的方法,防止此类事情的发生。
1 2 3 4 5 6 7
| server{ listen 80; server_name www.yourdomain.com; if ($host != 'www.yourdomain.com'){ return 403; } }
|
1.10. gzip压缩
在我们进行 gzip 打包压缩之前,最好将一些静态文件先进行压缩为 min 文件,请求的时候合并为同一文件。再通过 gzip 压缩后,你会发现网站加载又加速了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #开启gzip,减少我们发送的数据量 gzip on;
#允许压缩的最小字节数 gzip_min_length 1k;
#4个单位为16k的内存作为压缩结果流缓存 gzip_buffers 4 16k;
#设置识别HTTP协议版本,默认是1.1 gzip_http_version 1.1;
#gzip压缩比,可在1~9中设置,1压缩比最小,速度最快,9压缩比最大,速度最慢,消耗CPU gzip_comp_level 4;
#压缩的类型 gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
#给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩 gzip_vary on;
#禁用IE6以下的gzip压缩,IE6的某些版本对gzip的压缩支持很不好 gzip_disable "MSIE [1-6].";
|
1.11. 连接超时
长时间占着连接资源不释放,最终会导致请求的堆积,Nginx 处理请求效率大大降低。所以我们对连接的控制都要注意设置超时时间,通过超时机制自动回收资源、避免资源浪费。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #客户端、服务端设置 server_names_hash_bucket_size 128; server_names_hash_max_size 512;
# 长连接超时配置 keepalive_timeout 65; client_header_timeout 15s; client_body_timeout 15s; send_timeout 60s;
#代理设置 #与后端服务器建立连接的超时时间。注意这个一般不能大于75秒 proxy_connect_timeout 30s; proxy_send_timeout 120s;
#从后端服务器读取响应的超时 proxy_read_timeout 120s;
|
1.12. 隐藏版本信息
暴露出来的版本号就容易变成攻击者可利用的信息。所以,从安全的角度来说,隐藏版本号会相对安全些!在http模块中增加:
1 2
| # 隐藏版本信息 server_tokens off;
|
1.13. 防盗链
防止别人直接从你网站引用图片等链接,消耗了你的资源和网络流量
在server模块中配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| location ~*^.+\.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {
valid_referers noneblocked www.kubelet.cn;
if($invalid_referer) { return 404; break; }
|
参数可以使如下形式:
- none :意思是不存在的Referer头(表示空的,也就是直接访问,比如直接在浏览器打开一个图片)。
- blocked :意为根据防火墙伪装Referer头,如:“Referer:XXXXXXX”。
- server_names :为一个或多个服务器的列表,0.5.33版本以后可以在名称中使用“*”通配符。
1.14. 进程数的优化
一般一个进程足够了,你可以把连接数设得很大。(worker_processes: 1,worker_connections: 10,000)
如果有SSL、gzip这些比较消耗CPU的工作,而且是多核CPU的话,可以设为和CPU的数量一样。(worker_processes: CPU核心数)
或者要处理很多很多的小文件,而且文件总大小比内存大很多的时候,也可以把进程数增加,以充分利用IO带宽(主要似乎是IO操作有block)
worker_processes,工作进程数
- 1.默认:worker_processes: 1
- 2.调大:worker_processes: CPU核心数,(双核4线程,可以设置为4)
- 3.auto:worker_processes: auto
也可以将其设置为auto。 这样nginx会自动根据核心数为生成对应数量的worker进程。
1.15. 连接数的优化
linux 默认值 open files为1024。查看当前系统值:
1 2
| [root@linux ~]# ulimit -n 1024
|
说明server只允许同时打开1024个文件。
使用ulimit -a 可以查看当前系统的所有限制值,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| [root@linux ~]# ulimit -a core file size (blocks, -c) unlimited data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 14982 max locked memory (kbytes, -l) 64 max memory size (kbytes, -m) unlimited open files (-n) 1024 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 14982 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
|
使用ulimit -n 可以查看当前的最大打开文件数。
新装的linux 默认只有1024 ,当作负载较大的服务器时,很容易遇到error: too many open files。因此,需要将其改大,在/etc/security/limits.conf最后增加:
1 2 3 4 5 6 7 8 9 10 11 12 13
| # 永久生效 [root@linux ~]# vim /etc/security/limits.conf
* soft nofile 65535 * hard nofile 65535 * soft noproc 65535 * hard noproc 65535 * soft memlock unlimited * hard memlock unlimited
# 临时生效 [root@linux ~]# ulimit -Sn 2048 #修改softlimit [root@linux ~]# ulimit -Hn 2048 #修改hardlimit
|
*代表针对所有用户,nproc是代表最大进程数,nofile 是代表最大文件打开数
修改完成后重启即可
nginx中配置连接数:
1 2 3 4 5
| [root@linux ~]# vim /etc/nginx/nginx.conf
events { worker_connections 65535; }
|