nginx高级应用——调优

    xiaoxiao2025-06-05  25

    1. 隐藏 Nginx 版本号

    为什么要隐藏 Nginx 版本号:一般来说,软件的漏洞都与版本有关,隐藏版本号是为了防止恶意用户利用软件漏洞进行攻击

    [root@localhost ~]# cat /usr/local/nginx/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server_tokens off; # 隐藏版本号 server { listen 80; server_name www.abc.com; location / { root html/www; index index.html index.htm; } } } ... [root@localhost ~]# /usr/local/nginx/sbin/nginx -t [root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload [root@localhost ~]# curl -I 127.0.0.1 # 查看是否隐藏版本号 HTTP/1.1 404 Not Found Server: nginx Date: Thu, 25 May 2017 05:23:03 GMT Content-Type: text/html Content-Length: 162 Connection: keep-alive

    2.隐藏 Nginx 版本号和软件名

    为什么要隐藏 Nginx 版本号和软件名:一般来说,软件的漏洞都与版本有关,隐藏版本号是为了防止恶意用户利用软件漏洞进行攻击,而软件名可以进行修改,否则黑客知道是 Nginx 服务器更容易进行攻击,需要注意的是,隐藏 Nginx 软件名需要重新编译安装 Nginx ,如果没有该方面需求尽量不要做

    1) 修改:/usr/local/src/nginx-1.6.3/src/core/nginx.h

    #define NGINX_VERSION "8.8.8.8" # 修改为想要显示的版本号 #define NGINX_VER "Google/" NGINX_VERSION # 修改为想要显示的软件名 #define NGINX_VAR "Google" # 修改为想要显示的软件名

    2) 修改:/usr/local/src/nginx1.6.3/src/http/ngx_http_header_filter_module.c

    static char ngx_http_server_string[] = "Server: Google" CRLF; # 修改为想要显示的软件名 static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;

    3)修改:/usr/local/src/nginx1.6.3/src/http/ngx_http_special_response.c

    static u_char ngx_http_error_full_tail[] = "<hr><center>" NGINX_VER "(www.google.com)</center>" CRLF # 此行定义对外展示的内容 "</body>" CRLF "</html>" CRLF ; static u_char ngx_http_error_tail[] = "<hr><center>Google</center>" CRLF # 此行定义对外展示的软件名 "</body>" CRLF "</html>" CRLF ;
    4) 重新编译 Nginx
    cd /usr/local/src/nginx-1.6.3 ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module make && make install /usr/local/nginx/sbin/nginx

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

    为什么要更改 Nginx 服务的默认用户:就像更改 ssh 的默认 22 端口一样,增加安全性,Nginx 服务的默认用户是 nobody ,我们更改为 nginx

    1) 添加 nginx 用户

    useradd -s /sbin/nologin -M nginx

    2) 更改 Nginx 配置文件

    [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf worker_processes 1; user nginx nginx; # 指定Nginx服务的用户和用户组 events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server_tokens off; server { listen 80; server_name www.abc.com; location / { root html/www; index index.html index.htm; } } }

    3) 重新加载 Nginx

    [root@localhost ~]# /usr/local/nginx/sbin/nginx -t [root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

    4) 验证是否生效

    [root@localhost ~]# ps aux | grep nginx root 8901 0.0 0.1 45036 1784 ? Ss 13:54 0:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 8909 0.0 0.1 45460 1828 ? S 13:59 0:00 nginx: worker process # Nginx进程的所属用户为nginx

    4.优化 Nginx 文件权限

    为了保证网站不受木马入侵,所有文件的用户和组都应该为 root ,所有目录的权限是 755 ,所有文件的权限是 644

    [root@localhost ~]# chown -R root.root /usr/local/nginx/.... # 根据实际来调整 [root@localhost ~]# chmod 755 /usr/local/nginx/.... [root@localhost ~]# chmod 644 /usr/local/nginx/....

    5.控制 Nginx 并发连接数

    1. 限制单个 IP 的并发连接数

    [root@localhost ~]# cat /usr/local/nginx/conf/nginx.conf .... http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; limit_conn_zone $binary_remote_addr zone=addr:10m; # 用于设置共享内存区域,addr 是共享内存区域的名称,10m 表示共享内存区域的大小 server { listen 80; server_name www.abc.com; location / { root html/www; index index.html index.htm; limit_conn addr 1; # 限制单个IP的并发连接数为1 } } }

    2. 限制虚拟主机总连接数

    .... http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; limit_conn_zone $server_name zone=perserver:10m; server { listen 80; server_name www.abc.com; location / { root html/www; index index.html index.htm; limit_conn perserver 2; # 设置虚拟主机连接数为2 } } }
    最新回复(0)