Jquery中文網 www.twdanq.live
Jquery中文網 >  服務器技術  >  集群與高可用  >  正文 nginx反向代理/負載均衡配置

nginx反向代理/負載均衡配置

發布時間:2014-08-15   編輯:www.twdanq.live
nginx反向代理/負載均衡配置,供大家學習參考。

nginx反向代理/負載均衡配置,供大家學習參考。

實驗環境三臺服務器:
一臺nginx作為前端反向代理服務器,IP地址192.168.2.73
一臺apache作為后端的web服務器(apache用的系統自帶的),IP地址192.168.5.54
一臺apache作為后端的web服務器(apache用的系統自帶的),IP地址192.168.5.57
nginx服務器配置:

1、安裝步驟很簡單。
 

復制代碼 代碼如下:
./configure --prefix=/usr/local/nginx
make
make install

2、修改nginx.conf文件,設置proxy相關參數,在httpd字段中增加如下內容:
 

復制代碼 代碼如下:
http {
    include       mime.types;
    default_type  application/octet-stream;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #  '$status $body_bytes_sent "$http_referer" '
    #  '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  logs/access.log  main;
    sendfileon;
    #tcp_nopush     on;
    client_max_body_size 300m;
    client_body_buffer_size 128k;
    client_body_temp_path /dev/shm/client_body_temp;
    proxy_read_timeout 600;
    proxy_send_timeout 600;
    proxy_buffer_size 16k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
    proxy_temp_path /dev/shm/proxy_temp;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    upstream server_pool {
    server 192.168.5.54:8080 weight=8 max_fails=2 fail_timeout=30s;
    server 192.168.5.57:8080 weight=8 max_fails=2 fail_timeout=30s;
    }
    #gzip  on;

繼續修改nginx.conf文件,在server中lication /配置中做如下修改:
 

復制代碼 代碼如下:
location / {
    proxy_pass http://server_pool/;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
    root   html;
    index  index.html index.htm;
}
 

然后保存,檢查配置文件是否有問題
[[email protected] ~]# /usr/local/nginx/sbin/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、啟動nginx
[[email protected] ~]# /usr/local/nginx/sbin/nginx
配置兩臺apache服務器

登錄192.168.5.54上操作:
[[email protected] ~]# echo 'this is 192.168.5.54!' > /var/www/html/index.html
修改/etc/httpd/conf/httpd.conf文件的監聽端口為8080
[[email protected] ~]# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
[[email protected] ~]# /etc/init.d/httpd start

登錄192.168.5.57上操作:
[[email protected] ~]# echo 'Hello,This is 192.168.5.57!' > /var/www/html/index.html
修改/etc/httpd/conf/httpd.conf文件的監聽端口為8080
[[email protected] ~]# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
[[email protected] ~]# /etc/init.d/httpd start

測試:
[[email protected] ~]# for i in $(seq 20); do curl http://192.168.2.73/; done
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!

成功完成了nginx反向代理服務器的配置,如果我沒記錯應該比lvs的負載強一點,lvs只支持80端口轉發到80端口,而nginx可以80端口轉發到任意不一樣的端口。

您可能感興趣的文章:
nginx配置反向代理的簡單示例
nginx反向代理/負載均衡配置
Nginx負載均衡和LVS負載均衡的比較分析
squid反向代理配置(web服務器的前端內容緩存器)
Nginx的配置Upstream負載均衡模塊
如何開啟Nginx的gzip壓縮功能
LVS調度算法和三種工作模式
多規則替換過濾nginx模塊nginx_substitutions_filter
Amoeba:開源的分布式數據庫Porxy解決方案
nginx配置gzip壓縮一例

上一篇:ipvsadm 命令參考
[關閉]
广西快乐10分怎样开奖号码