Nginx性能优化

一、性能优化概述

(1)性能优化之前要考虑的事情

  • 当前系统结构的瓶颈

使用ab压力测试观察指标

  • 了解业务模式

接口业务类型,系统层次化结构

  • 性能与安全

性能好安全弱,安全好性能低

(2)压力测试工具

******(1)做基础配置(略)
******(2)使用源代码包安装nginx(略)
******(3)使用yum安装ab
[root@rzy ~]# yum -y install httpd-tools  #ab是apache自带的,但是没有安装apache所以需要使用yum安装
。。。。。。
完毕!

******(4)使用ab压力测试工具测试nginx  #一般都是整个服务器架构搭建好之后在进行ab测试
[root@rzy ~]# ab -n 1000 -c 20 http://127.0.0.1/
#总共1000个请求,20个并发
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        nginx/1.18.0
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /
Document Length:        612 bytes

Concurrency Level:      20
Time taken for tests:   0.079 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      845000 bytes
HTML transferred:       612000 bytes
Requests per second:    12624.35 [#/sec] (mean)
Time per request:       1.584 [ms] (mean)
Time per request:       0.079 [ms] (mean, across all concurrent requests)
Transfer rate:          10417.55 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       0
Processing:     0    1   5.9      1      43
Waiting:        0    1   5.8      0      43
Total:          1    2   5.9      1      43

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      1
  98%     43
  99%     43
 100%     43 (longest request)

(3)影响nginx服务器性能的指标

  • 网络: 网络的流量,网络是否丢包,这些会影响http的请求与调用
  • 系统: 硬件有没有磁盘损坏,磁盘速率
  • 服务: 连接优化,请求优化
  • 程序: 接口性能,处理速度,程序执行效率
  • 数据库: 每个架构服务与服务之间或多或少有一些关联,我们需要将整个架构进行分层,找到对应系统或服务的短板,然后进行优化

例如:Nginx交给Tomcat,Tomcat在交给mysql数据

二、Nginx性能优化

(1)Nginx基础配置

******(1)使用源码包方式安装nginx(略)
******(2)查看Nginx基础配置文件
[root@rzy ~]# ps aux | grep nginx  #查看nginx的进程
root       1044  0.0  0.0  20520   620 ?        Ss   17:12   0:00 nginx: master process   #/usr/local/nginx/sbin/nginx   #Nginx的主进程
nginx      1045  0.0  0.1  20956  1076 ?        S    17:12   0:00 nginx: worker process  #Nginx的工作进程
root       1047  0.0  0.0 112676   984 pts/0    R+   17:12   0:00 grep --color=auto nginx
——————————————————————————————————————————————————————————————————————
#nginx的主配置文件,一个区块对应一个{},不在{}里的表示是全局配置 
#main位于nginx。conf的最高层
#main层下面可以有Event和HTTP层
#HTTP层下面可以有多个Server层。用于对不同网站做不同的配置
#Server层也允许有多个Location
——————————————————————————————————————————————————————————————————————
[root@rzy ~]# vim /usr/local/nginx/conf/nginx.conf 
#user  nobody;            #设置nginx服务的系统使用用户
worker_processes  1;      #工作进程,配置要和CPU个数保持一致

#error_log  logs/error.log;        #错误日志,后面的是路径
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;        #Nginx服务启动时的pid


events {    #事件模块
    use epoll;                       #use指定Nginx的内核模型,一般是epoll
    worker_connections  1024;        #每个worker进程支持的最大连接数
}

                    
http {              #非虚拟主机的配置或者公共配置要配置到httpd{}中,Server{}外
    include       mime.types;
    default_type  application/octet-stream;
    charset utf-8      #统一使用utf-8字符集
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;  
    #tcp_nopush     on;   #静态资源服务器建议打开

    #keepalive_timeout  0;   
    keepalive_timeout  65;  #动态资源服务器需要打开keepalived

    #gzip  on;

    server {        #必须使用虚拟主机配置站点,每个虚拟主机使用一个Server{}
        listen       80;           #监听端口,默认80
        server_name  localhost;    #提供服务的域名或者主机名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;                     #存放网站路径,是相对路径,即/usr/local/nginx
            index  index.html index.htm;     #默认可以解析的页面,配合php可以解析.php的动态页面
        }                                      #可以写多个server配置,配置多个虚拟主机

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {  
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

(2)系统性能优化

文件句柄:Linux系统中一切皆文件,文件句柄可以理解为是一个索引,文件句柄会随着我们频繁调用进程的而增加

系统默认对文件句柄有着限制,不会让一个进程无限的调用,需要限制每个进程和每个服务可以调用的进程的上限,故而可以限制文件句柄的上限(默认限制为1024个)

文件句柄是必须要调整的优化参数,不修改的话当进程达到限制数量就会报错:Error:too many open files

文件句柄的限制方式有两种设置方式,分别是系统全局性修改(直接对系统生效),用户局部性修改(只对某个用户生效)
[root@rzy ~]# vim /etc/security/limits.conf  #设置的主配置文件
。。。。。。#最后一行添加
#用户局部性修改
root soft nofile 65535   #软限制,最大65535,想要所有用户都进行限制,只需要把root改成*即可,最大限制一般为65535
root hard nofile 65535   #硬限制
#对Nginx进程进行限制
worker_rlimit_nofile 45535;   #对Nginx进程限制最大45535

使用xshell再开一个终端,使用ulimit -n 进行查询,没改之前是默认的1024

**
修改文档之后,再次重新开一个终端,发现已经成功修改
在这里插入图片描述

(3)Nginx性能优化之CPU亲和

CPU亲和,也叫CPU绑定,减少进程之间不断频繁迁移,减少性能损耗

#查看当前CPU物理状态
[root@rzy ~]# lscpu | grep "CPU(s)"  #通过grep查看筛选
CPU(s):                1
On-line CPU(s) list:   0

#配置主配置文件进行绑定
[root@rzy ~]# vim /usr/local/nginx/conf/nginx.conf
  1
  2 #user  nobody;
  3 worker_processes  1;  #这里是1核,所以这里绑定一个核心
  4                       #这里可以写worker_cpu_affinity 两核写01 10 四核写0001 0010 0100 1000依次类推
。。。。。。
也可以写成自动:
worker_processes auto
worker_cpu_affinity auto

(4)Nginx常见问题

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

  • 实验环境:
系统ip主机名ngixn版本
Centos7.4192.168.100.202rzynginx-1.18.0
  • 实验步骤:
******(1)使用源码包方式安装nginx(略)
******(2)创建三个目录
[root@rzy ~]# mkdir -p /aaa/bbb{1..3}  #创建aaa目录下以bbb开头后面分别是1到3的目录
[root@rzy ~]# for i in {1..3};do echo "abc $i" > /aaa/bbb"$i"/index.html;done #使用for循环,给三个目录下写网页

******(3)修改主配置文件
[root@rzy ~]# vim /usr/local/nginx/conf/nginx.conf #以#开头的注释行可以删除
。。。。。。  #在http{}里修改,创建三个虚拟主机,分别对应刚才创建的三个不同的目录
 34     server {
 35         listen 80;
 36         server_name 192.168.100.202;
 37         location / {
 38              root /aaa/bbb2;
 39              index index.html;
 40            }
 41        }
 42     server {
 43         listen 80;
 44         server_name 192.168.100.202;
 45         location / {
 46              root /aaa/bbb1;
 47              index index.html;
 48            }
 49        }
 50     server {
 51         listen 80;
 52         server_name 192.168.100.202;
 53         location / {
 54              root /aaa/bbb3;
 55              index index.html;
 56            }
 57        }
 58 
。。。。。。
#保存退出
[root@rzy ~]# nginx -t  #检查nginx语句是否正确
nginx: [warn] conflicting server name "192.168.100.202" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "192.168.100.202" 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@rzy ~]# systemctl restart nginx  #重启服务
[root@rzy ~]# curl 127.0.0.1  #访问本地 
abc 2      
#发现访问的是bbb2目录的,但是主配置文件里的server虚拟主机不是按1、2、3的这种顺序写的,而bbb2在第一个,说明当servername相同时,访问的优先级是从上到下的(域名都包含192.168.100.202)

2、location优先级

  • 实验环境:
系统ip主机名ngixn版本
Centos7.4192.168.100.202rzynginx-1.18.0
  • location匹配优先级

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

完整匹配优先级
=进行普通字符的精确匹配,即完全匹配
^~表示普通字符匹配,使用前缀匹配
正则匹配匹配后会继续查找更加精确的location
~区分大小写匹配
~*不区分大小写匹配
/默认匹配
  • 实验步骤:
******继续上面的操作步骤
******(1)修改主配置文件
[root@rzy ~]# vim /usr/local/nginx/conf/nginx.conf
。。。。。。#还是修改server虚拟主机
 34     server {
 35         listen 80;
 36         server_name  192.168.100.202;
 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@rzy 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@rzy aaa]# systemctl restart nginx  #重启服务
[root@rzy aaa]# curl http://192.168.100.202/bbb1  #访问本地,指定访问目录
abc 1

******(2)再次修改配置文件,注释掉精确匹配那一行
[root@rzy aaa]# vim /usr/local/nginx/conf/nginx.conf
。。。。。。
 34     server {
 35         listen 80;
 36         server_name  192.168.100.202;
 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@rzy 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@rzy aaa]# systemctl restart nginx
[root@rzy aaa]# curl http://192.168.100.202/bbb1  #再次访问本地,发现访问的变成了bbb3目录
abc 3

******(3)再次注释掉前缀匹配那一行
[root@rzy aaa]# vim /usr/local/nginx/conf/nginx.conf
。。。。。。
 34     server {
 35         listen 80;
 36         server_name  192.168.100.202;
 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@rzy 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@rzy aaa]# systemctl restart nginx
[root@rzy aaa]# curl http://192.168.100.202/bbb1  #再次访问本地,发现访问的是bbb2目录
abc 2

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

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

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

  • 实验环境:
系统ip主机名ngixn版本
Centos7.4192.168.100.202rzynginx-1.18.0
Centos7.4192.168.100.203rzy02nginx-1.18.0
  • 实验步骤

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

rzy配置:

******(1)使用源码包方式安装nginx(略)
******(2)修改配置文件,利用$uri进行本地页面跳转
[root@rzy ~]# 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@rzy ~]# cd /usr/local/nginx/html/ #进入默认的网页目录
[root@rzy html]# ll
总用量 8
-rw-r--r-- 1 root root 494 421 00:03 50x.html
-rw-r--r-- 1 root root 612 421 00:03 index.html
[root@rzy html]# echo "aaaaaaaaa" > index.html   #重新写一个页面
[root@rzy html]# /usr/local/nginx/sbin/nginx  #启动nginx
[root@rzy html]# curl 127.0.0.1  #访问本地测试是否可以正常访问
aaaaaaaaa
[root@rzy html]# mkdir aaa #在当前html目录下创建aaa目录
[root@rzy html]# echo "bbbbbbbbbb" > aaa/index.html #在aaa目录写一个页面
[root@rzy html]# rm -rf index.html  #删除当前html目录的index.html页面
[root@rzy html]# curl 127.0.0.1/afsdf #进行测试,发现就算不是访问aaa目录也能跳转到aaa目录的页面
bbbbbbbbbb

******(3)再次修改配置文件,利用porxy_pass和$uri进行网页跳转
[root@rzy 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.203;  #配置跳转的java_page是http://192.168.100.203
 45         } 
 46         error_page   500 502 503 504  /50x.html;
 47         location = /50x.html {
 48             root   html;
 49         }
。。。。。。
#保存退出
[root@rzy html]# curl 127.0.0.1 #先进行访问
aaaaaaa
[root@rzy html]# ll
总用量 8
-rw-r--r-- 1 root root 494 421 00:03 50x.html
drwxr-xr-x 2 root root  24 421 00:08 aaa
-rw-r--r-- 1 root root   8 421 00:10 index.html
[root@rzy html]# rm -rf index.html  #删除index.html
[root@rzy html]# killall -9 nginx  #关闭nginx
[root@rzy html]# /usr/local/nginx/sbin/nginx  #开启nginx使配置文件生效
[root@rzy html]# curl 127.0.0.1  #再次访问本地,发现访问的是192.168.100.203的页面,说明成功跳转
rzy-02

rzy02配置:

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

4、alias与root的区别

  • 实验环境:
系统ip主机名ngixn版本
Centos7.4192.168.100.202rzynginx-1.18.0
  • 实验步骤
******(1)使用源码包方式安装nginx
******(2)修改配置文件,root的作用
[root@rzy ~]# 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@rzy ~]# cd /usr/local/nginx/html/ 
[root@rzy html]# mkdir -p bbb/aaa
[root@rzy html]# mkdir -p bbb/aaa
[root@rzy html]# echo "aaaaaa" > bbb/aaa/index.html
[root@rzy html]# curl http://192.168.100.202/bbb/aaa/
aaaaaa   #实际访问的是/html/bbb/aaa/index.html

******(3)修改配置文件,alias的作用
[root@rzy 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@rzy html]# echo "111111" > index.html
[root@rzy html]# killall -9 nginx
[root@rzy html]# /usr/local/nginx/sbin/nginx 
[root@rzy html]# curl http://192.168.100.202/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.202rzynginx-1.18.0
Centos7.4192.168.100.203rzy02nginx-1.18,0
  • 实验步骤

rzy配置

******(1)使用源码包方式安装nginx(略)
******(2)修改配置文件
[root@rzy 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.203;  #跳转到100.203
 45         }
 46     }
 47 }
