nginx實現反向代理、負載均衡-技術流ken
1.簡介
本篇博文是《 ofollow,noindex" target="_blank">nginx實現動態/靜態檔案快取-技術流ken 》的二部曲。將詳細介紹nginx如何實現反向代理以及負載均衡技術,並輔以實戰案例。
反向代理--“反向代理(Reverse Proxy)方式是指以 代理伺服器 來接受internet上的連線請求,然後將請求轉發給內部網路上的伺服器,並將從伺服器上得到的結果返回給internet上請求連線的 客戶端 ,此時代理伺服器對外就表現為一個反向代理伺服器。”
負載均衡--“網路專用術語,負載均衡建立在現有網路結構之上,它提供了一種 廉價 有效 透明 的方法擴充套件網路裝置和伺服器的頻寬、增加吞吐量、加強網路資料處理能力、提高網路的 靈活性 和可用性。”
2.nginx實現反向代理
1.幾個概念
反向代理:在收到客戶端請求之後,會修目標 IP 地址和埠
正向代理:在收到客戶端請求之後,會修源 IP 地址和埠
上游伺服器:代理伺服器後端的哪些真正給客戶端提供服務的節點,這樣的伺服器稱之為上游伺服器
下游伺服器:客戶端就是下游節點
2.反向代理指令
模組:nginx_http_proxy_module 指令 proxy_pass:指定上游伺服器的ip和埠 proxy_set_header:指定在重新封裝請求報文的時候,新增一個新的首部 Syntax:proxy_pass URL; Default:— Context:location, if in location, limit_except 例子:proxy_pass http://10.220.5.200:80; Syntax:proxy_set_header field value; Default:proxy_set_header Host $proxy_host; Context:http, server, location
3.反向代理簡單示例
location / { proxy_pass http://10.220.5.180; proxy_set_header X-Real-IP $remote_addr proxy_set_header Host $proxy_host; }
4.反向代理實戰案例
1.環境準備
centos7.5
反向代理伺服器IP:172.20.10.7/28
web1伺服器IP:172.20.10.8/28
web2伺服器IP:172.20.10.9/28
2.配置反向代理伺服器端
yum安裝nignx需要配置網路源,複製下面的程式碼到你的yum倉庫中
[ken] name=ken enabled=1 gpgcheck=0 baseurl=https://mirrors.aliyun.com/epel/7Server/x86_64/
安裝nginx
[root@ken ~]# yum install nginx -y
配置nginx檔案,我們實現這樣一個效果,靜態檔案都被代理到172.20.10.8,動態檔案都被排程到172.20.10.9,實現動靜分離。
[root@ken ~]# vim /etc/nginx/nginx.conf # For more information on configuration, see: #* Official English Documentation: http://nginx.org/en/docs/ #* Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_formatmain'$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log/var/log/nginx/access.logmain; sendfileon; tcp_nopushon; tcp_nodelayon; keepalive_timeout65; types_hash_max_size 2048; include/etc/nginx/mime.types; default_typeapplication/octet-stream; # include /etc/nginx/conf.d/*.conf; server { listen80 default_server; listen[::]:80 default_server; server_name_; root/var/www/html; index index.html index.php; # Load configuration files for the default server block. location / { proxy_pass http://172.20.10.8; proxy_set_header host $proxy_host; proxy_set_header realip $remote_addr; } location ~^/.*(\.php)$ { proxy_pass http://172.20.10.9; proxy_set_header host $proxy_host; proxy_set_header realip $remote_addr; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } }
進行語法檢測
[root@ken ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
檢查沒有問題之後進行重啟
[root@ken ~]# systemctl start nginx
3.配置web伺服器端
安裝apache
[root@ken ~]# yum install httpd -y
準備測試檔案,172.20.10.8準備靜態檔案
[root@ken ~]# echo "this is 172.20.10.8 for static test">/var/www/html/index.html
172.20.10.9需要下載php以便支援動態檔案
[root@ken html]# yum install php -y
172.20.10.9準備動態檔案,
[root@ken ~]# cd /var/www/html/ [root@ken html]# vim index.php <?php phpinfo(); ?>
4.web伺服器重啟
[root@ken html]# systemctl restart httpd
5.關閉安全服務
[root@ken ~]# iptables -F
6.瀏覽器測試
請求靜態檔案測試
靜態檔案請求已經成功轉發至172.20.10.8。
測試成功!
請求動態檔案測試
動態檔案請求已經成功轉發至172.20.10.9.
測試成功!
7.補充
補充一
補充1: location如下 location /admin { proxy_pass http://www.ken.com/; proxy_pass http://www.ken.com; } 請求的url 是http://www.ken.com/admin/a.html 如果代理方式是 proxy_pass http://www.ken.com/; 那麼去www.ken.com的跟目錄下找a.html,/代表完全代理。 如果代理方式是 proxy_pass http://www.ken.com; 那麼去www.ken.com的跟目錄下的admin找a.html
補充二
補充2: 如果location中使用了模式匹配(正則),那麼,location中的url會直接補充到代理節點的後面. 此時,上游伺服器的的後面不能有任何內容,包括 / location ~ \.php$ { proxy_pass http://www.ken.com; [正則表示式proxy_pass轉發的地址後面什麼都不能加]<<< 正確寫法 proxy_pass http://www.ken.com:80;<<< 正確寫法 proxy_pass http://www.ken.com/;<<< 錯誤寫法 proxy_pass http://www.ken.com/img;<<< 錯誤寫法 } 此時,如果請求的url是 http://www.baidu.com/book/stu/a.php ,就會代理成 http://www.ken.com/book/stu/a.php
補充三
補充3: 在location中如果有重定向的話,那麼就用重定向後的uri替換掉代理節點中的uri location / { rewrite /(.*)$ /index.php?name=$1 break; proxy_pass http://www.baidu.com:80/img; } 此時,如果請求的url是 http://www.ken.com/bajie ,就會代理成 www.baidu.com/index.php?name=bajie
3.nginx實現負載均衡
1.幾個概念
排程器:分發使用者的請求到一個後端節點
上游伺服器 ( 真實伺服器 ) :每個真正用來處理使用者請求的節點都是一個上游伺服器
CIP :客戶端的 IP 地址
RIP :真實伺服器的 IP 地址
VIP :虛擬 IP ,使用者所看到的是也是虛擬 IP
2.指令
指令:upstream 作用:定義一個上游伺服器組 格式 upstream name { server上游伺服器1引數 引數; server上游伺服器1引數 引數; server上游伺服器1引數 引數; }
3.重要引數
weight=#:設定伺服器的權重(數字越大,權重越高) backup: 設定伺服器處於備用狀態(其他節點出現故障,備用節點才開始工作) down:設定讓一個節點處於離線狀態(經常用在維護一個節點的情況下) max_fails=number:設定連續幾次轉發失敗就認為該節點出現故障,然後就不再向該節點轉發使用者請求了 fail_timeout=time: 和上個引數組合使用,作用是設定等待上游伺服器響應超時時間
4.nginx實現負載均衡實戰案例
1.環境準備
centos7.5
nginx伺服器IP:172.20.10.7/28
web1伺服器端IP:172.20.10.8/28
web2伺服器端IP:172.20.10.9/28
2.配置nginx伺服器端
安裝nginx略
配置nginx檔案
[root@ken ~]# vim /etc/nginx/nginx.conf # For more information on configuration, see: #* Official English Documentation: http://nginx.org/en/docs/ #* Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_formatmain'$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log/var/log/nginx/access.logmain; sendfileon; tcp_nopushon; tcp_nodelayon; keepalive_timeout65; types_hash_max_size 2048; include/etc/nginx/mime.types; default_typeapplication/octet-stream; # include /etc/nginx/conf.d/*.conf; upstream ken { server 172.20.10.8 weight=1 max_fails=3 fail_timeout=5; server 172.20.10.9 weight=2 max_fails=3 fail_timeout=5; } server { listen80 default_server; listen[::]:80 default_server; server_name_; root/var/www/html; index index.php index.html; # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; location / { proxy_pass http://ken/; proxy_set_header host $proxy_host; proxy_set_header realip $remote_addr; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } }
語法檢測
[root@ken ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
重啟nginx
[root@ken ~]# systemctl restart nginx
3.配置web伺服器端
略.和上面反向代理配置一樣。
4.瀏覽器測試
輸入nginx伺服器端的IP地址
因為172.20.10.9的權重為2,即出現兩次172.20.10.9才會出現一次172.20.10.8.進行重新整理測試
測試成功!
nginx的三大功能,快取,反向代理,負載均衡,已經全部講解完畢,是否對nginx有了全新的認識那?馬上自己動手實驗一下吧