[关闭]
@yanglt7 2018-10-21T15:56:25.000000Z 字数 4111 阅读 1091

【Web 集群实战】15_Nginx Web 服务优化

Web集群实战


一、Nginx 基本安全优化

1. 调整参数隐藏 Nginx 软件版本号信息

  1. [root@ylt001 conf]# curl -I 192.168.2.137
  2. HTTP/1.1 301 Moved Permanently
  3. Server: nginx/1.14.0 # <-- 这里暴露了 Web 版本号及软件名称
  4. Date: Sat, 29 Sep 2018 05:14:06 GMT
  5. Content-Type: text/html
  6. Content-Length: 185
  7. Connection: keep-alive
  8. Location: http://www.yangyangyang.org/

404

  1. [root@ylt001 conf]# cat nginx.conf
  2. worker_processes 1;
  3. error_log logs/error.log;
  4. events {
  5. worker_connections 1024;
  6. }
  7. http {
  8. ...
  9. server_tokens off;
  10. ...
  11. }
  12. # 参数作用:激活或禁止 Nginx 的版本信息显示在报错信息和 Server 的响应首部位置中
  1. [root@ylt001 conf]# /application/nginx/sbin/nginx -s reload
  2. [root@ylt001 conf]# curl -I 192.168.2.137
  3. HTTP/1.1 301 Moved Permanently
  4. Server: nginx # <-- 版本号已消失
  5. Date: Sat, 29 Sep 2018 05:24:09 GMT
  6. Content-Type: text/html
  7. Content-Length: 178
  8. Connection: keep-alive
  9. Location: http://www.yangyangyang.org/

404_

2. 更改源码隐藏 Nginx 软件名及版本号

3. 更改 Nginx 服务的默认用户

  1. [root@ylt001 conf]#grep '#user' nginx.conf.default
  2. #user nobody;
  1. [root@ylt001 conf]# useradd nginx -s /sbin/nologin -M
  2. [root@ylt001 conf]# id nginx
  3. uid=1001(nginx) gid=1001(nginx) groups=1001(nginx)
  1. [root@ylt001 conf]# sed -i 's#\#user nginx#user nginx nginx#g' nginx.conf.default
  2. [root@ylt001 conf]# grep 'user nginx nginx' nginx.conf.default
  3. user nginx nginx;
  1. [root@ylt001 conf]# ps -ef|grep nginx|grep -v grep
  2. root 1702 1 0 08:42 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx
  3. nginx 67706 1702 0 13:24 ? 00:00:00 nginx: worker process

二、根据参数优化 Nginx 服务性能

1. 优化 Nginx 服务的 worker 进程个数

2. 优化绑定不同的 Nginx 进程到不同的 CPU 上

3. Nginx 事件处理模型优化

4. 调整 Nginx 单个进程允许的客户端最大连接数

5. 配置 Nginx worker 进程最大打开文件数

6. 优化服务器域名的散列表大小

7. 开启高效文件传输模式

8. 优化 Nginx 连接参数,调整连接超时时间

9. 上传文件大小的限制(动态应用)

10. FastCGI 相关参数调优(配合 PHP 引擎动态服务)

11. 配置 Nginx gzip 压缩实现性能优化

12. 配置 Nginx expires 缓存实现性能优化

  1. server {
  2. listen 80;
  3. server_name yangyangyang.org;
  4. location / {
  5. root html/www;
  6. index index.html index.htm;
  7. }
  8. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  9. {
  10. expires 10y;
  11. root html/www;
  12. }
  13. location ~ .*\.(js|css)?$
  14. {
  15. expires 30d'
  16. }
  17. access_log logs/access_www.log main gzip buffer=32k flush=5s;
  18. }
  1. location ~ ^/(images|javascripts|js|css|flash|media|static)/
  2. {
  3. expires 360d;
  4. }

三、Nginx 日志相关优化与安全

1. 编写脚本实现 Nginx access 日志轮询

2. 不记录不需要的日志

  1. location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$ {
  2. access_log off;
  3. }

3. 访问日志的权限设置

  1. chown -R root.root /application/nginx/logs/
  2. chmod -R 700 /application/nginx/logs/

四、Nginx 站点目录及文件 URL 访问控制

1. 根据扩展名限制程序和文件访问

  1. location ~ ^/images/.*\.(php|php5|sh|pl|py)$
  2. {
  3. deny all;
  4. }
  5. location ~ ^/static/.*\.(php|php5|sh|pl|py)$
  6. {
  7. deny all;
  8. }
  9. location ~* ^/data/ (attachment|avatar)/.*\.(php|php5)$
  10. {
  11. deny all;
  12. }
  1. location ~ .*\. (php|php5)?$
  2. {
  3. fastcgi_pass 127.0.0.1:9000;
  4. fastcgi_index index.php;
  5. include fcgi.conf;
  6. }

2. 禁止访问指定目录下的所有文件和目录

  1. location ~ ^/(static)/
  2. {
  3. deny all;
  4. }
  5. location ~ ^/static/
  6. {
  7. deny all;
  8. }
  9. location ~ ^/(static|js)/
  10. {
  11. deny all;
  12. }

3. 限制网站来源 IP 访问

  1. location ~ ^/ylt/
  2. {
  3. allow 202.116.83.77
  4. deny all;
  5. }
  6. location ~ .*\. (php|php5)?$
  7. {
  8. fastcgi_pass 127.0.0.1:9000;
  9. fastcgi_index index.php;
  10. include fastcgi_params;
  11. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  12. }
  1. location /
  2. {
  3. deny 192.168.1.1;
  4. allow 192.168.1.0/24;
  5. allow 10.1.1.0/16;
  6. deny all;
  7. }

4. 配置 Nginx,禁止非法域名解析访问企业网站

五、Nginx 图片及目录防盗链

六、Nginx 错误页面的优雅显示

七、Nginx 站点目录文件及目录权限优化

八、Nginx 防爬虫优化

九、利用 Nginx 限制 HTTP 的请求方法

十、使用 CDN 做网站内容加速

十一、Nginx 程序架构优化

十二、使用普通用户启动 Nginx(监牢模式)

十三、控制 Nginx 并发连接数量

十四、控制客户端请求 Nginx 的速率

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注