13

I am very new to nginx configuration. I want to use it set virtual hosts so that I can run different Node JS apps on different ports on the same server, but have them served at different subdomains. After some searching I found a post on stackoverflow and it works well for what I want to do. Amongst other things, the code involved the following location block

location / {                                                              │
    proxy_set_header X-Real-IP $remote_addr;                              │
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;          │
    proxy_set_header Host $http_host;                                     │
    proxy_set_header X-NginX-Proxy true;                                  │
                                                                          │
    proxy_pass http://otherapp/;                                          │
    proxy_redirect off;                                                   │
}

Now I wanted to understand what the script does. I looked at nginx docs and notes posted by various people, and understood a few things. It appears to me that what I want will probably work even if I remove all the proxy_set_header lines in the above code EXCEPT proxy_pass. I want to learn what the different fields in the above code are doing. Probably they are useful for some things that I don't realize at the moment. Is there a place which lists all the fields for proxy_set_header module and explains them?

Thank you.

1 Answer 1

15

Essentially, what it's doing is providing information to the server that it's proxying to. In order:

  1. X-Real-IP - the server you're proxying to is going to see the nginx proxy system as the "client IP" in any sort of application logic or logging that it's doing, since the nginx service is the source of the TCP connection. This is usually pretty worthless, especially in logs. The backend server can use this header instead of its normal client IP for logging or other uses where it needs the client's IP.
  2. X-Forwarded-For - this is a standard-ish header, which is similar to X-Real-IP, but provides added connection source entries for the entire chain of proxies the connection's passed through. Can be a bit more of a headache to parse and work with from the backend server since there's possibly multiple entries.
  3. Host - This is the one most likely to matter for your application. Normally, the request that nginx makes to the backend server will contain in its Host header the address that you've configured (in this case http://otherapp/). This is probably not desired if proxying to something doing name-based virtual hosting, or something that generates links based on the received host header. This config is making it so that the Host header that the client sent nginx is sent on to the backend (equivalent to Apache's ProxyPreserveHost).
  4. X-NginX-Proxy - All this is doing is acting as a marker that the proxy is used. Probably not useful in most cases.
4
  • Thank you. Is there a site that has information about any other fields? The documentation on nginx site does not have that information.
    – user191017
    Sep 22, 2013 at 22:44
  • @user191017 Any HTTP header (including ones you make up that you application handles) is fair game - see here. Sep 22, 2013 at 22:55
  • @user191017 Nginx website doesn't have information about english alphabet too. What are you asking is actually HTTP-protocol essentials. See: tools.ietf.org/html/rfc2616
    – VBart
    Sep 23, 2013 at 17:18
  • After hunting around, this is the first description I've been able to find for "X-NginX-Proxy" - it seems many people use it but no-one explains it? Is there official docs somewhere?
    – eugenevd
    Feb 6, 2019 at 13:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .