nginx的一些优化

nginx优化

  • 一、自定义报错页面?
  • 二、优化Nginx并发量
  • 三、优化Nginx服务的安全配置
    • 1, 修改版本信息,并隐藏具体的版本号
    • 2, 拒绝非法的请求
  • 四、编写日志切割脚本
  • 五、Nginx的一些模块


一、自定义报错页面?

优化前,客户端使用浏览器访问不存在的页面,会提示404文件未找到

nginx的一些优化_第1张图片

操作步骤:
1,vim打开配置文件,修改error_page行

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf

 47         error_page  404              /404.jpg;

2,网上下载一张图片,命名为404.jpg放到下面的位置

[root@nginx html]# pwd
/usr/local/nginx/html
[root@nginx html]# ls
404.jpg  50x.html  index.html

优化后,客户端使用浏览器访问不存在的页面,会提示自己定义的404页面
nginx的一些优化_第2张图片

二、优化Nginx并发量

优化前使用ab高并发测试

[root@nginx html]# ab -n 2000 -c 2000 http://192.168.4.100/
Benchmarking 192.168.4.100 (be patient)
socket: Too many open files (24)

修改Nginx配置文件,增加并发量

[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf

… …
worker_processes 2; //与CPU核心数量一致
events {
worker_connections 65535; //每个worker最大并发连接数
}
… …

优化Linux内核参数(最大文件数量)

除了nginx本身对并发量有限制,linux系统本身对文件的访问也有限制,默认情况下linux系统仅允许一个文件同时被打开1024次。
临时修改限制,修改为支持10万

[root@nginx html]# ulimit -Hn 100000

[root@nginx html]#  ulimit -Sn 100000

测试

[root@nginx html]# ab -n 2000 -c 2000 http://192.168.4.100/


Percentage of the requests served within a certain time (ms)
  50%     10
  66%     19
  75%     39
  80%     40
  90%     55
  95%     62
  98%     62
  99%     62
 100%    239 (longest request)

三、优化Nginx服务的安全配置

1, 修改版本信息,并隐藏具体的版本号

默认Nginx会显示版本信息以及具体的版本号,这些信息给攻击者带来了便利性,便于他们找到具体版本的漏洞。
优化前:

[root@nginx html]# curl -I http://192.168.4.100
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Mon, 28 Dec 2020 16:47:23 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 28 Dec 2020 15:42:55 GMT
Connection: keep-alive
ETag: "5fe9fcff-264"
Accept-Ranges: bytes

优化:

[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
… …
http{
     
     server_tokens off; #在http下面手动添加这么一行
     … …
}

优化后

[root@nginx html]# curl -I http://192.168.4.100
HTTP/1.1 200 OK
Server: nginx

2, 拒绝非法的请求

网站使用的是HTTP协议,该协议中定义了很多方法,可以让用户连接服务器,获得需要的资源。但实际应用中一般仅需要get和post。

优化前:GET和HEAD都可以

[root@nginx ~]# curl -i -X GET  http://192.168.4.100
HTTP/1.1 200 OK
Server: nginx

[root@nginx ~]#  curl -i -X HEAD http://192.168.4.100
HTTP/1.1 200 OK
Server: nginx

优化:

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
在server{
     }里面加一个if语句
  if ($request_method !~ ^(GET|POST)$ ) {
     
     return 404;
       }  
[root@nginx ~]# /usr/local/nginx/sbin/nginx -s reload

优化后:HEAD请求出错

[root@nginx ~]# curl -i -X GET http://192.168.4.100
HTTP/1.1 200 OK
Server: nginx

[root@nginx ~]# curl -i -X HEAD http://192.168.4.100
HTTP/1.1 404 Not Found
Server: nginx


四、编写日志切割脚本

1.编写脚本

[root@nginx ~]# vim /usr/local/nginx/logbak.sh

#!/bin/bash
date=`date +%Y%m%d`
logpath=/usr/local/nginx/logs
mv $logpath/access.log $logpath/access-$date.log
mv $logpath/error.log $logpath/error-$date.log
kill -USR1 $(cat $logpath/nginx.pid)

2.创建计划任务
crontab -e
03 03 * * 5 /usr/local/nginx/logbak.sh
计划任务可以随便写

五、Nginx的一些模块

ngx_http_core_module(核心模块,包含http、server_name、root等配置参数)

ngx_http_access_module(访问控制模块,包含allow和deny配置参数)

ngx_http_auth_basic_module(用户认证模块,包含auth_basic等配置参数)

ngx_http_charset_module(字符集模块,包含charset utf8等配置参数)

ngx_http_fastcgi_module(fastcgi模块,包含fastcgi_pass等配置参数)

ngx_http_gzip_module(压缩模块,包含gzip、gzip_type等配置参数)

ngx_http_limit_conn_module(限制并发量模块,包含limit_conn等参数)

ngx_http_log_module(日志模块,包含access_log等配置参数)

ngx_http_proxy_module(代理模块,包含proxy_pass等配置参数)

ngx_http_rewrite_module(地址重写模块,包含rewrite、break、last等配置参数)

ngx_http_ssl_module(加密模块,包含ssl_certificate、ssl_certificate_key等参数)

ngx_http_stub_status_module(状态模块,包含stub_status配置参数)

ngx_http_upstream_module(调度器模块,包含upstream、hash、ip_hash等配置参数)

ngx_stream_core_module(4层代理模块)

你可能感兴趣的:(linux,linux,nginx)