。。。。。。
#保存退出
[root@rzy html]# killall -9 nginx
[root@rzy html]# /usr/local/nginx/sbin/nginx  #重启

rzy02配置

******(1)使用源码包方式安装nginx(略)
******(2)配置网页目录和开启 Nginx即可
[root@rzy02 ~]# echo "rzy-02" > /usr/local/nginx/html/index.html 
[root@rzy02 ~]# /usr/local/nginx/sbin/nginx 
[root@rzy02 ~]# curl 127.0.0.1
rzy-02

先使用本机浏览器访问,发现可以成功眺转
在这里插入图片描述

******(1)分别查看两台服务器的nginx访问日志
rzy:
[root@rzy html]# tail -1 /usr/local/nginx/logs/access.log  
192.168.100.230 - - [21/Apr/2021:01:13:50 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.100.202/" "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.230访问了rzy
rzy02:
[root@rzy02 ~]# tail -1 /usr/local/nginx/logs/access.log 
192.168.100.202 - - [21/Apr/2021:00:42:48 +0800] "GET /favicon.ico HTTP/1.0" 404 555 "http://192.168.100.202/" "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"
#但是在rzy02只看到是rzy访问了,看不到是谁真正的访问了它

******(2)配置rzy来使rzy02可以看到真正访问它的ip地址
[root@rzy 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.203;
 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,然后查看日志
rzy:
[root@rzy html]# tail -1 /usr/local/nginx/logs/access.log 
192.168.100.230 - - [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"


rzy02:
[root@rzy02 ~]# 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@rzy02 ~]# killall -9 nginx
[root@rzy02 ~]# /usr/local/nginx/sbin/nginx  #重启服务
[root@rzy02 ~]# > /usr/local/nginx/logs/access.log  #清空日志
[root@rzy02 ~]# tail -f /usr/local/nginx/logs/access.log   #tail -f 即实时查看
192.168.100.202 - - [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.230"
#发现在日志的最后增加了一个ip地址,而这个ip就是访问rzy02的真实ip

6、网站访问原理

  • DNS流程,即解析域名
  1. 先查询本地hosts文件
  2. 请求本地localDNS
  3. 返回对应的IP地址
  • HTTP连接
  1. 首先先建立TCP连接,即TCP的三次握手,发送请求内容、请求起始行、请求头部、请求的主体

    • 将请求传递给负载均衡,通过负载均衡做出相应的请求调度

    • 如果请求的是静态页面,那么会将请求调度到对应的静态集群解析

    • 如果请求的是动态页面,那么会将请求调度到对应的动态集群解析

    • 如果仅仅是请求页面,那么可以会通过Opcache

  2. 如果请求的页面需要查询数据库,或者是往数据库输入内容,那么就需要进行检查

    • 检查对应的操作是查询还是写入,如果是查询数据库
    • 会检查查询的内容是否有缓存,如果有缓存则返回,没有缓存则开始从数据库查询数据
    • 检查查询语句,将查询结果返回
  3. 返回对应客户端的请求内容至WEB节点

  4. WEB节点收到请求后返回内容至负载均衡

  5. 负责均衡返回客户端内容,然后TCP连接断开,即TCP的四次断开

  6. HTTP断开连接

  • 按照分层结构,每一层都有其对应的缓存机制

C D N 层 — — 负 载 均 衡 层 — — W E B 层 — — 存 储 层 — — 缓 存 层 — — 数 据 库 层 CDN层——负载均衡层——WEB层——存储层——缓存层——数据库层 CDNWEB

7、Nginx优化方案总结

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

接**

  1. 首先先建立TCP连接,即TCP的三次握手,发送请求内容、请求起始行、请求头部、请求的主体

    • 将请求传递给负载均衡,通过负载均衡做出相应的请求调度

    • 如果请求的是静态页面,那么会将请求调度到对应的静态集群解析

    • 如果请求的是动态页面,那么会将请求调度到对应的动态集群解析

    • 如果仅仅是请求页面,那么可以会通过Opcache

  2. 如果请求的页面需要查询数据库,或者是往数据库输入内容,那么就需要进行检查

    • 检查对应的操作是查询还是写入,如果是查询数据库
    • 会检查查询的内容是否有缓存,如果有缓存则返回,没有缓存则开始从数据库查询数据
    • 检查查询语句,将查询结果返回
  3. 返回对应客户端的请求内容至WEB节点

  4. WEB节点收到请求后返回内容至负载均衡

  5. 负责均衡返回客户端内容,然后TCP连接断开,即TCP的四次断开

  6. HTTP断开连接

  • 按照分层结构,每一层都有其对应的缓存机制

C D N 层 — — 负 载 均 衡 层 — — W E B 层 — — 存 储 层 — — 缓 存 层 — — 数 据 库 层 CDN层——负载均衡层——WEB层——存储层——缓存层——数据库层 CDNWEB

7、Nginx优化方案总结

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值