教程集 www.jiaochengji.com
教程集 >  服务器技术  >  集群与高可用  >  正文 nginx反向代理/负载均衡配置

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

发布时间:2014-08-15   编辑:jiaochengji.com
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端口转发到任意不一样的端口。

您可能感兴趣的文章:
nginx反向代理/负载均衡配置
django如何解决高并发
php集群如何实现
Python3爬虫进阶:Splash负载均衡配置
Nginx负载均衡和LVS负载均衡的比较分析
PHP-FPM与Nginx的通信机制总结
php的web容器有哪些
php运行在什么服务器上?
squid反向代理配置(web服务器的前端内容缓存器)
Go1.12, 轻松实现 HTTPS & WSS 动态反向代理

上一篇:ipvsadm 命令参考
[关闭]
~ ~