nginx反向代理/负载均衡配置

发布时间:2020-12-06编辑:脚本学堂
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;
}
 

然后保存,检查配置文件是否有问题
[root@hadoop3 ~]# /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
[root@hadoop3 ~]# /usr/local/nginx/sbin/nginx
配置两台apache服务器

登录192.168.5.54上操作:
[root@hadoop5 ~]# echo 'this is 192.168.5.54!' > /var/www/html/index.html
修改/etc/httpd/conf/httpd.conf文件的监听端口为8080
[root@hadoop5 ~]# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
[root@hadoop5 ~]# /etc/init.d/httpd start

登录192.168.5.57上操作:
[root@service ~]# echo 'Hello,This is 192.168.5.57!' > /var/www/html/index.html
修改/etc/httpd/conf/httpd.conf文件的监听端口为8080
[root@service ~]# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
[root@service ~]# /etc/init.d/httpd start

测试:
[root@hadoop3 ~]# 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端口转发到任意不一样的端口。