Nginx性能优化

一、Nginx性能优化

(1)Nginx常见问题

1、Nginx多个相同的Server_name优先级

  • 实验环境:
系统ip主机名ngixn版本
Centos7.4192.168.100.107-10nginx-1.12.0
  • 实验步骤:
(1)使用源码包方式安装nginx(略)
(2)创建三个目录
[root@7-10 ~]# mkdir -p /aaa/bbb{1..3}  #创建aaa目录下以bbb开头后面分别是1到3的目录
[root@7-10 ~]# for i in {1..3};do echo "abc $i" > /aaa/bbb"$i"/index.html;done #使用for循环,给三个目录下写网页
(3)修改主配置文件
[root@7-10 ~]# vim /usr/local/nginx/conf/nginx.conf #以#开头的注释行可以删除
。。。。。。  #在http{}里修改,创建三个虚拟主机,分别对应刚才创建的三个不同的目录
 34     server {
 35         listen 80;
 36         server_name 192.168.100.10;
 37         location / {
 38              root /aaa/bbb1;
 39              index index.html;
 40            }
 41        }
 42     server {
 43         listen 80;
 44         server_name 192.168.100.10;
 45         location / {
 46              root /aaa/bbb2;
 47              index index.html;
 48            }
 49        }
 50     server {
 51         listen 80;
 52         server_name 192.168.100.10;
 53         location / {
 54              root /aaa/bbb3;
 55              index index.html;
 56            }
 57        }
 58 
。。。。。。
#保存退出
[root@7-10 ~]# nginx -t  #检查nginx语句是否正确
nginx: [warn] conflicting server name "192.168.100.10" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "192.168.100.10" on 0.0.0.0:80, ignored
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@7-10 ~]# systemctl restart nginx  #重启服务
[root@7-10 ~]# curl 192.168.100.10  #访问本地 
abc 1      

发现访问的是bbb1目录的,之后再将第一个location加上#

37        # location / {
 38           #   root /aaa/bbb1;
 39             # index index.html;
 40           # }

就会发现访问到bbb2,再给第二个加上#就会发现访问到bbb3,说明当servername相同时,访问的优先级是从上到下的

2、location优先级

  • 实验环境:
系统ip主机名ngixn版本
Centos7.4192.168.100.107-10nginx-1.12.0
  • location匹配优先级

location指定资源,即用户访问这个资源的时候location中的配置才会生效,location / 默认匹配则全部生效

完整匹配优先级
=进行普通字符的精确匹配,即完全匹配
^~表示普通字符匹配,使用前缀匹配
正则匹配匹配后会继续查找更加精确的location
~区分大小写匹配
~*不区分大小写匹配
/默认匹配
  • 实验步骤:
    继续上面的操作 (1)修改主配置文件
[root@7-10 ~]# vim /usr/local/nginx/conf/nginx.conf    #还是修改server虚拟主机 
                   
 34     server {
 35         listen 80;
 36         server_name  192.168.100.10;
 37         root /aaa;
 38         index index.html;
 39         location = /bbb1 {
 40         rewrite ^(.*)$ /bbb1/index.html break;
 41            }
 42         location ~ /bbb* {
 43         rewrite ^(.*)$ /bbb2/index.html break;
 44            }
 45         location ^~ /bbb {
 46         rewrite ^(.*)$ /bbb3/index.html break;
 47            }
 48        }
 [root@7-10 aaa]# nginx -t  #检查nginx语句是否配置正确
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@7-10 aaa]# systemctl restart nginx  #重启服务
[root@7-10 aaa]# curl http://192.168.100.202/bbb1  #访问本地,指定访问目录
abc 1

(2)再次修改配置文件,注释掉精确匹配那一行

[root@7-10 aaa]# vim /usr/local/nginx/conf/nginx.conf
 34     server {
 35         listen 80;
 36         server_name  192.168.100.10;
 37         root /aaa;
 38         index index.html;
 39        # location = /bbb1 {
 40         #rewrite ^(.*)$ /bbb1/index.html break;
 41         #   }
 42         location ~ /bbb* {
 43         rewrite ^(.*)$ /bbb2/index.html break;
 44            }
 45         location ^~ /bbb {
 46         rewrite ^(.*)$ /bbb3/index.html break;
 47            }
 48        }
