Nginx "invalid number of arguments in "try_files" directive..." for PHP security

31,004

Solution 1

Nginx tries to explain that the try_files directive needs at least two paths:

    try_files /path1$uri /path2$uri ...

Use either /dev/null as a simple work-around:

    try_files $uri /dev/null =404;

Or a named location that allows for more customization:

    try_files $uri @error
    ...
    location @error {
        ...
    }

Solution 2

Not sure if this is your issue, but I've got my try_files outside the PHP location block:

location / {
    try_files $uri $uri/ =404;
}
location ~ \.php$ {
    ....
}
Share:
31,004
HittingSmoke
Author by

HittingSmoke

Updated on July 20, 2020

Comments

  • HittingSmoke
    HittingSmoke over 2 years

    I'm trying to get Nginx running from source in the user folder of my shared host with debian-style directory structure. I'm getting an error when I try to start the server up:

    [emerg] invalid number of arguments in "try_files" directive in /home/.../nginx/conf/sites-enabled/default:11
    

    The line referenced is the PHP execution protection from the Nginx pitfalls page. Here are my config files:

    nginx.conf:

    worker_processes 1;
    events {
        worker_connections 1024;
    }
    http {
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        client_max_body_size 5m;
        include /home/hittingsmoke/nginx/conf/mime.types;
        default_type application/octet-stream;
        gzip on;
        gzip_disable \"msie6\";
        include /home/hittingsmoke/nginx/conf/sites-enabled/*;
    }
    

    ...and sites-available/default:

    server {
        listen       12513;
        root /home/hittingsmoke/nginx/html/;
        index index.php index.html index.htm;
        server_name _;
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/home/hittingsmoke/php-5.3/var/run/php5-fpm.sock;
            fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_index index.php;
            include fastcgi_params;
        }
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    

    I can't find anything wrong with my configs. My setup is almost identical to a working installation on an Ubuntu box I'm running. What am I doing wrong?

    EDIT: Upon further testing, this only happens when I'm using a sites-available setup with an include in nginx.conf. If I copy/paste the contents of my sites-available/default into my nginx.conf everything works fine.

    EDIT2: As mentioned, if I removed try_files from the vhosts file it fails again with the same error on fastcgi_params. Here is the contents of my fastcgi_params file. It is all default:

    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_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  HTTPS              $https if_not_empty;
    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;
    # PHP only, required if PHP was built with --enable-force-cgi-redirect
    fastcgi_param  REDIRECT_STATUS    200;
    

    EDIT3: I made a slight mistake. It's fastcgi_param, not fastcgi_param*s* where the error contiunes after removing the try_files directive.