Example of how nginx implements dynamic and static separation

Example of how nginx implements dynamic and static separation

This article mainly introduces an example of how nginx can achieve dynamic and static separation, which has certain learning value. The details are as follows

environment:

System/Hostname IP address Serve
Redhat8: server1 192.168.244.131 nginx
Redhat8: server2 192.168.244.133 lnmp
Content7:node3 192.168.244.142 httpd

Disable the firewall on the three hosts

[root@server1 ~]# systemctl stop firewalld
[root@server1 ~]# systemctl disable firewalld
[root@server1 ~]# vim /etc/selinux/config 
SELINUX=disabled

Deploy nginx on server1

[root@server1 opt]# cat nginx.sh 
#!/bin/bash

if [ $UID -ne 0 ];then
        echo "Please use administrator account"
        exit
fi

app_a=nginx-1.20.1.tar.gz

dir_a=/usr/local
dir_b=/var/log
dir_c=nginx-1.20.1

if [ ! -d $dir_b/nginx ];then
        mkdir -p $dir_b/nginx
fi
chown -R nginx.nginx $dir_b/nginx

yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
yum -y groups mark install 'Development Tools'

id nginx &>/dev/null 
if [ $? -ne 0 ];then
        useradd -r -M -s /sbin/nologin nginx
fi

tar xf bag/$app_a -C $dir_a

cd $dir_a/$dir_c
if [ ! -d $dir_a/nginx ];then
        ./configure \
                --prefix=$dir_a/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=$dir_b/nginx/access.log \
                --error-log-path=$dir_b/nginx/error.log && make && make install
fi

cd ..
if [ ! -f /etc/profile.d/nginx.sh ];then
        echo "export PATH=$dir_a/nginx/sbin:\$PATH" > /etc/profile.d/nginx.sh
fi

cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=Nginx server daemon
Wants=sshd-keygen.target

[Service]
Type=forking
ExecStart=$dir_a/nginx/sbin/nginx
ExecStop=$dir_a/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF


systemctl daemon-reload
systemctl enable --now nginx

View Ports

