我有两个域website1.com
和website2.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不管请求的文件是什么。
待办事项WEBSITE1和网站2没有单独的服务器块? – MatTheWhale
没有。他们应该吗?这是我第一次使用nginx进行嵌套。 –
如果你愿意,你不必拥有所有'if($ http_host ==)'块。他们可能是问题的原因 – MatTheWhale