Difference between revisions of "Nginx"

From WhyAskWhy.org Wiki
Jump to: navigation, search
m (Added additional links)
m (Fixed quote marks, added Reference for RackCorp Industry Blog and added a warning about relying on nginx behavior based on older documentation)
Line 15: Line 15:
  
 
<blockquote>
 
<blockquote>
nginx first searches for the most specific prefix location given by literal strings regardless of the listed order. In the configuration above the only prefix location is /and since it matches any request it will be used as a last resort. Then nginx checks locations given by regular expression in the order listed in the configuration file. The first matching expression stops the search and nginx will use this location. If no regular expression matches a request, then nginx uses the most specific prefix location found earlier.
+
nginx first searches for the most specific prefix location given by literal strings regardless of the listed order. In the configuration above the only prefix location is "/" and since it matches any request it will be used as a last resort. Then nginx checks locations given by regular expression in the order listed in the configuration file. The first matching expression stops the search and nginx will use this location. If no regular expression matches a request, then nginx uses the most specific prefix location found earlier.
 
</blockquote>
 
</blockquote>
  
The same thing, said a little differently according to Samuel at the [http://blog.rackcorp.com/?p=31 RackCorp Industry Blog]:
+
The same thing, said a little differently according to Samuel at the RackCorp Industry Blog <ref>[http://blog.rackcorp.com/?p=31 Nginx location and rewrite configuration made easy]</ref>:
  
 
<blockquote>
 
<blockquote>
Line 24: Line 24:
 
</blockquote>
 
</blockquote>
  
 +
{{InfoBox
 +
|warning
 +
|Pay close attention to what version of nginx a book or web resource is based on as behavior may change between releases <ref>[http://nginx.org/en/docs/http/ngx_http_core_module.html#location In versions from 0.7.1 to 0.8.41, if a request matched the prefix location without the “=” and “^~” prefixes, the search also terminated and regular expressions were not checked.]</ref>
 +
}}
  
 
=== References ===
 
=== References ===

Revision as of 20:32, 1 August 2012




The following content is a Work In Progress and may contain broken links, incomplete directions or other errors. Once the initial work is complete this notice will be removed. Please contact me via Twitter with any questions and I'll try to help you out.


I've been using Nginx for a few years now and plan to do so for many more, but it wasn't an easy setup to get used to. In fact, I'm still getting bit by the differences between it and Apache, which I have much more experience with. The one difference that stands out the most of the location blocks.

For the most part, Apache directives are matched in a top down manner with later directives overwriting earlier ones if duplicated. Nginx directives on the other hand seem to operate a little differently.

Location blocks

According to the official documentation [1], this is how nginx processes a request:

nginx first searches for the most specific prefix location given by literal strings regardless of the listed order. In the configuration above the only prefix location is "/" and since it matches any request it will be used as a last resort. Then nginx checks locations given by regular expression in the order listed in the configuration file. The first matching expression stops the search and nginx will use this location. If no regular expression matches a request, then nginx uses the most specific prefix location found earlier.

The same thing, said a little differently according to Samuel at the RackCorp Industry Blog [2]:

The best way to think of things is that as a request comes in, Nginx will scan through the configuration to find a "location" line that matches the request. There are TWO modes that nginx uses to scan through the configuration file: literal string matching and regular expression checks. Nginx first scans through ALL literal string location entries in the order that they occur in the configuration file, and secondly scans through ALL the regular expression location entries in the order that they occur in the configuration file. So be aware – location ordering order DOES matter.


Pay close attention to what version of nginx a book or web resource is based on as behavior may change between releases [3]


References

  1. How nginx processes a request, A simple PHP configuration
  2. Nginx location and rewrite configuration made easy
  3. In versions from 0.7.1 to 0.8.41, if a request matched the prefix location without the “=” and “^~” prefixes, the search also terminated and regular expressions were not checked.

Additional Info


Changelogs


Books

nginx versions covered (Jun 2010)
Stable Dev Legacy
0.7.66 0.8.40 0.5.38, 0.6.39