Internal redirect to another domain with proxy_pass and Nginx

Let say we have multiple sites and and created some shared resourse (widget) under For simplicity just imagine Disqus where you need to embed comments widget (shared resourse) to every site, but don’t want to deal with AJAX “same origin policy” problems and Iframes ( actually sometimes Iframes are not so bad and could be used with care, but we are not talking about that ).

Or maybe you just want to go to and see a page from another domain, let say homepage, but preserving original url ( have no idea why would you do that, but hey.. ). So --> (internally fetches) -->

How would you do that with Nginx?

Let’s go back to our a b c .com –> . To overcome AJAX same-origin policy problem on each client side we can create special path prefix /comment, then in Nginx config we do something like this:

Now we could request which internally will be transferred to its own service as (notice /comment prefix is gone).

That way you can create small Nginx API to fetch external resources (omg!). Feel free to improvise!

Wake up, Neo... The matrix has you...

I'm Sergey Khaladzinski (@gansbrest). Not so long ago, I decided to take my life to another level. If you'd like to follow my journey, feel free subscribe below to get periodic updates about my life and the new stuff I explore.

Don't be mad if you don't receive anything from me for a period of time. I'm still learning the process.. Powered by ConvertKit

Leave a Reply

Your email address will not be published. Required fields are marked *