Question

A few doubtful nginx directives. Pls help.

I was following this DO article: https://www.digitalocean.com/community/tutorials/nginx-location-directive

And have these doubts regarding nginx conf files directives:

  • What if there are 2 non-regexp location blocks: having /t and /s and user types /f, then which location would be served, and why?
  • If a location directive has ^~ /somepath prefix, and the visitor has typed example.com/some then would Nginx stop searching even at this partial match? If yes, then it’d stop at / matching also? If so, then why that conf file should’ve any other location block at all? Because in any case, / would be matched at all times, then why bother having any other locations?!!
  • When a user types sub.example.com/somepath/?user=any then what exactly is $host and what exactly is $hostname in it?

Submit an answer
Answer a question...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
February 26, 2023

Hey @bathindahelper,

Those are all good question. Let me try to clarify them. Having said that, If I’m unable to or it’s not clear enough, I’ll urge you to check the following Nginx documentation on location blocks:

https://docs.nginx.com/nginx/admin-guide/web-server/web-server/#location_priority

  1. What if there are 2 non-regexp location blocks: having /t and /s and user types /f, then which location would be served, and why?
  • Neither as they won’t match the location block you’ve created.
  1. If a location directive has ^~ /somepath prefix, and the visitor has typed example.com/some then would Nginx stop searching even at this partial match? If yes, then it’d stop at / matching also? If so, then why that conf file should’ve any other location block at all? Because in any case, / would be matched at all times, then why bother having any other locations?!!
  • ^~: The carat followed by tilde sign is used to perform longest nonregular expression match against the requested URI. If the requested URI hits such a location block, no further matching will takes place. Basically, The modifier ^~ in the mentioned location block results in a case-sensitive regular expression match. Therefore the URI /somepath or /somepath/logo.png will be matched but stops searching as soon as a match is found. So no, it will not work at some
  • As for the ‘/’ it will match all requests but will be used as a last resort if no matches are found.

More on those here

https://www.digitalocean.com/community/tutorials/nginx-location-directive