Nginx reverse proxy to external URL which have redirects in place

6,339

I've figured out that in this case you basically have two different options.

  1. You either use the ngx.lua module, as Andrei Mikhaltsov pointed out, and change the proxy on whatever HTTP response you'll get back, this might of course be quite the nightmare.

  2. You do some changes on whatever application you're "forwarding" to, which in our case was a forum.

In our case, we chose number 2 and basically put them under the same domain and did some changes to the forum.

Share:
6,339

Related videos on Youtube

Lars
Author by

Lars

Updated on September 18, 2022

Comments

  • Lars
    Lars over 1 year

    Afternoon gentlemen,

    My current setup is one single site, lets call that http://production.com, that's being served by Nginx on port 80 as a static cache and Apache in the background on port 8080 localhost, nothing fancy really and this works very well.

    What I'm trying to do now is to make the URL http://production.com/foobar to include data from an external URL which we'll call http://internal.com/.

    The issue I'm having is that if I browse to http://internal.com/ I get automatically redirected to http://www.internal.com/forum/forum.php due to this being a vBulletin forum that have their own internal redirects and what not based on the Site URL variable. Of course the same thing happens if I try to proxy it through Nginx, I just get "forwarded" to the ../forum/forum.php URL.

    So, how would I go about to solve this? Is there a way to make that redirect invisible somehow and not let Nginx actually follow it in the address bar somehow? Basically an iframe without using an actual iframe. :-)

    This is my Nginx code snippet I'm trying with now:

        location /foobar {
                proxy_pass      http://internal.com/;
                proxy_redirect  off;
                proxy_set_header        Host $host;
        }
    

    Ideas? Thanks in advance.

    • Admin
      Admin about 11 years
      I do understand the problem and there are complex solutions like ngx.lua module than allows parsing of returned proxy contnent but it is easy to drown there. You can just pass /forum requests to internal.com/forum and my preferred way is to make separate subdomain for this forum like forum.production.com
    • Admin
      Admin about 11 years
      Have you tried with proxy_redirect default? This is supposed to handle HTTP redirects from the upstream. If you're referring to HTML links in the content that's a different story however.