Wednesday, February 8, 2012 14:40

Nginx configuration – nginx.conf

Tagged with:
Posted by on Tuesday, August 10, 2010, 19:52
This news item was posted in Java, Linux category and has 1 Comment so far.
server {
    listen        80;
    server_name   nginx.org  www.nginx.org;
    root          /data/www;

    location / {
        index     index.html  index.php;
    }

    location ~* \.(gif|jpg|png)$ {
        expires   30d;
    }

    location ~ \.php$ {
        fastcgi_pass   localhost:9000;
        fastcgi_param  SCRIPT_FILENAME
                       $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

nginx first searches for the most specific location given by literal strings regardless of the listed order. In the configuration above the only literal 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 literal location found earlier.

File excerpt: nginx.conf location directives

location / { }
location /images/ { }
location /blog/ { }
location /planet/ { }
location /planet/blog/ { }

location ~ IndexPage\.php$ { }
location ~ ^/BlogPlanet(/|/index\.php)$ { }

location ~* \.(pl|cgi|perl|prl)$ { }
location ~* \.(md|mdwn|txt|mkdn)$ { }

location ^~ /images/IndexPage/ { }
location ^~ /blog/BlogPlanet/ { }

location = / { }

The first five examples are “literal string” matches, and match the beginning part of the request following the host portion. In a hypothetical request for http://ducklington.org/, assuming that there is a server_name for ducklington.org, the “location /” directive will catch this request. Nginx will always fulfill a request with most specific match. A request for http://ducklington.org/planet/blog/ and http://ducklington.org/planet/blog/about/ will be fulfilled by the “location /planet/blog/” even though “location /planet/” also fills this request.

When a location directive is followed by a tilde (e.g. ~) as in the fourth group of examples, nginx performs a regular expression match. These matches are always case sensitive, so in the first example requests ending in IndexPage.php would match, but indexpage.php would not. In the second the regular expression ^/BlogPlanet(/|index\.php)$ will match requests for /BlogPlanet/ and /BlogPlanet/index.php, but not /BlogPlanet, /blogplanet/, or /blogplanet/index.php. Nginx uses Perl Compatible Regular Expressions (PCRE).

Using a tilde-asterisk (e.g. ~*) as an argument to a location directive, as in the third group of examples, removes case sensitivity from the matches. These examples all specify how nginx should process requests that end in a particular file extension. In the first example, any file ending in: .pl, .PL, .cgi, .CGI, .perl, .Perl, .prl, and .PrL (among others) will match the request.

In the next group of examples, the caret-tilde argument (e.g. ^~) to location functions like the literal string matches of the first group. However, when used, they force nginx to stop searching for more specific matches. So in these examples, the location directives for “^~ /images/IndexPage/” and “^~ /blog/BlogPlanet/” would be used even though the request matches another location directive. See below for more information about the order and priority of location directive processing.

Finally, using the equal sign (e.g. =) as an argument to location forces an exact match with the path requested and then stops searching for more specific matches. For instance, the final example will only match http://ducklington.org/ but not http://ducklington.org/index.html. Using exact matches can speed up request times slightly especially when some requests are particularly popular.

Directives are processed in the following manner:

  • Exact string matches are processed first. If a match is found, nginx stops searching and fulfills the request.
  • Remaining literal string directives are processed next. If the “^~” argument is used, then ngnix stops here and fulfills the request. Otherwise, nginx continues to process location directives.
  • All location directives specified by regular expressions (with the ~ and ~* arguments) are processed. If a regular expression matches the request, nginx stops here and fulfills the request.
  • When there are no regular expressions, or no regular expressions match, the most specific literal string match is used.

It is incumbent upon the administrator to ensure that possible location directives will match for every resource nginx is to provide for a given domain within the server block. Additionally, while nginx’s configuration parser is capable of reading nested location blocks, using location directives in this manner is not a supported behavior.

Location Configuration Link

Once nginx has selected a location to provide the resource for a given request, the response to this request is determined by the contents of the location directive block. Allow us to consider the following basic configuration block:

File excerpt: nginx.conf location directive

location / {
    root   html;
    index  index.html index.htm;
}

In this example the document root is located in the html/ directory. Given the default installation prefix for nginx, the full path to this location is /opt/nginx/http/. A request for the resource located at /blog/includes/style.css and assuming no other location directives matched, would serve the resource located on the file system at /opt/nginx/html/blog/includes/style.css. If you like, you can specify absolute paths for the root directive.

The index directive tells nginx which resource on the file system should be used if the request does not include a file name. Therefore, in this case the request for http://.ducklington.org/ would be fulfilled by the resource located at /opt/nginx/http/index.html. If multiple files are specified for the index directive, nginx will process the list in order and fulfill the request with the first file that exists. If index.html doesn’t exist in the relevant directory, index.htm would be used. If neither exist, a 404 message would be sent.

Allow us to consider additional examples of location directives from the serving websites with nginx and Perl-FastCGI guide for a server responding for the domain ducklington.org:

File excerpt: nginx.conf location directive

location / {
    root   /srv/www/ducklington.org/public_html;
    index  index.html index.htm;
}

location ~ \.pl$ {
    gzip off;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass  127.0.0.1:8999;
    fastcgi_index index.pl;
    fastcgi_param  SCRIPT_FILENAME  /srv/www/ducklington.org/public_html$fastcgi_script_name;
}

In this example, all requests for resources that end in a .pl extension are handled by the second location block, which specifies a fastcgi handler for these requests. Otherwise, nginx uses the first location directive. Resources are located on the file system at /srv/www/ducklington.org/public_html/. If no file name is specified in the request, nginx will look for and provide the index.html and index.htm files in turn. If no index files are found, the server will return a 404 error. Consider the following requests:

  • The request for http://ducklington.org/ will return the file located at /srv/www/ducklington.org/public_html/index.html if it exists. If that file doesn’t exist, it will fulfill /srv/www/ducklington.org/public_html/index.htm. If neither exists, nginx returns a 404 error.
  • The request for http://ducklington.org/blog/ will return the file located at /srv/www/ducklington.org/public_html/blog/index.html if it exists. If that file doesn’t exist, it will fulfill /srv/www/ducklington.org/public_html/blog/index.htm. If neither exists, nginx returns a 404 error.
  • The request for http://ducklington.org/tasks.pl will use the FastCGI handler to execute the file located at /srv/www/ducklington.org/public_html/tasks.pl and return the result.
  • The request for http://ducklington.org/squire/roster.pl will use the FastCGI handler execute the file located at /srv/www/ducklington.org/public_html/squire/roster.pl and return the result.

Reference:

nginx.conf.zip

http://nginx.org/en/docs/http/request_processing.html

http://library.linode.com/web-servers/nginx/configuration/basic#location_configuration

http://articles.slicehost.com/2009/3/5/ubuntu-intrepid-nginx-configuration

http://blog.rackcorp.com/?p=31

1 Response to “Nginx configuration – nginx.conf”

  1. 30 November, 2011, 10:56

    Nice Post…

    [...]Nginx configuration – nginx.conf | UKoom[...]…

Leave a Reply

You can leave a response, or trackback from your own site.