skip to Main Content

I want to route the traffic from a domain to another domain(with different subpath) without change the URL in brower using Nginx.
For example, I /app/example/ —> https://another_domain.com:443/dev/app/
I have tried using

location /app/example/ {
    proxy_pass https://another_domain.com:443/dev/app/;
    proxy_ssl_server_name on;
}

But the URL in the browser will be changed to "https://another_domain.com:443/dev/app/" when I try to access "/app/example", that it not what I want.

I have also tried using the same subpath:

 location /app/example/ {
    proxy_pass https://another_domain.com:443;
    proxy_ssl_server_name on;
}

The URL in the browser does not change, but my requirement is to route traffic to another subpath in another domain.

Could anybody help me? How can I route the traffic to another subpath of another domain using Nginx without changing the URL in browser?

2

Answers


  1. You can’t.

    Proxying isn’t a good solution, as you’ll likely break some resources on the page by serving them from a different domain.

    The best you can do is create a page that has nothing but an iframe, which loads the other site. And, this only works if the target site doesn’t have frame busting.

    Login or Signup to reply.
  2. To get this working correctly, you’ll need to own and control the responses of the destination server to ensure the HTML sub-resources (images, CSS, JS) work correctly. As Brad said, it’s difficult to proxy third-party sites you don’t control. Typically, the relative href and src tags in the destination HTML response need to use absolute URLs of the destination server URL.

    When done correctly using absolute URLs, this works:

    ##
    ##  ✅ https://example.com/proxy-absolute
    ##  ❌ https://example.com/proxy-relative
    ##
    location ~ ^/proxy-(absolute|relative)$ {
      proxy_pass https://batman.dev/static/70408096/$1.html;
    }
    

    From these third-party examples, you can see how Google search is missing sub-resources. Bing detects the proxy outright and redirects to its own domain.

    ##
    ##  https://example.com/google-search/apple
    ##
    location ~ ^/google-search/([^/]+) {
      proxy_pass https://www.google.com/search?q=$1;  
    }
    
    ##
    ##  https://example.com/bing-search/apple
    ##
    location ~ ^/bing-search/([^/]+) {
      proxy_pass https://bing.com/search?q=$1;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search