nginx 虚拟主机配置

http {
server {
listen  80;
server_name     www.domain1.com;
access_log              logs/domain1.access.log main;
location / {
index index.html;
root /web/www/domain1.com/htdocs;
                }
        }
server {
listen          80;
server_name             www.domain2.com;
access_log logs/domain2.access.log main;
location / {
index index.html;
root /web/www/domain2.com/htdocs;
                }
        }
include /opt/nginx/conf/vhosts/www.domain2.com.conf;
#可以用apache类似include方式load进来
}

 nginx负载均衡配置

环境
后端nginx节点
192.168.3.128
192.168.3.129
192.168.3.130

nginx负载均衡服务器
192.168.3.131


131上
编辑配置nginx.conf
http
{
        upstream myserver{
 server 192.168.3.128:80 weight=3 max_fails=3 fail_timeout=20s;
 server 192.168.3.129:80 weight=1 max_fails=3 fail_timeout=20s;
 server 192.168.3.130:80 weight=4 max_fails=3 fail_timeout=20s;
}
server
 {
listen          8080;
server_name     www.zhoutao.name 192.168.12.131;
index           index.htm index.html;
root            /tmp/;
 location /{
 proxy_pass http://myserver;
 proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
 include /usr/local/nginx/conf/proxy.conf;
  }
 }
}


编辑proxy.conf配置文件,然后include进来
proxy_redirect off;
proxy_set_header Host $host; 由后端服务器获取用户的主机名或真实ip地址
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 90; 后端服务器连接的超时时间,
proxy_send_timeout 90;  后端服务器的数据传回时间,如在规定时间之内后端服务器必须传完所有的数据,否则nginx断开这个连接
proxy_read_timeout 90; 设置nginx从代理的后端服务器获取信息的时间,表示连接建立成功后,nginx等待后端服务器的相应时间,其实是nginx已经进入后端队列中等候处理的时间
proxy_buffer_size 4k; 指定客户端请求主体缓冲区大小,可以理解为先保存到本地在传给用户
proxy_buffers 4 32k;  设置缓冲区大小, 默认,该缓冲区大小等于指令 proxy_buffers 设置的大小
proxy_busy_buffers_size 64k; 用于设置系统很忙的时候可以使用proxy_buffers大小,官方推荐proxy_buffers*2
proxy_temp_file_write_size 64k; 指定proxy缓存临时文件的大小

启动4台nginx
用while循环测试实验成功
[root@localhost ~]# while true ; do curl 192.168.3.131:8080 `sleep 1`; done
192.168.3.128
192.168.3.130
192.168.3.128
192.168.3.128
192.168.3.128
192.168.3.128
192.168.3.130
192.168.3.130
192.168.3.129
192.168.3.130
192.168.3.130
192.168.3.128
192.168.3.130
192.168.3.130
192.168.3.128