[root@server1 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port                 
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*                    
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*                    
LISTEN 0 128 [::]:22 [::]:*   

Visit Page

insert image description here

Deploy lnmp on the server

Deploy nginx

[root@server2 lnmp]# cat install.sh 
#!/bin/bash

if [ $UID -ne 0 ];then
        echo "Please use administrator account"
        exit
fi

app_a=nginx-1.20.1.tar.gz

dir_a=/usr/local
dir_b=/var/log
dir_c=nginx-1.20.1

if [ ! -d $dir_b/nginx ];then
        mkdir -p $dir_b/nginx
fi
chown -R nginx.nginx $dir_b/nginx

yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
yum -y groups mark install 'Development Tools'

id nginx &>/dev/null 
if [ $? -ne 0 ];then
        useradd -r -M -s /sbin/nologin nginx
fi

tar xf bag/$app_a -C $dir_a

cd $dir_a/$dir_c
if [ ! -d $dir_a/nginx ];then
        ./configure \
                --prefix=$dir_a/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=$dir_b/nginx/access.log \
                --error-log-path=$dir_b/nginx/error.log && make && make install
fi

cd ..
if [ ! -f /etc/profile.d/nginx.sh ];then
        echo "export PATH=$dir_a/nginx/sbin:\$PATH" > /etc/profile.d/nginx.sh
fi

cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=Nginx server daemon
Wants=sshd-keygen.target

[Service]
Type=forking
ExecStart=$dir_a/nginx/sbin/nginx
ExecStop=$dir_a/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now nginx

Deploy mysql

[root@server2 lnmp]# cat mysql.sh 
#!/bin/bash

if [ $UID -ne 0 ];then
        echo "root?"
        exit
fi

dir_a=/usr/local
dir_b=/opt/data
app_a=mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
app_b=mysql-5.7.34-linux-glibc2.12-x86_64

id mysql &>/dev/null
if [ $? -ne 0 ];then
        useradd -r -M -s /sbin/nologin mysql
 fi


yum -y install ncurses-compat-libs ncurses-devel openssl-devel openssl cmake mariadb-devel 


if [ ! -d $dir_a/$app_b ];then
        tar xf bag/$app_a -C $dir_a
fi

if [ ! -d $dir_a/mysql ];then
        ln -sv $dir_a/$app_b $dir_a/mysql
fi
chown -R mysql:mysql $dir_a/mysql*

echo "export PATH=$dir_a/mysql/bin:\$PATH" > /etc/profile.d/mysql.sh

source /etc/profile.d/mysql.sh

if [ ! -d /$dir_b ];then
        mkdir -p /$dir_b 
        chown -R mysql.mysql /$dir_b
fi


content=$(ls $dir_b | wc -l)
if [ $content -eq 0 ];then
        mysqld --initialize-insecure --user mysql --datadir $dir_b
fi

cat > /etc/my.cnf <<EOF
[mysqld]
basedir = $dir_a/mysql    
datadir = $dir_b           
socket = /tmp/mysql.sock      
port = 3306                   
pid-file = $dir_b/mysql.pid
user = mysql                  
skip-name-resolve
EOF



sed -ri "s#^(basedir=).*#\1$dir_a/mysql#g" $dir_a/mysql/support-files/mysql.server
sed -ri "s#^(datadir=).*#\1$dir_b#g" $dir_a/mysql/support-files/mysql.server


cat > /usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=mysql server daemon
After=network.target 

[Service]
Type=forking

ExecStart=$dir_a/mysql/support-files/mysql.server start
ExecStop=$dir_a/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now mysqld

Deploy PHP

 https://www.php.net/distributions/php-8.0.10.tar.xz

Unzip

[root@server2 ~]# tar -xf php-8.0.10.tar.gz -C /usr/local/

Install dependency packages

[root@server2 ~]# wget http://mirrors.aliyun.com/repo/epel-7.repo
[root@server1 ~]# 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
[root@server2 ~]# yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

Compile and install

[root@server2 ~]# cd /usr/local/php-8.0.10/
[root@server2 php-8.0.10]# ./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
......
......
......
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+

Thank you for using PHP.


[root@server2 php-8.0.10]# make
.......
.......
.......
invertedregexiterator.inc
pharcommand.inc
phar.inc

Build complete.
Don't forget to run 'make test'.

[root@server2 php-8.0.10]# make install
......
......
/root/php-8.0.10/build/shtool install -c ext/phar/phar.phar /usr/local/php8/bin/phar.phar
ln -s -f phar.phar /usr/local/php8/bin/phar
Installing PDO headers: /usr/local/php8/include/php/ext/pdo/

Configure php-fpm

[root@server2 php-8.0.10]# cp /etc/php.ini /opt/
[root@server2 php-8.0.10]# cp php.ini-production /etc/php.ini 
cp: overwrite '/etc/php.ini'? y
[root@server2 php-8.0.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@server2 php-8.0.10]# chmod +x /etc/init.d/php-fpm
[root@server2 php-8.0.10]# cd ..
[root@server2 local]# cd php8/
[root@server2 php8]# cd etc/
[root@server2 etc]# cp php-fpm.conf.default php-fpm.conf
[root@server2 etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf
[root@server2 etc]# cd php-fpm.d
[root@server2 php-fpm.d]# ls
www.conf www.conf.default

Configuring environment variables

[root@server2 ~]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@server2 ~]# cat /etc/profile.d/php.sh
export PATH=/usr/local/php8/bin:$PATH
[root@server2 ~]# source /etc/profile.d/php.sh
[root@server2 ~]# which php
/usr/local/php8/bin/php

Writing service files

[root@server2 ~]# cat /usr/lib/systemd/system/php-fpm.service 
[Unit]
Description=php-fpm 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]
[root@server2 ]# systemctl daemon-reload

Start PHP

