Thursday, July 22, 2021

Nginx Passing a Request to a Proxied Server

When NGINX proxies a request, it sends the request to a specified proxied server, fetches the response, and sends it back to the client. It is possible to proxy requests to an HTTP server (another NGINX server or any other server) or a non-HTTP server (which can run an application developed with a specific framework, such as PHP or Python) using a specified protocol. Supported protocols include FastCGI , uwsgi , SCGI , and memcached .

To pass a request to an HTTP proxied server, the <code>proxy_pass</code> directive is specified inside a <code>location</code> . For example:



location /some/path/ {

    proxy_pass http://www.example.com/link/;

}


This example configuration results in passing all requests processed in this location to the proxied server at the specified address. This address can be specified as a domain name or an IP address. The address may also include a port:



location ~ \.php {

    proxy_pass http://127.0.0.1:8000;

}


Note that in the first example above, the address of the proxied server is followed by a URI, /link/. If the URI is specified along with the address, it replaces the part of the request URI that matches the location parameter. For example, here the request with the /some/path/page.html URI will be proxied to http://www.example.com/link/page.html. If the address is specified without a URI, or it is not possible to determine the part of URI to be replaced, the full request URI is passed (possibly, modified).

To pass a request to a non-HTTP proxied server, the appropriate **_pass directive should be used:

Note that in these cases, the rules for specifying addresses may be different. You may also need to pass additional parameters to the server (see the reference documentation for more detail).

The <code>proxy_pass</code> directive can also point to a named group of servers. In this case, requests are distributed among the servers in the group according to the specified method .


Passing Request Headers


By default, NGINX redefines two header fields in proxied requests, “Host” and “Connection”, and eliminates the header fields whose values are empty strings. “Host” is set to the $proxy_host variable, and “Connection” is set to close.

To change these setting, as well as modify other header fields, use the <code>proxy_set_header</code> directive. This directive can be specified in a <code>location</code> or higher. It can also be specified in a particular <code>server</code> context or in the <code>http</code> block. For example:



location /some/path/ {

    proxy_set_header Host $host;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_pass http://localhost:8000;

}


In this configuration the “Host” field is set to the $host variable.

To prevent a header field from being passed to the proxied server, set it to an empty string as follows:



location /some/path/ {

    proxy_set_header Accept-Encoding "";

    proxy_pass http://localhost:8000;

}




References:

https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/

No comments:

Post a Comment