Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Sample code for nginx to achieve static and dynamic separation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

First, the static and dynamic separation of simple configuration nginx

Suppose web1 is a static server, web2 is a dynamic server, and node2 is a proxy

1.1 separate according to catalog

Web1 only handles static requests

[root@web1 ~] # mkdir-p / var/www/www/image [root@web1 ~] # yum-y install lrzsz [root@web1 ~] # cd / var/www/www/image/ [root@web1 image] # rz [root@web1 image] # ll-rw-r--r--. 1 root root 156848 Mar 13 11:31 nhrzyx.png [root@web2 ~] # vim / etc/httpd/conf/httpd.conf DocumentRoot "/ var/www/www" [root@web2 ~] # systemctl restart httpd

Web2 only handles dynamic requests

[root@web2 ~] # mkdir-p / var/www/www/dynamic [root@web2 ~] # echo dynamic10 > / var/www/www/dynamic/index.html [root@web2 ~] # vim / etc/httpd/conf/httpd.conf DocumentRoot "/ var/www/www" [root@web2 ~] # systemctl restart httpd

Access test

Http://172.25.254.134/image/nhrzyx.png

Http://172.25.254.135/dynamic/index.html

1.2 detach by request

Configure the agent

[root@lb01 conf] # vim nginx.confworker_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 weight=5; 80 weight=5;} upstream dynamic_pools {server 172.25.254.135 weight=5; 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

Configure hosts, browser access testing

172.25.254.131 www.lbtest.com

Http://www.lbtest.com/image/nhrzyx.png

Http://www.lbtest.com/dynamic/

1.3 separate by extension

[root@lb01 conf] # vim nginx.confworker_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 weight=5; 80 weight=5;} upstream dynamic_pools {server 172.25.254.135 weight=5; 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

Http://www.lbtest.com/image/nhrzyx.png

1.4 Separation based on client identity

