Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

nginx

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.

like image 828
HittingSmoke Avatar asked Jun 27 '13 17:06

HittingSmoke


People also ask

What is the try_FILES directive in Nginx?

The try_files directive commonly uses the $uri variable, which represents the part of the URL after the domain name. In the following example, NGINX serves a default GIF file if the file requested by the client doesn’t exist.

What happens if I disable Nginx's server_name_in_redirect?

If disabled, redirects issued by nginx will be relative. See also server_name_in_redirect and port_in_redirect directives. This directive appeared in version 0.8.11.

What is the “parameter value” directive in Nginx?

Parameter value can contain variables (1.17.0). This directive appeared in versions 1.1.0 and 1.0.6. Controls how nginx closes client connections. The default value “ on ” instructs nginx to wait for and process additional data from a client before fully closing a connection, but only if heuristics suggests that a client may be sending more data.

Are there any Nginx user guides that are wrong?

Not all guides out there are wrong, but a scary number of them are. These docs were created and reviewed by community members that work directly with all types of NGINX users. This specific document exists only because of the volume of common and recurring issues that community members see.


2 Answers

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 {
        ...
    }
like image 76
Per Cederberg Avatar answered Oct 20 '22 00:10

Per Cederberg


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$ {
    ....
}
like image 36
Alex Howansky Avatar answered Oct 19 '22 22:10

Alex Howansky