[root@7-10 aaa]# nginx -t  
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@7-10 aaa]# systemctl restart nginx
[root@7-10 aaa]# curl http://192.168.100.10/bbb1  #再次访问本地,发现访问的变成了bbb3目录
abc 3

(3)再次注释掉前缀匹配那一行

[root@7-10 aaa]# vim /usr/local/nginx/conf/nginx.conf
。。。。。。
 34     server {
 35         listen 80;
 36         server_name  192.168.100.10;
 37         root /aaa;
 38         index index.html;
 39        # location = /bbb1 {
 40         #rewrite ^(.*)$ /bbb1/index.html break;
 41         #   }
 42         location ~ /bbb* {
 43         rewrite ^(.*)$ /bbb2/index.html break;
 44            }
 45         #location ^~ /bbb {
 46         #rewrite ^(.*)$ /bbb3/index.html break;
 47         #   }
 48        }
。。。。。。
#保存退出
[root@7-10 aaa]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@7-10 aaa]# systemctl restart nginx
[root@7-10 aaa]# curl http://192.168.100.10/bbb1  #再次访问本地,发现访问的是bbb2目录
abc 2

#由此得知,location的优先级是精确匹配大于前缀匹配大于区分大小写匹配大于默认匹配的(nginx默认是默认匹配)

=>正则匹配>^~>~>~*>默认匹配

3、try_files的使用(页面跳转)

  • 实验环境:
系统ip主机名ngixn版本
Centos7.4192.168.100.107-10nginx-1.12.0
Centos7.4192.168.100.117-11nginx-1.12.0
  • 实验步骤

nginx的try_files按顺序检查文件是否存在,不存在就按照指定路径跳转

rzy配置:

(1)使用源码包方式安装nginx(略)
(2)修改配置文件,利用$uri进行本地页面跳转
[root@7-10 ~]# vim /usr/local/nginx/conf/nginx.conf
。。。。。。 #修改文件,删除多余注释
 35     server {
 36         listen       80;
 37         server_name  localhost;
 38         root   html;
 39         index  index.html ;
 40         location / {
 41              try_files $uri $uri/ /aaa/index.html;  #uri判断默认目录下也就是访问的指定目录下有没有index.html文件的存在,存在则显示,不存在则通过$uri/重定向到/aaa/index.html,输出index.html的内容,这里的location是/默认匹配,即不管后面访问什么资源都进行跳转
 42         }
 43         error_page   500 502 503 504  /50x.html;
 44         location = /50x.html {
 45             root   html;
 46         }
 47     }
。。。。。。
#保存退出
[root@7-10 ~]# cd /usr/local/nginx/html/ #进入默认的网页目录
[root@7-10 html]# ll
总用量 8
-rw-r--r-- 1 root root 494 4月  21 00:03 50x.html
-rw-r--r-- 1 root root 612 4月  21 00:03 index.html
[root@7-10 html]# echo "aaaaaaaaa" > index.html   #重新写一个页面
[root@7-10 html]# /usr/local/nginx/sbin/nginx  #启动nginx
[root@7-10 html]# curl 192.168.100.10  #访问本地测试是否可以正常访问
aaaaaaaaa
[root@7-10 html]# mkdir aaa #在当前html目录下创建aaa目录
[root@7-10 html]# echo "bbbbbbbbbb" > aaa/index.html #在aaa目录写一个页面
[root@7-10 html]# rm -rf index.html  #删除当前html目录的index.html页面
[root@7-10 html]# curl 192.168.100.10/afsdf #进行测试,发现就算不是访问aaa目录也能跳转到aaa目录的页面
bbbbbbbbbb

