How to debug Nginx `location` matching?
Note: This applies to Debian. File locations may differ on other operating systems.
First, enable debug logging in /etc/nginx/nginx.conf
. To do so, watch out for the line error_log
/var/log/nginx.error.log;
. Append debug
so that the line looks like error_log
/var/log/nginx.error.log debug;
.
Then, restart Nginx to pick up the configuration change: sudo systemctl restart nginx
Execute the following command in the shell: sudo tail -fn0 /var/log/nginx/error.log | grep "test location\|using configuration"
Now, use your browser (or curl) to issue the request that you'd like to debug.
You should see log messages along the lines of
2023/11/21 12:22:13 [debug] 9911#9911: *172 test location: "/"
2023/11/21 12:22:13 [debug] 9911#9911: *172 test location: "private"
2023/11/21 12:22:13 [debug] 9911#9911: *172 using configuration "/private"
test location
shows which location declarations Nginx considers for the given request, and using configuration
shows the one Nginx selects for processing the request.
Last but not least, disable Nginx debugging again. Don't forget to restart Nginx.