I've got a mandate to make an internal URL (coolnewproduct.mycoolcorp.net
) redirect to an external entity (mycoolcorp.coolerproducts.com
). Im using nginx to perform a 301 redirect for coolnewproduct.mycoolcorp.net
to mycoolcorp.coolerproducts.com
however the host header visible in the browser changes and mycoolcorp.coolerproducts.com
is seen. Is there a way for nginx to preserve the original coolnewproduct.mycoolcorp.net
host header after performing the redirect?
Asked
Active
Viewed 2,476 times
0

Alireza Rahmani Khalili
- 2,727
- 2
- 32
- 33

Vlad Bekker
- 11
- 2
-
It seems you need a transparent proxying, not a redirect. Can you include your current nginx config to your question? Does site `mycoolcorp.coolerproducts.com` use cookies? – Ivan Shatsky Dec 12 '18 at 14:23
-
``` server { listen 80; server_name coolnewproduct.mycoolcorp.net; root /var/www/html; index index.html index.htm index.nginx-debian.html; location / { proxy_pass http://mycoolcorp.coolerproducts.com/; proxy_set_header Host $proxy_host; } ``` – Vlad Bekker Dec 12 '18 at 14:29
1 Answers
1
Try this:
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;
}
}
If site mycoolcorp.coolerproducts.com
uses automatic redirection from HTTP to HTTPS, change line proxy_pass http://mycoolcorp.coolerproducts.com;
to proxy_pass https://mycoolcorp.coolerproducts.com;
.
Update
Assuming nginx compiled with a ngx_http_sub_module, to rewrite absolute links in a proxied site request body, you can try to use this config:
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;
}
}
I have never worked with this module, some people say it makes only one substitution per page. There is an alternative from our China friends which confirmed to work for this case (never used it too).

Ivan Shatsky
- 13,267
- 2
- 21
- 37
-
This works and does redirect to the new site, however Im trying to preserve the original http header that I passed in...if possible. – Vlad Bekker Dec 12 '18 at 20:35
-
@VladBekker Are you sure your browser do not caching a redirect from your previous config? Try this configuration from an incognito browser window to be sure. – Ivan Shatsky Dec 13 '18 at 05:45
-
I think this works properly, but Im missing a rewrite to make sure that all of the URI's get rewritten properly with a new hoist header. The moment you navigate on any links, you'd get a 404 – Vlad Bekker Dec 13 '18 at 14:04
-
So your external site uses absolute links instead of relative? Proxying such site is a non-trivial task. You can try to use a [`ngx_http_sub_module`](http://nginx.org/en/docs/http/ngx_http_sub_module.html), but for this you must manually compile nginx from source with a `--with-http_sub_module` configuration parameter. – Ivan Shatsky Dec 13 '18 at 14:24