nginx单一端口域名转发

对于后端是同一端口多域名转发的nginx proxy。在nginx中的默认proxy是只能对后面real server做端口转发的,而不能做域名转发。
这个是因为默认

proxy_set_header Host $proxy_host; 

这样就等于前端输入域名后到nginx这里直接转换成IP进行转发了。
于是我们需要修改proxy_set_header的参数

proxy_set_header Host $http_host;

例如下面一个举例。下面这个例子中backend1权重为5,其他默认为1,最大失效次数3次,如果30秒内没有响应就认为是失效了。

        upstream lb  {
		server backend1.test.com weight=5;
		server backend2.test.com:8080 max_fails=3  fail_timeout=30s;
		server unix:/tmp/test3;

        }

        server {
                listen 80;
                server_name  lb.test.com;
                location  /     {
                proxy_store off;
                proxy_redirect  off;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $http_host;
                proxy_pass http://lb;
                }
        }

关于 Timo
XNIX SA & MYSQL DBA

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: