0

我的任务是使内部 URL ( coolnewproduct.mycoolcorp.net) 重定向到外部实体 ( mycoolcorp.coolerproducts.com)。我使用 nginx 执行 301 重定向,coolnewproduct.mycoolcorp.net但是mycoolcorp.coolerproducts.com浏览器中可见的主机标头会发生变化并被mycoolcorp.coolerproducts.com看到。nginx 有没有办法coolnewproduct.mycoolcorp.net在执行重定向后保留原始主机头?

4

1 回答 1

1

尝试这个:

server {
    listen 80;
    server_name coolnewproduct.mycoolcorp.net;
    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
    location / {
        proxy_set_header Host mycoolcorp.coolerproducts.com;
        proxy_cookie_domain mycoolcorp.coolerproducts.com coolnewproduct.mycoolcorp.net;
        proxy_pass http://mycoolcorp.coolerproducts.com;
    }
}

如果站点mycoolcorp.coolerproducts.com使用从 HTTP 到 HTTPS 的自动重定向,请将行更改proxy_pass http://mycoolcorp.coolerproducts.com;proxy_pass https://mycoolcorp.coolerproducts.com;.

更新

假设 nginx 使用ngx_http_sub_module编译,要重写代理站点请求正文中的绝对链接,您可以尝试使用此配置:

server {
    listen 80;
    server_name coolnewproduct.mycoolcorp.net;
    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
    location / {
        sub_filter_once off;
        sub_filter '//mycoolcorp.coolerproducts.com/' '//coolnewproduct.mycoolcorp.net/';
        proxy_set_header Host mycoolcorp.coolerproducts.com;
        proxy_set_header Accept-Encoding "";
        proxy_cookie_domain mycoolcorp.coolerproducts.com coolnewproduct.mycoolcorp.net;
        proxy_pass http://mycoolcorp.coolerproducts.com;
    }
}

我从未使用过这个模块,有人说它每页只替换一次。我们的中国朋友有一个替代方案,证实适用于这个案例(也从未使用过)。

于 2018-12-12T14:35:18.640 回答