1

I want Nginx to load only index.php on every URL coming in

example.com/urlb?id=1 example.com/urlc?id=2 example.com/urld?id=3 my /etc/nginx/sites-available/default looks as the following -

server {
    listen 80;
    listen [::]:80;
root /var/www/html;
index index.php index.html index.htm;

server_name html;



location ~ \$ {
    try_files /index.php$is_args$args;
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix://var/run/php/php7.3-fpm.sock;
}

}

I want for all the URL above or any URL in the feature to always load index.php

Mat Cn
  • 23

2 Answers2

1
  1. Location you used will match only the requests containing $ symbol, that's definitely not what you want.
  2. Using the try_files directive you must specify at least one file/folder to check. The easiest way to specify the file that would fail the existence check is to specify dev/null.

So your location should look like

    location / {
        try_files /dev/null /index.php$is_args$args;
        include fastcgi.conf;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass unix://var/run/php/php7.3-fpm.sock;
    }

or, without the try_files directive

    location / {
        include fastcgi.conf;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
        fastcgi_pass unix://var/run/php/php7.3-fpm.sock;
    }
Ivan Shatsky
  • 2,866
  • @MichaelHampton No, this check would fail, I already used this solution in my configurations. Found this recipe here. – Ivan Shatsky Jun 22 '20 at 05:17
  • this solution give an error when trying to restart my Nginx Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details. – Mat Cn Jun 22 '20 at 05:25
  • this is the code now `server { listen 80; listen [::]:80;
    root /var/www/html;
    index index.php index.html index.htm;
    
    server_name html;
    
    
    
     location / {
         try_files /dev/null /index.php$is_args$args;
         include snippets/fastcgi-php.conf;
         fastcgi_pass unix://var/run/php/php7.3-fpm.sock;
     }
    

    }`

    – Mat Cn Jun 22 '20 at 05:26
  • @MatCn What nginx -t says? – Ivan Shatsky Jun 22 '20 at 05:27
  • nginx: [emerg] "try_files" directive is duplicate in /etc/nginx/snippets/fastcgi-php.conf:5 nginx: configuration file /etc/nginx/nginx.conf test failed – Mat Cn Jun 22 '20 at 05:28
  • @MatCn I see the extra slash in fastcgi_pass directive, try fastcgi_pass unix:/var/run/php/php7.3-fpm.sock; – Ivan Shatsky Jun 22 '20 at 05:28
  • same error nginx: [emerg] "try_files" directive is duplicate in /etc/nginx/snippets/fastcgi-php.conf:5 nginx: configuration file /etc/nginx/nginx.conf test failed – Mat Cn Jun 22 '20 at 05:30
  • @MatCn What is the contents of /etc/nginx/snippets/fastcgi-php.conf file? – Ivan Shatsky Jun 22 '20 at 05:38
  • `# regex to split $uri to $fastcgi_script_name and $fastcgi_path fastcgi_split_path_info ^(.+?.php)(/.*)$;

    Check that the PHP script exists before passing it

    try_files $fastcgi_script_name =404;

    Bypass the fact that try_files resets $fastcgi_path_info

    see: http://trac.nginx.org/nginx/ticket/321

    set $path_info $fastcgi_path_info; fastcgi_param PATH_INFO $path_info;

    fastcgi_index index.php; include fastcgi.conf; `

    – Mat Cn Jun 22 '20 at 05:39
  • @MatCn Check the updated answer, both variants shouls work. – Ivan Shatsky Jun 22 '20 at 05:48
  • @MatCn Please always add additional information into the question by editing it. The information is difficult to read in comments. – Tero Kilkanen Jun 22 '20 at 06:39
  • @IvanShatsky The answer you referred was about a different situation, where people wanted to use only a named location. In this case, there is a physical file index.php and error code, which can be implemented with try_files like my answer shows. – Tero Kilkanen Jun 22 '20 at 06:41
1

Try the following location block instead:

location / {
    try_files /index.php$is_args$args =404;
include snippets/fastcgi-php.conf;
fastcgi_pass unix://var/run/php/php7.3-fpm.sock;

}

This satisfies the requirement of try_files needing at least two arguments. =404 is the fallback to return 404 Not Found response when index.php does not exist.

Tero Kilkanen
  • 37,584