******(3)再次修改配置文件,利用porxy_pass和$uri进行网页跳转
[root@7-10 html]# vim /usr/local/nginx/conf/nginx.conf
。。。。。。
 35     server {
 36         listen       80;
 37         server_name  localhost;
 38         root   html;
 39         index  index.html ;
 40         location / {
 41              try_files $uri @java_page; #配置眺转,这个@后面的字符只要跟下面的location匹配就行
 42         }
 43         location @java_page{
 44              proxy_pass http://192.168.100.11;  #配置跳转的java_page是http://192.168.100.11
 45         } 
 46         error_page   500 502 503 504  /50x.html;
 47         location = /50x.html {
 48             root   html;
 49         }
。。。。。。
#保存退出
[root@7-10 html]# curl 192.168.100.10 #先进行访问
aaaaaaa
[root@7-10 html]# ll
总用量 8
-rw-r--r-- 1 root root 494 4月  21 00:03 50x.html
drwxr-xr-x 2 root root  24 4月  21 00:08 aaa
-rw-r--r-- 1 root root   8 4月  21 00:10 index.html
[root@7-10 html]# rm -rf index.html  #删除index.html
[root@7-10html]# killall -9 nginx  #关闭nginx
[root@7-10 html]# /usr/local/nginx/sbin/nginx  #开启nginx使配置文件生效
[root@7-10 html]# curl 192.168.100.10  #再次访问本地,发现访问的是192.168.100.11的页面,说明成功跳转
7-11

7-11的配置

1)使用源码包方式安装nginx(略)
2)配置网页开启Nginx即可
[root@7-11 ~]# echo "7-11" > /usr/local/nginx/html/index.html 
[root@7-11 ~]# /usr/local/nginx/sbin/nginx 
[root@7-11 ~]# curl 192.168.100.11
7-11

4、alias与root的区别

  • 实验环境:
系统ip主机名ngixn版本
Centos7.4192.168.100.107-10nginx-1.12.0
  • 实验步骤
(1)使用源码包方式安装nginx
(2)修改配置文件,root的作用
[root@7-10 ~]# vim /usr/local/nginx/conf/nginx.conf
。。。。。。
 35     server {
 36         listen       80;
 37         server_name localhost;
 38         location /bbb/aaa {  #指定访问的资源,即用户访问服务器下的/bbb/aaa时,实际显示的时/html/bbb/aaa下的index,html
 39         root html;     #root只能写一个,location写了server就不能写了
 40         index index.html;
 41         }
 42     }
 43 }
。。。。。。
#保存退出
[root@7-10 ~]# cd /usr/local/nginx/html/ 
[root@7-10 html]# mkdir -p bbb/aaa
[root@7-10 html]# mkdir -p bbb/aaa
[root@7-10 html]# echo "aaaaaa" > bbb/aaa/index.html
[root@7-10 html]# curl http://192.168.100.10/bbb/aaa/
aaaaaa   #实际访问的是/html/bbb/aaa/index.html

******(3)修改配置文件,alias的作用
[root@7-10 html]# vim /usr/local/nginx/conf/nginx.conf
。。。。。。
 35     server {
 36         listen       80;
 37         server_name localhost;
 38         location /bbb/aaa {   #访问/bbb/aaa/时会跳转
 39         alias html;   #root修改为alias,会直接跳转
 40         index index.html;
 41         }
 42     }
。。。。。。
#保存退出
[root@7-10 html]# echo "111111" > index.html
[root@7-10 html]# killall -9 nginx
[root@7-10 html]# /usr/local/nginx/sbin/nginx 
[root@7-10 html]# curl http://192.168.100.10/bbb/aaa/ #再次访问发现直接跳转了
111111

(4)alias和root的区别
root指定路径例如:
   location /bbb/aaa {
        root html;
        index index.html;
        }
location指定资源,即用户访问这个资源的时候location中的配置才会生效,location / 默认匹配则全部生效
这里的配置,即用户访问/bbb/aaa/资源时,location中的配置生效,root其实就是又加了一个根路径。用户实际访问变成了/html/bbb/aaa/下的资源

alias指定路径例如:
   location /bbb/aaa {
        alias html;
        index index.html;
        }
这里的location和上面的相同。都是用户访问/bbb/aaa的资源才会生效
alias在用户界面的命令中就是别名的意思,在这里也一样的,用户实际访问直接变成了/html/下的资源

5、获取用户真实ip

  • 实验环境:
系统ip主机名ngixn版本
Centos7.4192.168.100.107-10nginx-1.10.0
Centos7.4192.168.100.117-11nginx-1.11.0
  • 实验步骤

