运维之道 | Nginx调优

Nginx调优


一、隐藏 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

二、隐藏 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/nginx-1.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/nginx-1.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

[root@bogon ~]# cd /usr/local/src/nginx-1.8.0
[root@bogon nginx-1.8.0]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@bogon nginx-1.8.0]# make && make install
[root@bogon nginx-1.8.0]# /usr/local/nginx/sbin/nginx

三、更改 Nginx 服务的默认用户

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

[root@localhost ~]# 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

四、优化 Nginx worker 进程数

Nginx 有 Master 和 worker 两种进程,Master 进程用于管理 worker 进程,worker 进程用于 Nginx 服务

worker 进程数应该设置为等于 CPU 的核数,高流量并发场合也可以考虑将进程数提高至 CPU 核数 * 2

[root@localhost ~]# grep -c processor /proc/cpuinfo         # 查看CPU核数
2
 
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf    # 设置worker进程数
worker_processes  2;
user nginx nginx;
......
 
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t          # 重新加载Nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
 
[root@localhost ~]# ps -ef | grep nginx | grep -v grep      # 验证是否为设置的进程数
root       8901      1  0 13:54 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      8937   8901  0 14:14 ?        00:00:00 nginx: worker process     
nginx      8938   8901  0 14:14 ?        00:00:00 nginx: worker process  

五、绑定 Nginx 进程到不同的 CPU 上

为什么要绑定 Nginx 进程到不同的 CPU 上 :默认情况下,Nginx 的多个进程有可能跑在某一个 CPU 或 CPU 的某一核上,导致 Nginx 进程使用硬件的资源不均,因此绑定 Nginx 进程到不同的 CPU 上是为了充分利用硬件的多 CPU 多核资源的目的。

[root@localhost ~]# grep 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值