2017-08-07 114 views
1

我有两个域website1.comwebsite2.com链接到我的服务器。nginx重定向取决于主机

我尝试做以下重写规则:

http://website1.com/   --> /website1/ (static) 
http://website2.com/   --> /website2/ (static) 

http://website1.com/app/  --> http://localhost:8080/web-app/web1/ 
http://website2.com/app/  --> http://localhost:8080/web-app/web2/ 

用户会被重定向到nginx的或根据URL的应用程序服务器提供一个静态的网站。

这里是我试过到目前为止:

location/{ 
     root html; 
     index index.html index.htm; 

     if ($http_host = website1.com) { 
      rewrite/ /website1/index.html break; 
      rewrite (.*) /website1/$1; 
     } 
     if ($http_host = website2.com) { 
      #same logic 
     } 
    } 

    location /app/ { 
     proxy_pass http://localhost:8080/web-app/; 
     proxy_set_header Host $host; 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 

     if ($http_host = website1.com) { 
     rewrite /app/(.*) /$1   break; 
     rewrite /app  /index.html; 
     } 
     if ($http_host = website2.com) { 
      #same logic 
     } 
    } 

静态部分看起来做工精细,但重定向的Web应用程序的一部分,似乎服务的index.html不管请求的文件是什么。

+0

待办事项WEBSITE1和网站2没有单独的服务器块? – MatTheWhale

+0

没有。他们应该吗?这是我第一次使用nginx进行嵌套。 –

+0

如果你愿意,你不必拥有所有'if($ http_host ==)'块。他们可能是问题的原因 – MatTheWhale

回答

1

这不是一个明确的答案,而只是我对如何让nginx代理工作的解释。

root html; 
index index.html index.htm; 

server { 

    listen 80; 
    server_name website1.com; 
    location/{ 
     alias html/website1; 
    } 

    location /app/ { 
     proxy_pass http://localhost:8080/web-app/web1/ 
    } 
} 

server { 

    listen 80; 
    server_name website2.com; 
    location/{ 
     alias html/website2; 
    } 

    location /app/ { 
     proxy_pass http://localhost:8080/web-app/web2/ 
    } 
} 

问题看起来像是被这些重写造成的:

rewrite /app/(.*) /$1   break; 
rewrite /app  /index.html; 

使用与server_name的服务器块和alias指令,我们可以与需要使用那么多的逻辑做了。让我知道是否还有什么还不清楚。

+0

我发现一个解决方案与这个稍有不同..因为我发布问题后我的需求发生了变化。你的答案似乎是正确的,谢谢。 –

1

我觉得你做错了。如果主机之间存在如此多的差异,那么拥有两种不同的配置会更清晰和更高效,每个主机都有一个配置。另一方面,如果您的意图是拥有多个几乎完全相同的配置,那么正确的解决方法可能是map,而不是if

返回到您的配置 - 我试着运行它只是为了看看它是如何工作的,以及一件事,你可能会注意到的是,你的proxy_pass中指定的路径实际上变成一个空操作,一旦内的$host特异性rewrite相同的上下文涉及到更改$uri - 这是设计,并在http://nginx.org/r/proxy_pass“当URI在代理位置内使用重写指令”“更改时)非常清晰地记录。

所以,实际上,使用下面的配置确实出现了坚持你的规格:

%curl -H "Host: host1.example.com" "localhost:4935/app/" 
host1.example.com/web-app/web1/ 

%curl -H "Host: host2.example.com" "localhost:4935/app/" 
host2.example.com/web-app/web2/ 

%curl -H "Host: example.com" "localhost:4935/app/" 
example.com/web-app/ 

下面是我使用的配置:

server { 
    listen [::]:4935; 
    default_type text/plain; 
    location/{ 
     return 200 howdy; 
    } 
    location /app/ { 
     proxy_set_header Host $host; 
     proxy_pass http://localhost:4936/web-app/;#path is NOOP if $uri get changed 
     if ($host = host1.example.com) { 
      rewrite /app/(.*) /web-app/web1/$1   break; 
      rewrite /app  /web-app/index.html; 
     } 
     if ($host = host2.example.com) { 
      rewrite /app/(.*) /web-app/web2/$1   break; 
      rewrite /app  /web-app/index.html; 
     } 
    } 
} 

server { 
    listen [::]:4936; 
    return 200 $host$request_uri\n; 
}