【问题标题】:Ngnix mask subdomain to URL from another subdomain with parametersNginx 使用参数将子域掩码为来自另一个子域的 URL
【发布时间】:2019-06-18 15:25:11
【问题描述】:

是否可以像这样屏蔽一个子域以指向另一个子域: 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 页面。

【问题讨论】:

    标签: nginx url-rewriting


    【解决方案1】:

    终于!我自己想通了。

    我不得不使用proxy_pass 而不是rewrite。为了扩展 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;
       }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-25
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      • 2014-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多