nginx realizes dynamic and static separation

Keywords: Linux Operation & Maintenance Load Balance Nginx

1. What is dynamic and static separation

Dynamic and static separation is mainly realized through nginx + PHP FPM, in which nginx handles static files such as pictures and html, and PHP handles dynamic programs.

Dynamic static separation refers to the architecture design method of separating static pages from dynamic pages or static content interfaces from dynamic content interfaces in the web server architecture, so as to improve the access performance and maintainability of the whole service.

To put it simply, when the user requests, if he simply accesses static requests such as pictures and html, nginx returns directly. If he sends a dynamic request, nginx sends the request to the program for dynamic processing.

2. nginx reverse proxy and load balancing

  • nginx is usually used as the reverse proxy of the back-end server, which can easily realize dynamic and static separation and load balancing, so as to greatly improve the processing capacity of the server.

  • Nginx realizes dynamic and static separation. In fact, when reverse proxy is used, if it is static resources, it will be read directly from the path published by nginx, rather than from the background server.

  • However, it should be noted that in this case, it is necessary to ensure that the back-end and front-end programs are consistent. Rsync can be used for server-side automatic synchronization or NFS and MFS distributed shared storage.

  • Http Proxy module has many functions. Proxy is the most commonly used_ Pass and proxy_cache

  • If you want to use proxy_cache needs to integrate the NGX of the third party_ cache_ Purge module, used to clear the specified URL cache. This integration needs to be done when installing nginx, such as:
    ./configure --add-module=.../ngx_cache_purge-1.0 ...

  • nginx implements simple load balancing through the upstream module, which needs to be defined in the http segment

  • In the upstream segment, define a server list. The default method is polling. If you want to make sure that the requests sent by the same visitor are always processed by the same back-end server, you can set ip_hash, such as:

upstream idfsoft.com {
  ip_hash;
  server 127.0.0.1:9080 weight=5;
  server 127.0.0.1:8080 weight=5;
  server 127.0.0.1:1111;
}

Note: the essence of this method is polling, and because the ip of the client may change constantly, such as dynamic ip, proxy, wall climbing, etc., ip_hash does not fully guarantee that the same client is always processed by the same server.

After defining upstream, you need to add the following contents in the server section:

server {
  location / {
    proxy_pass http://idfsoft.com;
  }
}

3. nginx realizes load balancing

Environmental description

systemIPservicehost name
Redhat8.2192.168.182.141nginxLB
Redhat8.2192.168.182.142nginxrs1
Redhat8.2192.168.182.143httpdrs2

First deploy the web server on rs1 and rs2

[root@RS1 ~]# yum -y install nginx
[root@RS2 ~]# yum -y install httpd
[root@RS1 ~]# systemctl start nginx.service
[root@RS2 ~]# systemctl start httpd.service

Modify the configuration file of nginx on LB host

upstream webserver {
        server 192.168.182.142;
        server 192.168.182.143;
     }

location / {
 44            proxy_pass http://webservers;
 45         }
[root@LB conf]# nginx -s reload

Enter the IP on the LB host for load balancing

4. nginx realizes dynamic and static separation

Environmental description

systemIPservicehost name
Redhat8.2192.168.182.141nginxdr
Redhat8.2192.168.182.143lnmprs1
Redhat8.2192.168.182.142httpdrs2

The first service to deploy web services in rs1 and rs2

rs1 Upper deployment lnmp
 Create system user nginx
[root@RS1 ~]# useradd -r -M -s /sbin/nologin nginx

Installation dependent environment
[root@RS1 ~]# yum -y install pcre-devel pcre gcc gcc-c++ openssl-devel zlib zlib-devel make vim wget openssl openssl-devel gd-devel

Create log storage directory
[root@RS1 ~]# mkdir -p /var/log/nginx
[root@RS1 ~]# chown nginx.nginx /var/log/nginx/

download nginx
[root@RS1 ~]# wget http://nginx.org/download/nginx-1.20.1.tar.gz

[root@RS1 ~]#  tar xf nginx-1.20.1.tar.gz 
[root@RS1 ~]# cd nginx-1.20.1/
[root@RS1 nginx-1.20.1]#  ./configure  --prefix=/usr/local/nginx  --user=nginx  --group=nginx  --with-debug  --with-http_ssl_module  --with-http_realip_module  --with-http_image_filter_module  --with-http_gunzip_module  --with-http_gzip_static_module  --with-http_stub_status_module  --http-log-path=/var/log/nginx/access.log  --error-log-path=/var/log/nginx/error.log

[root@RS1 nginx-1.20.1]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

