nginx implementation of static and static separation example

  • 2020-05-24 06:50:30
  • OfStack

In order to speed up the website's parsing speed, dynamic page and static page can be resolved by different servers to speed up the parsing speed. Reduce the pressure on individual servers. In simple terms, you use a regular expression matching filter and hand it over to a different server.

1. Prepare the environment

Prepare one nginx agent and two http agents to handle dynamic and static, respectively.

1. The nginx configured for compilation and installation is the reverse agent 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 allocation --10.0.105.196


server {
listen 80;
server_name localhost;

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

To upload pictures

Dynamic resource allocation: 10.0.105.200

yum installation 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 nginx profile:

[

[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 site directory

fastcgi_pass fastcgi_index fastcgi_param

The # site root directory, depending on the root configuration item

include

}

}

]

127.0.0.1:9000; Specify the access address

index.php;

# specifies the default file

SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_params; # contains the nginx constant definition

When accessing a static page, location matches (html|, jpg|, png|, js|, css) by forwarding to the static server, and the static service passes

Regular matching of location to process requests.

When the dynamic page is accessed, location files matching the end of.\php are forwarded to the back-end php service to process the request.

Knowledge 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

Related articles: