Score:0

Website using wrong URL after proxing though Nginx

cn flag

This is my first time using nginx and I'm finding this situation quite puzzling. My website is hosted at web.example.com and in order to reach the backend, requests are routed though nginx as a reverse proxy. After processing through nginx, the URL changes to the URL of the nginx server (proxy.example.com) instead of being the URL of the website.

The flow looks something like this:

web.example.com -> nginx (proxy.example.com) -> backend (service.example.com)

When I log the request in the backend service, I can see that the x-forwarded-host is set to proxy.example.com. Is this correct or should it be set to web.example.com here?

The nginx.conf file is:

events {
    worker_connections  1024;
}

http {
    include    /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format   main '$remote_addr - $remote_user [$time_local]  $status '
        '"$request" $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for"';

    access_log   logs/access.log  main;

    server { # simple reverse-proxy
        listen       80;
        server_name  proxy.example.com;

        location /site {
            proxy_set_header Host $http_host;
            proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
            proxy_set_header x-forwarded-host $host;
            proxy_set_header x-forwarded-server $host;
            proxy_set_header x-real-ip $remote_addr;
            proxy_pass_request_headers on;
            proxy_pass http://service.example.com$request_uri;
        }
    }
}

How do I get the URL to go back to web.example.com/site after going through this flow instead of showing as proxy.example.com/site?

Thanks.

Michael Hampton avatar
cz flag
This configuration looks correct. Exactly what are you seeing that is a problem? Be specific.
cn flag
@MichaelHampton the problem is that the proxy's URL is exposed. Why is that happening? I want the URL to remain web.example.com.
Michael Hampton avatar
cz flag
What exactly do you mean? The vague description is useless.
cn flag
The URL in the location bar of the browser changes from web.example.com to proxy.example.com so the end users can see the URL of the proxy. I don't think that's supposed to happen
Michael Hampton avatar
cz flag
OK, now I get it. That isn't caused by nginx though; that's your backend.
cn flag
@MichaelHampton can you say more? How would my backend change the URL in the location bar?
Michael Hampton avatar
cz flag
There's no way any of us can know; it's your backend and you have said nothing about what it is.
cn flag
Backend is a java application running on Tomcat 9. I'm logging the headers that come into that application and see the x-forward-* headers set to `proxy.example.com`
Michael Hampton avatar
cz flag
What header? Again, be specific.
cn flag
x-forward-host and x-forward-for are both set to proxy.example.com
Michael Hampton avatar
cz flag
That doesn't correspond to the nginx configuration you have shown. What else is in this stack? This time, tell us everything and don't leave out relevant details.
cn flag
@MichaelHampton yes, I added a few more headers in debugging.
ar flag
Could you please elaborate on your stack? You say that you have a website at `web.example.com` and you use an nginx as a reverse proxy to `service.example.com` so, it's your web served by the nginx server and context `/site` is proxied to the backend or do you have a previous element prior to that nginx? If you do not have it, why the nginx server is called internally `proxy.example.com` instead of `web.example.com`?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.