Understanding location blocks and try files

AD7six nginx-forum at nginx.us
Wed Jan 22 19:54:35 UTC 2014

Thanks for the reply,

I've read through those sections again - if I'm missing something obvious
I'm afraid I need someone to point it out to me :|

Sorry about that I thought pointing at a working example would allow close
scrutiny - didn't think to remove the files/config that weren't in use. Only
the mentioned location block is relevant, inlining that and the fastcgi
config becomes [1]:

server {
    listen 80;
    server_name nginx.dev *.nginx.dev;

    access_log /tmp/access.log;
    error_log  /tmp/error.log debug;

    error_page 404 /404.html;

    root /etc/nginx/www;
    index index.php index.html index.htm;

    try_files $uri $uri.html /index.php?$args;

    send_timeout 600s;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_param   QUERY_STRING        $query_string;
        fastcgi_param   REQUEST_METHOD      $request_method;
        fastcgi_param   CONTENT_TYPE        $content_type;
        fastcgi_param   CONTENT_LENGTH      $content_length;

        fastcgi_param   SCRIPT_FILENAME     $request_filename;
        fastcgi_param   SCRIPT_NAME     $fastcgi_script_name;
        fastcgi_param   REQUEST_URI     $request_uri;
        fastcgi_param   DOCUMENT_URI        $document_uri;
        fastcgi_param   DOCUMENT_ROOT       $document_root;
        fastcgi_param   SERVER_PROTOCOL     $server_protocol;

        fastcgi_param   GATEWAY_INTERFACE   CGI/1.1;
        fastcgi_param   SERVER_SOFTWARE     nginx/$nginx_version;

        fastcgi_param   REMOTE_ADDR     $remote_addr;
        fastcgi_param   REMOTE_PORT     $remote_port;
        fastcgi_param   SERVER_ADDR     $server_addr;
        fastcgi_param   SERVER_PORT     $server_port;
        fastcgi_param   SERVER_NAME     $server_name;

        fastcgi_param   HTTPS           $https if_not_empty;

        # PHP only, required if PHP was built with
        fastcgi_param   REDIRECT_STATUS     200;
        fastcgi_pass unix:/tmp/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_intercept_errors on; # to support 404s for PHP files not
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_read_timeout 600s;

    location ~* \.(?:manifest|appcache|html?|xml|json)$ {
      add_header section "expires.conf:13";
      expires -1;

The behavior is identical to as originally described.

A valid response where the url is a file:

$ curl -i http://nginx.dev/foo.json
HTTP/1.1 200 OK
Server: nginx/1.4.4
Date: Wed, 22 Jan 2014 19:28:34 GMT
Content-Type: application/json
Content-Length: 20
Last-Modified: Wed, 22 Jan 2014 18:01:51 GMT
Connection: keep-alive
ETag: "52e0078f-14"
Expires: Wed, 22 Jan 2014 19:28:33 GMT
Cache-Control: no-cache
section: expires.conf:13
Accept-Ranges: bytes

An invalid response when passed to php:

$ curl -i http://nginx.dev/apples.json
HTTP/1.1 404 Not Found
Server: nginx/1.4.4
Date: Wed, 22 Jan 2014 19:28:40 GMT
Content-Type: text/html
Content-Length: 8
Connection: keep-alive
ETag: "52dffeed-8"


Am I missing something obvious or falling for a common misunderstanding?
Is there a way to define location blocks for static files - without that
causing problems for dynamic requests for the same url pattern?




Posted at Nginx Forum: http://forum.nginx.org/read.php?2,246713,246718#msg-246718

More information about the nginx mailing list