安装nginx1.2.0配置简单负载

发布时间:2019-10-04编辑:脚本学堂
安装nginx1.2.0配置简单负载

安装nginx1.2.0

下载nginx源码包
http://nginx.org/download/nginx-1.2.0.tar.gz

安装pcre支持
yum install pcre-devel

安装nginx
cd /opt/
tar zxvf nginx-1.2.0.tar.gz
cd nginx-1.2.0
./configure --with-http_stub_status_module --prefix=/usr/local/nginx
make && make install
安装完成

cd /usr/local/nginx/sbin/
./nginx
启动 nginx
lsof -i:80

防火墙添加80端口 重启防火墙

访问 http://192.168.1.1
welcome to nginx !

实现简单负载

修改 /usr/local/nginx/conf/nginx.conf
修改内容如下:
 

复制代码 代码如下:

http {
    include       mime.types;
    default_type  application/octet-stream;

    upstream test{
        server  192.168.1.2:80        weight=3 max_fails=3 fail_timeout=20s;
        server  192.168.1.3:80        weight=3 max_fails=4 fail_timeout=20s;
        server  192.168.1.4:80        weight=3 max_fails=1 fail_timeout=20s;
}

 server {
        listen       80;
        server_name  www.test.com     192.168.1.1;

        index  index.html index.htm;
        location / {
        proxy_pass http://test;
        proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
        include  /usr/local/nginx/conf/proxy.conf;
}
        }

添加 /usr/local/nginx/conf/proxy.conf
内容如下:
 

复制代码 代码如下:
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

保存退出!

重启nginx。
修改本地hosts文件内容:
192.168.1.1   www.test.com

测试:
访问 http://www.test.com 或 http://192.168.1.1