nginx反向代理与负载均衡

发布时间:2019-11-17编辑:脚本学堂
实验环境 三台服务器:一台nginx作为前端反向代理服务器,IP地址192.168.2.73 一台apache作为后端的web服务器(apache用的系统自带的),IP地址192.168.5.54 一台apache作为后端的web服务器(apache用的系统自带的),IP地址192.168.5.57

实验环境
三台服务器:
一台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;
    sendfile        on;
    #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负载均衡与反向代理的例子(图文)
Nginx Proxy 代理配置图片缓存的实例参考
nginx正向代理配置简单一例
nginx反向代理配置简单示例
学习Nginx反向代理实现简单负载均衡(图文)
nginx缓存html静态文件 解析php及反向代理IIS的配置
nginx中配置proxy正向代理
Nginx实现简单的反向代理
nginx创建反向代理和虚拟主机的例子
nginx的反向代理配置与优化
nginx反向代理与varnish缓存配置
Nginx 反向代理的小例子
nginx反向代理与缓存详解
nginx反向代理配置一例
Nginx反向代理Nginx
nginx反向代理配置和优化
Nginx Proxy代理和图片缓存配置
nginx配置反向代理的简单示例