Ngixn dynamic and static separation detailed configuration method

Foreword:

  In order to speed up the analysis speed of the website, dynamic pages and static pages can be parsed by different servers to speed up the analysis speed. Reduce the pressure of the original single server. It is more obvious when tomcat separates dynamics and statics, because tomcat is very slow to parse statics. In fact, these principles are well understood. In simple terms, use regular expressions to match and filter, and then hand them to different servers.
  Static pages are generally processed directly by Nginx, while dynamic pages are proxied to the back-end Tomcat through a reverse proxy, and then load balancing is performed. Whether to choose a local static page or a back-end Tomcat is determined by the load balancing configuration.
  Dynamic and static separation is done after load balancing. For example, there are multiple static wbes and multiple dynamic webs. First, dynamic and static separation is performed, and then load balancing and weighting are performed in each cluster.
Insert picture description here


Ready to work

System: Centos7
Nginx version: 1.18.0
Address: 10.8.161.75
Test machine system: Win10
address: 10.8.161.61 (same LAN)
Nginx proxy host port: 80
Static host port: 91, 92, 93, 94, 95
dynamic host port : 96, 97


1. Static host configuration

  1. The configuration of server1 is as follows, the other four configurations are similar
vim /etc/nginx/conf.d/server1.conf  # 在子配置文件中创建第一台配置
###   配置如下   ###
server {
    
    
        listen       91;     #定义端口为 91 端口,其他四台静态主机更换端口
        server_name  10.8.161.75;
        location / {
    
    
                  root   /var/www/nginx/server1;   # 指定网页目录,其他四台静态主机自定义更换
                  index  index.html index.htm;     # 指定访问的网页,其他四台静态主机自定义更换
                  limit_rate  2k;                                                                                                       
                 }
     }

  1. Create server1 webpage, the other four have similar configurations
   vim /var/www/nginx/server1/index.html   # 配置文件中指定的目录
   
   ### 内容如下 ###
   111111111111
   

2. Dynamic host configuration

  1. Install PHP7.1
###     添加PHP 7.1 yum源      ###
rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
###     添加PHP 升级源         ###
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
###    yum安装PHP和运行的环境   ###
yum install php71w-xsl php71w php71w-ldap php71w-cli php71w-common php71w-devel php71w-gd php71w-pdo php71w-mysql php71w-mbstring php71w-bcmath php71w-mcrypt -y

  1. Start the php service, and set to start automatically
systemctl start php-fpm
systemctl enable php-fpm
  1. Create a dynamic host configuration, using php1 as an example, the other configuration is similar to the
    host configuration file
vim  /etc/nginx/conf.d/php.conf
server {
    
    
        listen      96;
        server_name     localhost;
        location ~ \.php$ {
    
    
                    root          /var/www/nginx/php;  #指定动态网站目录
                    fastcgi_pass   127.0.0.1:9000;    #指定访问地址
                    fastcgi_index  index.php;       #指定默认文件
                    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  #站点根目录,取决于root配置项
                    include        fastcgi_params;  #包含nginx常量定义
                        }
}

②Create a php1 webpage, the other one has similar configuration

 vim /var/www/nginx/php/index.php   #根据动态主机1指定的配置文件指定的目录和主页创建
###  内容如下  ### 
phphphphphphphp

3. Dynamic and static separation configuration

upstream jing {
    
                        #设置静态主机池
    # ip_hash;                     #负载均衡算法,不添加默认是轮询状态
        server 10.8.161.75:91;     #指定静态主机
        server 10.8.161.75:92;    
        server 10.8.161.75:93;
        server 10.8.161.75:94;
        server 10.8.161.75:95;
}
 
upstream dong {
    
                      #设置动态主机池
    #  ip_hash;                  #负载均衡算法,不添加默认是轮询状态 
        server 10.8.161.75:96;   #指定动态主机
        server 10.8.161.75:97;
}

server {
    
                             #代理主机配置
        listen 80;               #开启80端口 ,检查其他80端口,不要与其冲突
        server_name localhost;   #指定本机
        #动态主机 
        location ~ \.(php|jsp)$ {
    
                                         # 使用正则匹配网页后缀,如果是动态页面,跳转到动态主机池下去寻找主机       
               proxy_pass  http://dong;                               # 指定动态主机池
             # proxy_set_header Host $http_host;                      # 真实服务器的地址,可以是ip也可以是域名和url地址
             # proxy_set_header X-Real-IP $remote_addr;               # 启用客户端真实地址(否则日志中显示的是代理在访问网站)
             # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 记录代理地址
             # proxy_connect_timeout 30;                              # 后端服务器连接的超时时间发起三次握手等候响应超时时间
             # proxy_send_timeout 60;                                 # 后端服务器数据回传时间,就是在规定时间之内后端服务器必须传完所有的数据
             # proxy_read_timeout 60;                                 # nginx接收upstream(上游/真实) server数据超时, 默认60s, 如果连续的60s内没有收到1个字节, 连接关闭。像长连接
                }
        #静态主机
          location  ~ .*\.(html|gif|jpg|png|bmp|swf|css|js)$  {
    
           # 使用正则匹配网页后缀,如果是静态页面,跳转到静态主机池下去寻找主机         
               proxy_pass  http://jing;                               # 指定静态主机池
             # proxy_set_header Host $http_host;                      # 真实服务器的地址,可以是ip也可以是域名和url地址
             # proxy_set_header X-Real-IP $remote_addr;               # 启用客户端真实地址(否则日志中显示的是代理在访问网站)
             # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 记录代理地址
             # proxy_connect_timeout 30;                              # 后端服务器连接的超时时间发起三次握手等候响应超时时间
             # proxy_send_timeout 60;                                 # 后端服务器数据回传时间,就是在规定时间之内后端服务器必须传完所有的数据
             # proxy_read_timeout 60;                                 # nginx接收upstream(上游/真实) server数据超时, 默认60s, 如果连续的60s内没有收到1个字节, 连接关闭。像长连接
        }
}



Four. Configuration file directory structure

  1. Directory structure of all configuration files
    Insert picture description here

  2. Directory structure of all pages
    Insert picture description here


Five. Test

  1. When visiting a static page
    Insert picture description here

  2. When visiting dynamic pages
    Insert picture description here

Guess you like

Origin blog.csdn.net/qq_26129413/article/details/112623216