Nginx
24 Apr 2018- https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms
- https://www.digitalocean.com/community/tutorials/understanding-nginx-http-proxying-load-balancing-buffering-and-caching
notes
location
-
regular expressions (
~
and~*
modifiers)http://nginx.org/en/docs/http/ngx_http_core_module.html#location
Regular expressions are specified with the preceding “~*” modifier (for case-insensitive matching), or the “~” modifier (for case-sensitive matching).
https://www.linode.com/docs/web-servers/nginx/how-to-configure-nginx/#location-blocks
Just like the server_name directive tells NGINX how to process requests for the domain, location directives cover requests for specific files and folders
NGINX always fulfills requests using the most specific match
When a location directive is followed by a tilde (~), NGINX performs a regular expression match.
If you want matches to be case-insensitive, use a tilde with an asterisk (~*).
-
trailing slash (
/
)http://nginx.org/en/docs/http/ngx_http_core_module.html#location
If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_pass, …, then the special processing is performed. In response to a request with URI equal to this string, but without the trailing slash, a permanent redirect with the code 301 will be returned to the requested URI with the slash appended. If this is not desired, an exact match of the URI and location could be defined like this:
location /user/ { proxy_pass http://user.example.com; }
https://serverfault.com/a/870620
Additionally, note that the the trailing / in the location is quite important as well — without it, you risk having weird-looking URLs on your site at one point (e.g., a working /fooen in addition to /foo/en).
Additionally, the trailing / in the location with proxy_pass also ensures some special handling, as per the documentation of the location directive, to effectively cause an implicit location = /foo {return 301 /foo/;} as well.
proxy_pass
-
trailing slash (
/
)NOTE: trailing
/
inproxy_pass
directive is also considered to be URI:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive:
location /name/ { proxy_pass http://127.0.0.1/remote/; }
https://serverfault.com/a/870620
trailing slash in proxy_pass … automatically alters the $uri variable to have the /foo/ on the front-end correspond with / on the backend. No need for an explicit rewrite directive.
forward vs. reverse proxy
https://stackoverflow.com/a/366212/3632318
What is different this time compared to a “forward proxy”, is that this time the user X does not know he is accessing Z, because the user X only sees he is communicating with Y. The server Z is invisible to clients and only the reverse proxy Y is visible externally. A reverse proxy requires no (proxy) configuration on the client side.
https://stackoverflow.com/a/14190390/3632318
A proxy is simply a middleman for communication (requests+responses): Client <-> Proxy <-> Server
Client proxy: ( Client <-> Proxy ) <-> Server
The proxy acts on behalf of the client. Client knows about all 3 machines involved in chain. Server doesn’t.
Server proxy: Client <-> ( Proxy <-> Server )
The proxy acts on behalf of the server. Client only knows about proxy. Server knows whole chain.
Seems to me that forward and reverse are simply confusing, perspective-dependent names for client and server proxy.
https://stackoverflow.com/a/28001462/3632318:
tips
reload/restart Nginx
for some reason restarting Nginx service using init.d script doesn’t work (most
likely start-stop-daemon
used internally doesn’t stop it correctly):
$ sudo service nginx restart
instead it’s possible to restart Nginx manually:
-
send
reload
signalthis reloads Nginx configuration only - without restarting service itself:
$ sudo nginx -s reload
-
or else send
stop
signal and start Nginx service as usual$ sudo nginx -s stop $ sudo service nginx start
UPDATE (2020-07-02)
I managed to restart Nginx without any problems with command:
$ sudo systemctl restart nginx.service
$ sudo journalctl -u nginx
proxy POST requests
POST requests are passed to proxy just like GET requests, no additional configuration is required.
troubleshooting
open() “/etc/nginx/mime.types” failed (2: No such file or directory)
$ sudo journalctl -u nginx.service
...
nginx[824]: nginx: [emerg] open() "/etc/nginx/mime.types" failed (2: No such file or directory) in /etc/nginx/nginx.conf:25
solution
$ sudo apt-get autoremove --purge nginx
$ sudo apt-get install nginx
open() “/run/nginx.pid” failed (2: No such file or directory)
$ sudo journalctl -u nginx.service
...
nginx[3257]: nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)
solution
$ sudo systemctl --system restart nginx