Ngnix掩码子域到具有参数的另一个子域的URL

时间:2019-06-18 06:21:56

标签: nginx url-rewriting

是否可以像这样屏蔽一个子域以指向另一个子域: sub.domain.comsub2.domain.com/example/example2

我可以使用以下nginx配置来获得此信息:

server {
listen 443 ssl http2; # managed by Certbot
<!-- ssl_certificate goes here -->
server_name sub.domain.com;

rewrite ^/?$ https://sub2.domain.com/example/example2 permanent;
}

但是此配置的问题在于,当您转到sub.domain.com时,您将重定向到sub2.domain.com/example/example2,而不仅仅是屏蔽URL。

如果我转到sub.domain.com/test而不是转到sub2.domain.com/example/example2/test,它将显示404页。

1 个答案:

答案 0 :(得分:0)

最后!我自己弄清楚了。

我不得不使用rewrite而不是proxy_pass。为了扩展URL,我只需要在URL的末尾添加一个/

location / {
   proxy_pass   https://sub2.domain.com/example/example2/; <-- Note the slash at the end
   proxy_redirect off;
   proxy_set_header X-Real-IP sub2.domain.com;
   proxy_set_header X-Forwarded-For sub2.domain.com;
   proxy_set_header Host sub2.domain.com;
}

完整代码:

server {
   listen 443 ssl http2; # managed by Certbot

   <!-- ssl_certificate goes here -->

   server_name sub.domain.com;

   location / {
      proxy_pass   https://sub2.domain.com/example/example2/;
      proxy_redirect off;
      proxy_set_header X-Real-IP sub2.domain.com;
      proxy_set_header X-Forwarded-For sub2.domain.com;
      proxy_set_header Host sub2.domain.com;
   }

}