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

I am trying to run Nginx from the source code in the user folder of my shared host with a debian-style directory structure. I get an error when trying to start the server:

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

The link to which it refers is to protect PHP execution from the Nginx trap page. Here are my configuration 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 not find anything wrong with my configurations. My setup is almost identical to the working installation in the Ubuntu box in which I am running. What am I doing wrong?

EDIT: with further testing, this only happens when I use the installation available for sites with inclusion in nginx.conf. If I copy / paste the contents of my sites - available / by default to my nginx.conf, everything works fine.

EDIT2: As already mentioned, if I deleted the try_files from the vhosts file, it again crashes with the same error on fastcgi_params. Here is the contents of my fastcgi_params file. It is by 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 small mistake. This is fastcgi_param, not fastcgi_param * s *, where the contiunes error after removing the try_files directive.

+6
source share
2 answers

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

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

Use /dev/null as an easy way:

  try_files $uri /dev/null =404; 

Or a named location that allows you to perform additional configuration:

  try_files $uri @error ... location @error { ... } 
+11
source

Not sure if this is your problem, but I have my try_files outside the PHP location block:

 location / { try_files $uri $uri/ =404; } location ~ \.php$ { .... } 
+2
source

Source: https://habr.com/ru/post/948255/


All Articles