Http {include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65upstream stack_pools {server 172.25.254.134 stack_pools 80 weight=5;} upstream dynamic_pools {server 172.25.254.135 stack_pools 80 weight=5;} server {listen 80; server_name www.lbtest.com Location / {if ($http_user_agent ~ * "MSIE") {proxy_pass http://dynamic_pools;} if ($http_user_agent ~ * "firefox") {proxy_pass http://stack_pools;}} proxy_set_header Host $host }} [root@web1 image] # echo stack_web > > / var/www/www/test.html [root@web1 image] # systemctl restart httpd [root@web2 ~] # echo dynamic_web > > / var/www/www/test.html [root@web2 ~] # systemctl restart httpd

Visit using IE and Firefox respectively

Http://www.lbtest.com/test.html

1.5 using client-side pc and mobile separation

Http {include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65upstream stack_pools {server 172.25.254.134 stack_pools 80 weight=5;} upstream dynamic_pools {server 172.25.254.135 stack_pools 80 weight=5;} server {listen 80; server_name www.lbtest.com Location / {if ($http_user_agent ~ * "iphone") {proxy_pass http://dynamic_pools;} if ($http_user_agent ~ * "android") {proxy_pass http://stack_pools;}} proxy_set_header Host $host;}

Access tests using Android and iphone respectively

Http://www.lbtest.com/test.html

II. Optimization

[root@node2 ~] # vim / etc/sysctl.conf

Net.ipv4.tcp_fin_timeout = 2net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_syncookies = 1net.ipv4.tcp_keepalive_time = 600net.ipv4.ip_local_port_range = 4000 65000net.ipv4.tcp_max_syn_backlog = 16348net.ipv4.tcp_max_tw_buckets = 36000net.ipv4.tcp_syn_retries = 1net.ipv4.tcp_synack_retries = 1net.ipv4.tcp_max_orphans = 16384net.core.somaxconn = 16384net.core.netdev_max_backlog = 18364

[root@node2] # sysctl-p

A brief introduction to keepalive and nginx

Configure keepalived to achieve high availability of nginx load balancing

Keepalive is more suitable for seeing IP drift, and heartbeat is more suitable if resource services are controlled, such as high availability of storage direction.

III. Health check of nginx reverse agent

When nginx acts as a reverse proxy, when the back-end server goes down, ngixn cannot remove the realserver from upstream, so it will forward the request to the backend realserve. Although nginx can enable proxy_next_upstream in localtion to solve the error page returned to the customer, this will also forward the request to this server and then forward it to another server, thus wasting a forwarding. Check the health status of the rear realserver with the nginx module nginx_upstream_check_module developed by the Taobao team. If the back-end server is not available, all requests will not be forwarded to this server.

Check interval=5000 rise=1 fall=3 timeout=4000

3.1 add directly to the profile

[root@node2 ~] # vim / usr/local/nginx/conf/nginx.conf

Worker_processes 1 weight=5 backup;check interval=5000 rise=1 fall=3 timeout=4000; events {worker_connections 1024;} http {listen 65 server upstream web_pools {server 172.25.254.134worker_processes 172.25.254.134worker_processes 172.25.254.135worker_processes 80 weight=5 backup;check interval=5000 rise=1 fall=3 timeout=4000;} server {listen 80; server_name www.lbtest.com Location / {# root html; # index index.html index.htm; proxy_set_header Host $host; proxy_pass http://web_pools;}

[root@node2 ~] # nginx-t

Nginx: [emerg] unknown directive "check" in / usr/local/nginx/conf/nginx.conf:14nginx: configuration file / usr/local/nginx/conf/nginx.conf test failed

Check failed

Download the module https://github.com/yaoweibin/nginx_upstream_check_module of nginx and use nginx to type the installation of the module as an upgrade to nginx

3.2 download module

[root@node2 nginx-1.12.2] # yum-y install git [root@node2 nginx-1.12.2] # git clone https://github.com/yaoweibin/nginx_upstream_check_module.git[root@node2 nginx-1.12.2] # lldrwxr-xr-x. 7 root root 4096 Apr 13 00:57 nginx_upstream_check_ module [root @ node2 nginx-1.12.2] # cd nginx_upstream_check_module/ [root @ node2 nginx_upstream_check_module] # ll-rw-r--r--. 1 root root 0 Apr 13 00:57 CHANGES-rw-r--r--. 1 root root 7921 Apr 13 00:57 check_1.11.1+.patch-rw-r--r--. 1 root root 8330 Apr 13 00:57 check_1.11.5+.patch-rw-r--r--. 1 root root 8060 Apr 13 00:57 check_1.12.1+.patch-rw-r--r--. 1 root root 8054 Apr 13 00:57 check_1.14.0+.patch-rw-r--r--. 1 root root 5483 Apr 13 00:57 check_1.2.1.patch-rw-r--r--. 1 root root 7130 Apr 13 00:57 check_1.2.2+.patch-rw-r--r--. 1 root root 7094 Apr 13 00:57 check_1.2.6+.patch-rw-r--r--. 1 root root 6791 Apr 13 00:57 check_1.5.12+.patch-rw-r--r--. 1 root root 8295 Apr 13 00:57 check_1.7.2+.patch-rw-r--r--. 1 root root 8346 Apr 13 00:57 check_1.7.5+.patch-rw-r--r--. 1 root root 8509 Apr 13 00:57 check_1.9.2+.patch-rw-r--r--. 1 root root 6943 Apr 13 00:57 check.patch-rw-r--r--. 1 root root 749 Apr 13 00:57 configdrwxr-xr-x. 2 root root 43 Apr 13 00:57 doc-rw-r--r--. 1 root root 1709 Apr 13 00:57 nginx-sticky-module.patchdrwxr-xr-x. 2 root root 29 Apr 13 00:57 nginx-tests-rw-r--r--. 1 root root 112010 Apr 13 00:57 ngx_http_upstream_check_module.c-rw-r--r--. 1 root root 529 Apr 13 00:57 ngx_http_upstream_check_module.h-rw-r--r--. 1 root root 2848 Apr 13 00:57 ngx_http_upstream_jvm_route_module.patch-rw-r--r--. 1 root root 11509 Apr 13 00:57 READMEdrwxr-xr-x. 6 root root 79 Apr 13 00:57 test-rw-r--r--. 1 root root 3342 Apr 13 00:57 upstream_fair.patchdrwxr-xr-x. 2 root root 81 Apr 13 00:57 util

3.3 Patch

[root@node2 nginx_upstream_check_module] # cd.. / [root@node2 nginx-1.12.2] # yum-y install patch

Note: due to the update of nginx version, the patch is check_1.11.5+.patch for nginx version 1.12 or above.

[root@node2 nginx-1.12.2] # patch-p0

< ./nginx_upstream_check_module/check_1.11.5+.patchpatching file src/http/modules/ngx_http_upstream_hash_module.cpatching file src/http/modules/ngx_http_upstream_ip_hash_module.cpatching file src/http/modules/ngx_http_upstream_least_conn_module.cpatching file src/http/ngx_http_upstream_round_robin.cpatching file src/http/ngx_http_upstream_round_robin.h 3.4 编译安装 [root@node2 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx \>

-- user=nginx-- group=nginx\ >-- with-http_ssl_module\ >-- with-http_realip_module\ >-- with-http_addition_module\ >-- with-http_gzip_static_module\ >-- with-http_stub_status_module\ >-- with-http_sub_module\ >-- with-pcre\ >-- add-module=./nginx_upstream_check_module

A new directory appears as objs

[root@node2 nginx-1.12.2] # ll

Drwxr-xr-x. 4 root root 187 Apr 13 01:04 objs

[root@node2 nginx-1.12.2] # make

Sed-e "s |% PREFIX%% | / usr/local/nginx |"\-e "s |% PID_PATH%% | / usr/local/nginx/logs/nginx.pid |"\-e "s |% CONF_PATH%% | / usr/local/nginx/conf/nginx.conf |"\-e "s |% ERROR_LOG_PATH%% | / usr/local/nginx/logs/error.log |"\ "

< man/nginx.8 >

Objs/nginx.8make [1]: Leaving directory `/ usr/local/src/nginx-1.12.2'

[root@node2 nginx-1.12.2] # ll objs/

Drwxr-xr-x. 3 root root 41 Apr 13 01:04 addon-rw-r--r--. 1 root root 16895 Apr 13 01:04 autoconf.err-rw-r--r--. 1 root root 42396 Apr 13 01:04 Makefile-rwxr-xr-x. 1 root root 5993600 Apr 13 01:06 nginx # nginx New executable-rw-r--r--. 1 root root 5341 Apr 13 01:06 nginx.8-rw-r--r--. 1 root root 7202 Apr 13 01:04 ngx_auto_config.h-rw-r--r--. 1 root root 657 Apr 13 01:03 ngx_auto_headers.h-rw-r--r--. 1 root root 6412 Apr 13 01:04 ngx_modules.c-rw-r--r--. 1 root root 87120 Apr 13 01:06 ngx_modules.odrwxr-xr-x. 9 root root 91 Apr 3 22:38 src

Back up the ngxin and copy the new nginx

[root@node2 nginx-1.12.2] # cd / usr/local/nginx/sbin/ [root@node2 sbin] # mv nginx nginx.bak [root@node2 sbin] # cp / usr/local/src/nginx-1.12.2/objs/nginx / usr/local/nginx/sbin [root@node2 sbin] # / usr/local/nginx/sbin/nginx-s stop

Nginx: [emerg] getpwnam ("nginx") failed

[root@node2 sbin] # id nginx

Id: nginx: no such user

[root@node2 sbin] # useradd nginx [root@node2 sbin] # id nginx

Uid=1002 (nginx) gid=1002 (nginx) groups=1002 (nginx)

[root@node2 sbin] # / usr/local/nginx/sbin/nginx-s stop [root@node2 sbin] # / usr/local/nginx/sbin/nginx [root@node2 ~] # nginx-t

Nginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is ok

Nginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful

Successful installation

3.5 configuration

Worker_processes 1 weight=5 backup;check interval=5000 rise=1 fall=3 timeout=4000; events {worker_connections 1024;} http {listen 65 server upstream web_pools {server 172.25.254.134worker_processes 172.25.254.134worker_processes 172.25.254.135worker_processes 80 weight=5 backup;check interval=5000 rise=1 fall=3 timeout=4000;} server {listen 80; server_name www.lbtest.com Location / {proxy_set_header Host $host; proxy_pass http://web_pools;} location / nstatus {check_status; access_log off;}} [root@node2 conf] # nginx-s reload

3.6 access Test

Two states are normal.

Turn off one.

[root@web1 image] # systemctl stop httpd

Turn off two.

[root@web2 ~] # systemctl stop httpd

Open a

[root@web1 image] # systemctl start httpd

[root@web2 ~] # systemctl restart httpd

Successfully implement the back-end check function

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report