nginx域名转发 负载均衡 反向代理

公司有三台机器在机房,因为IP不够用,肯定要分出来,所以要建立单IP 多域名的反向代理,
就是当请求www.abc.com 跳转到本机, 请求www.bbc.com 跳转到192.168.0.35 机器上去,
前提 192.168.0.35 装了nginx和php环境。
#vi /usr/local/nginx/conf/nginx.conf
#修改其中的配置
 upstream www    # www可自定义,下面的名称可以用到
{
server xxx.xxx.xxx.xxx:80 max_fails=3 fail_timeout=30s; #可使用内网IP,端口可使用80 等等,服务器运行需要监听的端口。
}
upstream bbs
{
server xxx.xxx.xxx.xxx:8080 max_fails=3 fail_timeout=30s;
}

server {
listen 80;
server_name www.abc.com;
location / {
index index.html index.php index.jsp index.htm;
proxy_pass http://www;#和上面定义的名称对应 ,只能为httpURL
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;
#proxy_connect_timeout 90;
#proxy_send_timeout 90;
#proxy_read_timeout 90;
#proxy_buffers 32 4K;
}


}
server {
listen 80;
server_name bbs.abc.com ;
location / {
index index.html index.php index.jsp index.htm;
proxy_pass http://bbs;
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;
#proxy_connect_timeout 90;
#proxy_send_timeout 90;
#proxy_read_timeout 90;
#proxy_buffers 32 4K;
}
}


```````````````````````````````````````````````````````````````````````````````
当然这只是简单的通过域名进行的主机转发请求,如果负载均衡,nginx大多是对接收的主机进行轮询,所以在主机组里面添加主机就可以了。


  upstream bbs
{
server xxx.xxx.xxx.xxx:8080 max_fails=3 fail_timeout=30s;
 server 192.168.0.23:80 max_fails=3 fail_timeout=30s;
}

然后就是nginx检测配置,重启服务器了。
#/usr/local/nginx/sbin/nginx -t
#service nginx restart.
然后请求不同的域名,多台机器就多刷新几次,发现请求被分发到了不同的机器上。
然后就可以开怀的大笑了,建立好日志,等待做服务器监控脚本了。
原文地址:https://www.cnblogs.com/already/p/5063071.html