歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> nginx反向代理、負責均衡功能的實現

nginx反向代理、負責均衡功能的實現

日期:2017/3/1 12:11:12   编辑:關於Linux

nginx1.8.1 proxy 服務器192.168.8.40

web1 centos6.5 httpd2.2.15

web2 centos7.2 httpd2.4.6

1、代理功能的簡單實現

nginx代理服務器:192.168.8.40
web服務器:192.168.8.101

8.40添加代理:
location /forum/ {
proxy_pass http://192.168.8.101/bbs/;
}
\

在被代理的web端
創建目錄mkdir /web/htdocs/bbs
vim /web/htdocs/bbs/index.html
加入

192.168.8.101 bbs


訪問 http://192.168.8.40/forum/即可出現8.101的內容


改成正則表達式的方式:
location ~* ^/forum {
proxy_pass http://192.168.8.101;
}


此時http://192.168.8.40/forum/的方式不能訪問,需要通過修改192.168.8.101的bbs目錄改為forum即可訪問
# mv bbs forum


2、代理上顯示客戶端真實IP(方便統計真實的IP訪問情況)

8.101上更改顯示日志的方式:
# vim /etc/httpd/conf/httpd.conf


LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

\

\


nginx服務器端8.40配置:
location ~* ^/forum {
proxy_pass http://192.168.8.101;
proxy_set_header X-Real-IP $remote_addr;
}
\

3.實現簡單的負載均衡:

nginx proxy server:192.168.8.40
apache web1:192.168.8.39
apache web2:192.168.8.101
\

nginx proxy server:192.168.8.40配置:


# 定義web服務器集群:
upstream webservers {
server 192.168.8.39 weight=1;
server 192.168.8.101 weight=1;
}


server {


#location / {
# root /web/htdocs;
# index index.php index.html index.htm;
#}

#定義訪問集群
location / {
proxy_pass http://webservers/;
proxy_set_header X-Real-IP $remote_addr;
}
}
\

通過訪問http://192.168.8.40可以看到負載的效果


4、對負載均衡的服務器宕機情況進行適配

#添加錯誤的定義
server {
listen 8080;
server_name localhost;
root /web/errorpages;
index index.html;
}
# 創建錯誤頁面定義
# mkdir /web/errorpages/ -pv
# vim index.html
加入
sorry,website is being repaired please wait


# 添加超時定義及錯誤頁面定義,如果連續訪問錯誤兩次則踢掉,檢測時間間隔2秒
upstream webservers {
server 192.168.8.39 weight=1 max_fails=2 fail_timeout=2;
server 192.168.8.101 weight=1 max_fails=2 fail_timeout=2;
server 127.0.0.1:8080 weight=1 backup;
}


測試,關閉web1則,只能訪問到web2,關閉web2後出現錯誤提示
\

5、為反向代理啟用緩存功能



proxy_cache_path /nginx/cache/first levels=1:2 keys_zone=first:20m max_size=1g;


server {
listen 80;
server_name localhost;
index index.html index.php;


add_header X-Via $server_addr;
add_header X-Cache "$upstream_cache_status from $server_addr";


location / {
proxy_pass http://webservers/;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache first;
proxy_cache_valid 200 10m;
}

\
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] mkdir() "/nginx/cache/first" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed
[root@centossz008 ~]# mkdir -pv /nginx/cache/first
mkdir: created directory `/nginx'
mkdir: created directory `/nginx/cache'
mkdir: created directory `/nginx/cache/first'


add_header X-Via $server_addr;
add_header X-Cache "$upstream_cache_status from $server_addr";
提示信息如下:
\

6、重定向規則

location / {
#root html;
root /web/htdocs;
index index.html index.htm;
rewrite ^/bbs/(.*)$ http://192.168.8.101/forum/$1;
}


訪問:http://192.168.8.40/bbs/

\

\

7、上傳文件的負載均衡

可能碰到這樣的業務場景,幾台web app設置了主從,一個服務器負責上傳,其他只能通過同步來獲取

nginx配置:
location / {
proxy_pass http://192.168.8.40/;
if ($request_method = "PUT"){
proxy_pass http://192.168.8.101;
}
}


客戶端配置:
# vim /etc/httpd/conf/httpd.conf
下面添加Dav on

Dav on
\

# curl -T /etc/fstab http://192.168.8.40

Created

Resource /fstab has been created.

在8.101上可以看到上傳的文件
htdocs]# ls
forum fstab index.html
Copyright © Linux教程網 All Rights Reserved