[root@RS1 nginx-1.20.1]# cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=Nginx server daemon
After=network.target 

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx 
ExecStop=/usr/local/nginx/sbin/nginx -s quit
ExecReload=/bin/kill -HUP $MAINPID
EOF

[root@RS1 ~]# systemctl daemon-reload 
[root@RS1 ~]# systemctl enable --now nginx.service 


deploy mysql
 Install dependent packages
[root@RS1 ~]# yum -y install gcc gcc-c++ make zlib zlib-devel pcre pcre-devel openssl openssl-devel ncurses-compat-libs perl ncurses-devel cmake

Create users and groups
[root@RS1 ~]# useradd -r -M -s /sbin/nologin mysql

download MySQL of tar package
[root@RS1 ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -P /usr/local
[root@RS1 local]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
[root@RS1 local]# ln -s /usr/local/mysql-5.7.34-linux-glibc2.12-x86_64/ /usr/local/mysql

Add environment variable
[root@RS1 local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@RS1 local]#  source /etc/profile.d/mysql.sh 

Create data storage directory
[root@RS1 local]# mkdir -p /opt/data
[root@RS1 local]# chown -R mysql.mysql /opt/data/

Initialize database
[root@RS1 local]# mysqld --initialize-insecure --user mysql --datadir /opt/data/

Generate profile
[root@RS1 ~]#  cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve

Configure script for service startup
[root@RS1 ~]# vim /usr/local/mysql/support-files/mysql.server
basedir=/usr/local/mysql
datadir=/opt/data

Configure system service usage systemctl To manage MySQL
[Unit]
Description=Mysql server daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

[root@RS1 ~]# systemctl daemon-reload 
[root@RS1 ~]# systemctl enable --now mysqld.service

install php
 Network warehouse required: curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo

[root@RS1 ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd libsqlite3x-devel libzip-devel http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

[root@RS1 ~]# wget https://www.php.net/distributions/php-8.0.11.tar.gz
[root@RS1 ~]# tar xf packages/php-8.0.11.tar.xz -C /usr/local/

[root@RS1 php-8.0.11]# ./configure --prefix=/usr/local/php8  --with-config-file-path=/etc --enable-fpm --disable-debug --disable-rpath --enable-shared --enable-soap --with-openssl --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --enable-exif  --enable-ftp --enable-gd --with-jpeg --with-zlib-dir --with-freetype --with-gettext --enable-mbstring --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-readline --enable-shmop --enable-simplexml --enable-sockets --with-zip --enable-mysqlnd-compression-support --with-pear --enable-pcntl --enable-posix

[root@RS1 php-8.0.11]# make && make install

Configure environment variables
[root@RS1 ~]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@RS1 ~]# source /etc/profile.d/php.sh

[root@RS1 php-8.0.11]# cp php.ini-production /etc/php.ini
[root@RS1 php-8.0.11]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@RS1 php-8.0.11]#  chmod +x /etc/rc.d/init.d/php-fpm
[root@RS1 php-8.0.11]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf

[root@RS1 php-8.0.11]#cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf

[root@RS1 php-8.0.11]# service php-fpm start

Configure system service usage systemctl To manage PHP
[root@RS1 ~]# cp /usr/lib/systemd/system/mysql.service /usr/lib/systemd/system/php-fpm.service

[root@RS1 ~]# cat /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=php server daemon
After=network.target 

[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStop=/etc/init.d/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

[root@RS1 ~]# pkill php-fpm 
[root@RS1 ~]# systemctl daemon-reload 
[root@RS1 ~]# systemctl enable --now php-fpm.service

establish php Access interface
[root@RS1 ~]# vim /usr/local/nginx/html/index.php
[root@RS1 ~]# cat /usr/local/nginx/html/index.php
<?php
        phpinfo();
?>
[root@RS1 ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
       root   html;
       index  index.php index.html index.htm;   //Modify this line
    }
location ~ \.php$ {
       root           html;
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  $Document_Root$fastcgi_script_name;   //Modify this line
       include        fastcgi_params;
}
[root@RS1 ~]# systemctl restart nginx.service 

Installing httpd on rs2

[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# systemctl start httpd

Operate on LB host

[root@DR ~]# vim /usr/local/nginx/conf/nginx.conf
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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;

    #gzip  on;
    upstream static {
        server 192.168.182.142;    //Set static access
     }

    upstream dynamic {
        server 192.168.182.143;  //Set dynamic access
     }

    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            proxy_pass http://static;   // Processing static resources
        }

        #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://dynamic;  // Handle dynamic resources that end in. php.
        }

        # 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;
    #    }
    #}

}


[root@DR ~]# nginx -s reload

Posted by Lautarox on Wed, 24 Nov 2021 00:58:31 -0800