Nginx implements dynamic and static separation example explanation

Nginx implements dynamic and static separation example explanation

In order to speed up the parsing of the website, dynamic pages and static pages can be parsed by different servers to speed up the parsing speed. Reduce the pressure on the original single server. To put it simply, it uses regular expressions to match and filter, and then sends them to different servers.

1. Prepare the environment

Prepare an nginx proxy for two http to handle dynamic and static respectively.

1. Configure the compiled and installed nginx as a reverse proxy upstream;

upstream static {
server 10.0.105.196:80 weight=1 max_fails=1 fail_timeout=60s;
}
upstream php {
server 10.0.105.200:80 weight=1 max_fails=1 fail_timeout=60s;
}

server {
listen server_name
#Dynamic resource loading 80;
localhost
location ~ \.(php|jsp)$ { proxy_pass http://phpserver;

proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

#Static resource loading location ~ \.(html|jpg|png|css|js)$ { proxy_pass http://static; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

Static resource configuration---10.0.105.196

server {
listen 80;
server_name localhost;

location ~ \.(html|jpg|png|js|css)$ { root /var/www/nginx;
}
}

Upload pictures

Dynamic resource configuration: 10.0.105.200

yum install php7.1

[root@nginx-server ~]#rpm -Uvh https://mirror.webtatic.com/yum/el7/epel- release.rpm

[root@nginx-server ~]#rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic- release.rpm

[root@nginx-server ~]#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

[root@nginx-server ~]#yum install -y php71w-fpm [root@nginx-server ~]#systemctl start php-fpm [root@nginx-server ~]#systemctl enable php-fpm

Edit the nginx configuration file:

[root@nginx-server ~]# cd /etc/nginx/conf.d/ [root@nginx-server conf.d]# vim phpserver.conf server {

listen 80;

server_name localhost; location ~ \.php$ {

root /home/nginx/html; #Specify the website directory

fastcgi_pass fastcgi_index fastcgi_param

#Site root directory, depends on the root configuration item

include

}

}

127.0.0.1:9000; #Specify the access address

index.php;

#Specify the default file

SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_params; #Contains nginx constant definitions

When accessing a static page, the location matches (html|jpg|png|js|css) and is forwarded to the static server. The static service is

The regular match of location is used to process the request.

When accessing a dynamic page, the location matches the file ending with .\php and forwards it to the backend PHP service to process the request.

Knowledge point expansion:

Detach by request

[root@lb01 conf]# vim nginx.conf
worker_processes 1;
events {
  worker_connections 1024;
}
http {
  include mime.types;
  default_type application/octet-stream;
  sendfile on;
  keepalive_timeout 65;
upstream stack_pools {
    server 172.25.254.134:80 weight=5;
}
upstream dynamic_pools {
    server 172.25.254.135:80 weight=5;
}
  server {
    listen 80;
    server_name www.lbtest.com;
    location / {
      root html;
      index index.html index.htm;
      proxy_set_header Host $host;
      proxy_pass http://dynamic_pools;
    }
    location /image/ {
      proxy_set_header Host $host;
    proxy_pass http://stack_pools;
    }
    location /dynamic/ {
      proxy_set_header Host $host;
    proxy_pass http://dynamic_pools;
    }
  }
}
[root@lb01 conf]# nginx -s reload

Separate by extension

[root@lb01 conf]# vim nginx.conf

 worker_processes 1;
events {
  worker_connections 1024;
}
http {
  include mime.types;
  default_type application/octet-stream;
  sendfile on;
  keepalive_timeout 65;
upstream stack_pools {
    server 172.25.254.134:80 weight=5;
}
upstream dynamic_pools {
    server 172.25.254.135:80 weight=5;
}
  server {
    listen 80;
    server_name www.lbtest.com;
    location / {
      root html;
      index index.html index.htm;
      proxy_set_header Host $host;
      proxy_pass http://dynamic_pools;
    }
    location ~ .*.(jpg|png|gif|css|js|swf|bmp|jsp|php|asp)$ {
    proxy_set_header Host $host;
    proxy_pass http://stack_pools;
    }
  }
}
[root@lb01 conf]# nginx -s reload

This is the end of this article about the example of implementing dynamic and static separation with nginx. For more relevant content about implementing dynamic and static separation with 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 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
  • Example of how nginx implements 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

<<:  JS implements the rock-paper-scissors game

>>:  MySQL 8.0.18 installation tutorial under Windows (illustration)

Recommend

Detailed Tutorial on Using xargs Command on Linux

Hello everyone, I am Liang Xu. When using Linux, ...

How to try to add sticky effect to your CSS

Written in front I don’t know who first discovere...

css3 animation ball rolling js control animation pause

CSS3 can create animations, which can replace man...

Example of using mycat to implement MySQL database read-write separation

What is MyCAT A completely open source large data...

How to update Ubuntu 20.04 LTS on Windows 10

April 23, 2020, Today, Ubuntu 20.04 on Windows al...

MYSQL Operator Summary

Table of contents 1. Arithmetic operators 2. Comp...

HTML uses canvas to implement bullet screen function

Introduction Recently, I needed to make a barrage...

A complete list of commonly used Linux commands (recommended collection)

Table of contents 1. System Information 2. Shutdo...

Use of environment variables in Docker and solutions to common problems

Preface Docker can configure environment variable...

Several navigation directions that will be popular in the future

<br />This is not only an era of information...

Analysis of the principles of docker containers

Table of contents 01 What is the essence of a con...