Nginx-不同的“ proxy_pass”共享相同的“ server_name”

时间:2019-05-05 07:54:19

标签: nginx

nginx的新手

1现状

我有一项服务,但有两个版本-旧版本和新版本

http://old.somedomain.com -> old_server:3000
http://new.somedomain.com -> new_server:3000

nginx配置

server {
    listen 80;
    server_name old.somedomain.com;

    location / {
        proxy_pass http://old_server:3000;
        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;
    }
}

server {
    listen 80;
    server_name new.somedomain.com;

    location / {
        proxy_pass http://new_server:3000;
        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;
    }
}

主机文件

<old_server ip>     old.somedomain.com
<new_server ip>     new.somedomain.com

1.x结果

工作很好,但是此解决方案需要添加两行-<ip> <domain>每次都要在每个人的PC上托管文件!是否可以简化这种情况?


2我的想法

http://somedomain.com/old -> old_server:3000
http://somedomain.com/new -> new_server:3000http://somedomain.com -> new_server:3000

只需要向主机文件添加一行!

我尝试了这个nginx配置,但是没有用。(当然它不起作用,看起来像sh * t!)

server {
    listen 80;
    server_name somedomain.com;

    location /old {
        proxy_pass http://old_server:3000;
        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;
    }

    location / {
        proxy_pass http://new_server:3000;
        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;
    }
}

所以我的想法可能吗?有更好的解决方案吗?

0 个答案:

没有答案