7-10配置

(1)使用源码包方式安装nginx(略)
(2)修改配置文件
[root@7-10 html]# vim /usr/local/nginx/conf/nginx.conf
。。。。。。
 35     server {
 36         listen       80;
 37         server_name localhost;
 38         root html;
 39         index index.html ;
 40         location / {
 41           try_files $uri @aaa;    #配置网页调转
 42         }
 43         location @aaa {
 44              proxy_pass http://192.168.100.11;  #跳转到100.203
 45         }
 46     }
 47 }
。。。。。。
#保存退出
[root@7-10 html]# killall -9 nginx
[root@7-10 html]# /usr/local/nginx/sbin/nginx  #重启

7-11配置

(1)使用源码包方式安装nginx(略)
(2)配置网页目录和开启 Nginx即可
[root@7-11 ~]# echo "7-11" > /usr/local/nginx/html/index.html 
[root@7-11 ~]# /usr/local/nginx/sbin/nginx 
[root@7-11 ~]# curl 192.168.100.11
7-11
(1)分别查看两台服务器的nginx访问日志
7-10:
[root@7-10 html]# tail -1 /usr/local/nginx/logs/access.log  
192.168.100.200 - - [21/Apr/2021:01:13:50 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.100.10/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400"
#可以发现是100.200访问了7-10
7-11:
[root@7-11 ~]# tail -1 /usr/local/nginx/logs/access.log 
192.168.100.200 - - [21/Apr/2021:00:42:48 +0800] "GET /favicon.ico HTTP/1.0" 404 555 "http://192.168.100.10/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400"
#但是在7-11只看到是7-10访问了,看不到是谁真正的访问了它

******(2)配置7-10来使7-11可以看到真正访问它的ip地址
[root@7-10 html]# vim /usr/local/nginx/conf/nginx.conf
。。。。。。
 35     server {
 36         listen       80;
 37         server_name localhost;
 38         root html;
 39         index index.html ;
 40         location / {
 41           try_files $uri @aaa;
 42         }
 43         location @aaa {
 44             proxy_pass http://192.168.100.11;
 45             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #添加这段配置
 46         }
 47     }
 48 }
。。。。。。
#保存退出
[root@rzy html]# killall -9 nginx                                                                              
[root@rzy html]# /usr/local/nginx/sbin/nginx  #重新启动nginx

******(3)再次使用本机的浏览器访问rzy,然后查看日志
7-10:
[root@rzy html]# tail -1 /usr/local/nginx/logs/access.log 
192.168.100.200 - - [21/Apr/2021:01:19:46 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400"


7-11:
[root@7-11 ~]# vim /usr/local/nginx/conf/nginx.conf
。。。。。。
 21     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  #把这三行去掉注释,让配置项生效
 22                      # '$status $body_bytes_sent "$http_referer" '
 23                      # '"$http_user_agent" "$http_x_forwarded_for"';
 24 
。。。。。。
#保存退出
[root@7-11 ~]# killall -9 nginx
[root@7-11 ~]# /usr/local/nginx/sbin/nginx  #重启服务
[root@7-11 ~]# > /usr/local/nginx/logs/access.log  #清空日志
[root@7-11 ~]# tail -f /usr/local/nginx/logs/access.log   #tail -f 即实时查看
192.168.100.10 - - [21/Apr/2021:00:54:14 +0800] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400" "192.168.100.200"
#发现在日志的最后增加了一个ip地址,而这个ip就是访问7-11的真实ip

7、Nginx优化方案总结

  1. Gizp压缩
  2. Expires静态文件缓存
  3. 调整网络IO模型,调整Nginx worker进程的最大连接数
  4. 隐藏Nginx名称和版本号
  5. 配置防盗链,防止资源被盗用
  6. 禁止通过ip地址访问,进程恶意域名解析,只允许域名访问
  7. 防止DDos、CC攻击,限制单ip并发请求连接
  8. 配置错误页面,根据错误代码指定网页反馈用户
  9. 限制上传资源目录被程序访问,防止木马入侵系统
  10. Nginx加密传输优化
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值