0

My client requires to combine all his ventures into one umbrella without moving them from their original locations. For example, he already has a site running as realestate.com and movers.com and now he wants to combine them under corporate.com such that corporate.com/real link will map to real.corporate.com and shows the website hosted at realestate.com. This is not a redirection so the URL bar will show the real.corporate.com for realestate.com and all the links will also be relative e.g. realestate.com/index.html will show as real.corporate.com/index.html. realestate.com/portfolio/houses/pictures will become real.corporate.com/portfolio/houses/pictures. I have tried using proxy_pass but that changes the URL. I am currently testing it with one site and following is my server block

server{
listen 80;

location / {
proxy_pass http://www.realestate.com/;
sub_filter_once off;
proxy_redirect off;
proxy_set_header Host $host;
}
}

This setting takes me to realestate.com directly and URL is also changed in the browser. changing location to location /real gives 404 error.

Please help

Abdul
  • 3
  • 1
  • What is the output of `curl -v http://corporation.com/real`? Or alternatively, what does the developer tools Network tab show when you make the request? – Tero Kilkanen Aug 15 '20 at 16:14
  • It turned out much of the output i have been receiving was either from cache or CDN. I have now created a simple experimental setup in which i am trying to make 127.0.0.1(localhost) to sublocal.localhost and show stackoverflow page. With the above conf file using curl -v 127.0.0.1 show me "Welcome to Nginx Page" – Abdul Aug 15 '20 at 16:57
  • Please add the output of `nginx -T` in your question. The response is most likely sent by your nginx `default_server` block. – Tero Kilkanen Aug 15 '20 at 23:06
  • You are probably correct. You can view my nginx.conf at https://justpaste.it/5f511 – Abdul Aug 17 '20 at 19:38
  • In your example, you are reverse proxying to `http://www.stackoverflow.com`. That URL sends a response with 301 redirect to `https://stackoverflow.com`. You need to `proxy_pass` to a destination that does not send redirects, and you also need to handle redirects like I write in the answer below. – Tero Kilkanen Aug 17 '20 at 20:04
  • Thanks. Worked like a charm.I have hosted the example here. However some links are still off, like talent (https://stackoverflow.com/talent) , advertising(https://stackoverflow.com/advertising) etc. Any idea what i might have been doing wrong here ? – Abdul Aug 19 '20 at 22:01
  • Those URLs are generated by the server that you are proxying. They are sending absolute URLs in the payload, which is why they look like this. Modifying URLs in the payload could replace those, but that is complex and error-prone. – Tero Kilkanen Aug 20 '20 at 06:22
  • In general, you should only use reverse proxying to sites that you control. Otherwise the system will break often. – Tero Kilkanen Aug 20 '20 at 06:23

1 Answers1

1

Your configuration does not do any redirects. The redirect is sent by the upstream webserver at realestate.com.

The application sends a redirect to realestate.com, because the request is not sent to domain configured in the application.

You can change this by using

proxy_set_header Host www.realestate.com;

This setting sends the upstream proxied request with Host: www.realestate.com header, which will make it end up at proper virtual server in the upstream server.

However, you could still have an issue with links generated by www.realestate.com.

Additionally, if www.realestate.com sends any HTTP redirect, you need to replace redirect contents with proxy_redirect default; directive.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • Adding proxy_set_header does not help. corporation.com/real takes me directly to realestate.com and URL in the location bar also changes – Abdul Aug 15 '20 at 14:52