主頁(yè) > 知識(shí)庫(kù) > nginx實(shí)現(xiàn)動(dòng)靜分離的示例代碼

nginx實(shí)現(xiàn)動(dòng)靜分離的示例代碼

熱門標(biāo)簽:網(wǎng)絡(luò)電話外呼系統(tǒng)開(kāi)發(fā) 北海智能電銷機(jī)器人公司 福州語(yǔ)音電銷機(jī)器人招商 申請(qǐng)400電話收費(fèi)標(biāo) 艾澤拉斯地圖標(biāo)注 菏澤自動(dòng)電銷機(jī)器人公司 淘寶辦理400電話是多少 外呼線路隨意切換嗎 滴滴急救中心地圖標(biāo)注

一、簡(jiǎn)單配置nginx的動(dòng)靜分離

假設(shè)web1為靜態(tài)服務(wù)器,web2為動(dòng)態(tài)服務(wù)器,node2做代理

1.1 根據(jù)目錄分開(kāi)

web1只處理靜態(tài)請(qǐng)求

[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只處理動(dòng)態(tài)請(qǐng)求

[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

訪問(wèn)測(cè)試

http://172.25.254.134/image/nhrzyx.png

http://172.25.254.135/dynamic/index.html

1.2 通過(guò)請(qǐng)求分離

配置代理

[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

配置hosts ,瀏覽器訪問(wèn)測(cè)試

172.25.254.131 www.lbtest.com

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

http://www.lbtest.com/dynamic/

1.3 根據(jù)擴(kuò)展名分離

[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

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

1.4 根據(jù)客戶端標(biāo)識(shí)進(jìn)行分離

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 / {
        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

分別使用IE和火狐瀏覽器訪問(wèn)

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

1.5 使用客戶端的pc和移動(dòng)分離

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 / {
        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;
    }
}

分別使用安卓和iphone訪問(wèn)測(cè)試

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

二 、優(yōu)化

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

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

[root@node2 ~]# sysctl -p

簡(jiǎn)單介紹keepalive和nginx

配置keepalived實(shí)現(xiàn)nginx負(fù)載均衡的高可用

keepalive更適合于見(jiàn)得IP漂移,如果資源服務(wù)有控制,heartbeat更適合,比如存儲(chǔ)方向的高可用

三、 nginx反向代理的健康檢查

nginx做反向代理的時(shí)候,當(dāng)后端就的服務(wù)器出現(xiàn)宕機(jī)的時(shí)候,ngixn不能把這臺(tái)realserver剔除upstream的,所以還會(huì)把請(qǐng)求轉(zhuǎn)發(fā)到后端的這臺(tái)realserve上,雖然nginx可以在localtion中啟用proxy_next_upstream來(lái)解決返回給客戶的錯(cuò)誤頁(yè)面,但這個(gè)還會(huì)會(huì)把請(qǐng)求轉(zhuǎn)發(fā)轉(zhuǎn)給這臺(tái)服務(wù)器,然后再轉(zhuǎn)發(fā)別的服務(wù)器,這樣就浪費(fèi)了一次轉(zhuǎn)發(fā),借助淘寶團(tuán)隊(duì)開(kāi)發(fā)的nginx模塊nginx_upstream_check_module來(lái)檢測(cè)后方的realserver的健康狀態(tài),如果后端服務(wù)器不可用,則所有的請(qǐng)求不轉(zhuǎn)發(fā)到這臺(tái)服務(wù)器

check interval=5000 rise=1 fall=3 timeout=4000;

3.1 直接添加到配置文件

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

worker_processes 1;
events {
  worker_connections 1024;
}
http {
  include    mime.types;
  default_type application/octet-stream;
  sendfile    on;
  keepalive_timeout 65;
upstream web_pools {
    server 172.25.254.134:80 weight=5;
    server 172.25.254.135:80 weight=5;
#    server 172.25.254.158: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:14
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed 

檢查失敗

下載nginx的模塊https://github.com/yaoweibin/nginx_upstream_check_module,使用nginx打上模塊的安裝,當(dāng)做nginx的升級(jí)

3.2 下載模塊

[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]# ll
drwxr-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 config
drwxr-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.patch
drwxr-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 README
drwxr-xr-x. 6 root root   79 Apr 13 00:57 test
-rw-r--r--. 1 root root  3342 Apr 13 00:57 upstream_fair.patch
drwxr-xr-x. 2 root root   81 Apr 13 00:57 util

3.3 打補(bǔ)丁

[root@node2 nginx_upstream_check_module]# cd ../

[root@node2 nginx-1.12.2]# yum -y install patch

注:因nginx版本更新,1.12以上版本的nginx,補(bǔ)丁為check_1.11.5+.patch

[root@node2 nginx-1.12.2]# patch -p0 < ./nginx_upstream_check_module/check_1.11.5+.patch

patching file src/http/modules/ngx_http_upstream_hash_module.c
patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
patching file src/http/modules/ngx_http_upstream_least_conn_module.c
patching file src/http/ngx_http_upstream_round_robin.c
patching 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

出現(xiàn)一個(gè)新的目錄 為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.8
make[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新的可執(zhí)行文件
-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.o
drwxr-xr-x. 9 root root   91 Apr 3 22:38 src

備份就得ngxin,并拷貝新的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

成功安裝

3.5 配置

worker_processes 1;
events {
  worker_connections 1024;
}
http {
  include    mime.types;
  default_type application/octet-stream;
  sendfile    on;
  keepalive_timeout 65;
upstream web_pools {
    server 172.25.254.134:80 weight=5;
    server 172.25.254.135:80 weight=5;
#    server 172.25.254.158: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 訪問(wèn)測(cè)試

兩個(gè)狀態(tài)正常

關(guān)掉一個(gè)

[root@web1 image]# systemctl stop httpd

關(guān)掉兩個(gè)

[root@web2 ~]# systemctl stop httpd

開(kāi)啟一個(gè)

[root@web1 image]# systemctl start httpd

[root@web2 ~]# systemctl restart httpd

成功實(shí)現(xiàn)后端檢查功能

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

標(biāo)簽:三沙 延安 賀州 亳州 混顯 資陽(yáng) 滄州 丹東

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《nginx實(shí)現(xiàn)動(dòng)靜分離的示例代碼》,本文關(guān)鍵詞  nginx,實(shí)現(xiàn),動(dòng)靜,分離,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《nginx實(shí)現(xiàn)動(dòng)靜分離的示例代碼》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于nginx實(shí)現(xiàn)動(dòng)靜分離的示例代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章