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

版权声明: https://blog.csdn.net/weixin_42061048/article/details/82912842

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

标签(空格分隔): Web集群实战


一、Nginx 基本安全优化

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

  • 对 Linux 客户端,可通过命令行查看 Nginx 版本号
[root@ylt001 conf]# curl -I 192.168.2.137
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0  # <-- 这里暴露了 Web 版本号及软件名称
Date: Sat, 29 Sep 2018 05:14:06 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://www.yangyangyang.org/
  • 对 Windows 客户端,通过浏览器访问 Web 服务时,若找不到页面,,默认报错的信息如下图:

404

  • 编辑 nginx.conf 文件,增加参数 server_tokens off,可实现隐藏 Nginx 版本号
[root@ylt001 conf]# cat nginx.conf
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections  1024;
}
http {
    
    ...
    
    server_tokens off;
    
    ...
    
}
# 参数作用:激活或禁止 Nginx 的版本信息显示在报错信息和 Server 的响应首部位置中
  • 配置完成后保存,重新加载配置文件,再次通过 curl 查看:
[root@ylt001 conf]# /application/nginx/sbin/nginx -s reload
[root@ylt001 conf]# curl -I 192.168.2.137
HTTP/1.1 301 Moved Permanently
Server: nginx  # <-- 版本号已消失
Date: Sat, 29 Sep 2018 05:24:09 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://www.yangyangyang.org/
  • 此时浏览器报错已没有显示版本号:

404_

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

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

  • 查看 Nginx 服务对应的默认用户
[root@ylt001 conf]#grep '#user' nginx.conf.default
#user  nobody;
  • 为 Nginx 服务建立新用户
[root@ylt001 conf]# useradd nginx -s /sbin/nologin -M
[root@ylt001 conf]# id nginx
uid=1001(nginx) gid=1001(nginx) groups=1001(nginx)
  • 配置 Nginx 服务,让其使用刚建立的 nginx 用户
[root@ylt001 conf]# sed -i 's#\#user  nginx#user  nginx nginx#g' nginx.conf.default
[root@ylt001 conf]# grep 'user  nginx nginx' nginx.conf.default
user  nginx nginx;
  • 检查更改用户的效果
[root@ylt001 conf]# ps -ef|grep nginx|grep -v grep
root       1702      1  0 08:42 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx
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 缓存实现性能优化

  • 根据文件扩展名进行判断
server {
        listen       80;
        server_name  yangyangyang.org;
        location / {
            root    html/www;
            index   index.html index.htm;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires     10y;
            root    html/www;
        }
        location ~ .*\.(js|css)?$
        {
        
            expires     30d'
        }
        access_log logs/access_www.log main gzip buffer=32k flush=5s;
}
  • 根据 URI 中的路径(目录)进行判断
location ~ ^/(images|javascripts|js|css|flash|media|static)/
{
    expires     360d;
}

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

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

2. 不记录不需要的日志

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

3. 访问日志的权限设置

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

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

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

  • 配置 Nginx,禁止解析指定目录下的指定程序
location ~ ^/images/.*\.(php|php5|sh|pl|py)$
{
    deny all;
}
location ~ ^/static/.*\.(php|php5|sh|pl|py)$
{
    deny all;
}
location ~* ^/data/ (attachment|avatar)/.*\.(php|php5)$
{
    deny all;
}
  • 对上述目录的限制必须写在 Nginx 处理 PHP 服务配置的前面
location ~ .*\. (php|php5)?$
{
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fcgi.conf;
}

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

location ~ ^/(static)/
{
    deny all;
}
location ~ ^/static/
{
    deny all;
}
location ~ ^/(static|js)/
{
    deny all;
}

3. 限制网站来源 IP 访问

  • 禁止某目录让外界访问,但允许某 IP 访问该目录,且支持 PHP 解析:
location ~ ^/ylt/
{
    allow 202.116.83.77
    deny all;
}
location ~ .*\. (php|php5)?$
{
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
  • 限定指定 IP 或 IP 段访问
location /
{
    deny 192.168.1.1;
    allow 192.168.1.0/24;
    allow 10.1.1.0/16;
    deny all;
}

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

五、Nginx 图片及目录防盗链

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

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

  • 如果是单机的 LNMP 环境,站点目录和文件属性设置如下:
    • 先把所有的目录权限设置为 755,所有的文件权限设置为 644;
    • 所用的目录,以及文件用户和组都是 root;
    • 然后把用户上传资源的目录权限设置为 755,将用户和组设置为 Nginx 服务的用户;
    • 最后针对上传资源的目录做资源访问限制。

八、Nginx 防爬虫优化

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

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

十一、Nginx 程序架构优化

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

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

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

猜你喜欢

转载自blog.csdn.net/weixin_42061048/article/details/82912842
今日推荐