[root@server2 ~]# systemctl start php-fpm
[root@server2 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port                 
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*                    
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*                    
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*                    
LISTEN 0 80 *:3306 *:*                    
LISTEN 0 128 [::]:22 [::]:*

Configure virtual hosts in nginx.conf

[root@server2 ~]# cd /usr/local/nginx/html/
[root@server2 html]# ls
50x.html index.html
[root@server2 html]# vim index.php
[root@server2 html]# cat index.php 
<?php
    phpinfo();
?>
[root@server2 conf]# pwd
/usr/local/nginx/conf
[root@server2 conf]# vim nginx.conf
........
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;

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



        location ~ \.php$ {
            root html;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
            include fastcgi_params;
        }
[root@server2 conf]# nginx -s reload

access

insert image description here

Deploy httpd on node3

[root@node3 ~]# yum -y install httpd

start up

[root@node3 ~]# systemctl start httpd
[root@node3 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port              
LISTEN 0 128 *:22 *:*                  
LISTEN 0 100 127.0.0.1:25 *:*                  
LISTEN 0 128 :::80 :::*                  
LISTEN 0 128 :::22 :::*                  
LISTEN 0 100 ::1:25 :::*

access

insert image description here

Implementing Separate Deployment

Configure on nginx.conf on server1

[root@server1 ~]# cat /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 cm { #static resource address server 192.168.244.142;
    }
    
    upstream nm { #dynamic resource address server 192.168.244.133;
    }


    server {
        listen 80;
        server_name localhost;

        #charset koi8-r;

        #access_log logs/host.access.log main;

        location / {
            proxy_pass http://cm; #point to static}
        
        #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$ { #point to dynamic proxy_pass http://nm;
        }

[root@server1 ~]# nginx -s reload

Visit 192.168.244.131

insert image description here

When accessing 192.168.244.131/index.php

insert image description here

This is the end of this article about the example of how to achieve dynamic and static separation in nginx. For more relevant content about dynamic and static separation in nginx, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Basic configuration example of Nginx with Apache or Tomcat for dynamic and static separation
  • Using Nginx+uWsgi to separate the dynamic and static parts of Python's Django framework site
  • Simple implementation of nginx+tomcat reverse proxy and dynamic and static separation
  • Detailed explanation of nginx to separate static and dynamic tomcat
  • nginx realizes load balancing and dynamic and static separation
  • Nginx sample code for implementing dynamic and static separation
  • Detailed example of deploying Nginx+Apache dynamic and static separation
  • Sample code for nginx to achieve dynamic and static separation
  • Nginx implements dynamic and static separation example explanation
  • Nginx dynamic and static separation implementation case code analysis
  • Detailed explanation of the process of realizing dynamic and static separation in Springmvc nginx
  • Docker Nginx container and Tomcat container to achieve load balancing and dynamic and static separation operations
  • Analysis of the principle of Nginx+Tomcat to achieve load balancing and dynamic and static separation
  • The principle and configuration of Nginx load balancing and dynamic and static separation
  • Detailed instructions for nginx from installation to configuration (installation, security configuration, anti-hotlinking, dynamic and static separation, HTTPS configuration, performance optimization)
  • Implementation of Nginx+Tomcat load balancing and dynamic and static separation cluster
  • Server load balancing nginx+tomcat to achieve dynamic and static separation
  • Nginx dynamic and static separation configuration implementation and description

<<:  Solution to the problem that directly setting the width and height of a hyperlink does not work

>>:  js realizes a gradually increasing digital animation

Blog    

Recommend

Detailed explanation of the difference between alt and title

These two attributes are often used, but their di...

Examples of using html unordered list tags and ordered list tags

1. Upper and lower list tags: <dl>..</dl...

Table td picture horizontally and vertically centered code

Html code: Copy code The code is as follows: <t...

MySQL 5.6.22 installation and configuration method graphic tutorial

This tutorial shares the specific code of MySQL5....

Get the calculated style in the CSS element (after cascading/final style)

To obtain the calculated style in a CSS element (t...

MySQL Basic Tutorial Part 1 MySQL5.7.18 Installation and Connection Tutorial

Starting from this article, a new series of artic...

WeChat applet implements search function and jumps to search results page

Search Page: search.wxml page: <view class=&qu...

Example of using swiper plugin to implement carousel in Vue

Table of contents vue - Use swiper plugin to impl...

js to realize a simple advertising window

This article shares the specific code of js to im...

Webservice remote debugging and timeout operation principle analysis

WebService Remote Debugging In .NET, the remote d...

Detailed explanation of Vue life cycle

Table of contents Why understand the life cycle W...

Mysql: The user specified as a definer ('xxx@'%') does not exist solution

During the project optimization today, MySQL had ...