2

I've been Googling this for a while but can't seem to find a solution.

At the moment I have a config file setup on Nginx to send all requests regardless of file extension to a single index.php file. However, it ignores requests ending with .php and will throw a 404 if it's not there or, try to execute it if it is.

How can I configure Nginx to send .php requests to the index.php file too so I can use it to handle all file requests, not just non-PHP files?

My config file currently looks like the following:

server {
        listen 80;
        listen 443;

        ssl on;
        ssl_certificate /somecrt.crt;
        ssl_certificate_key /somekey.key;

        root /sites/;
        index index.php;

        server_name somesite.net;

        access_log /sites/logs/access.log;
        error_log /sites/logs/error.log;

        location ~ /\. { deny all; }

        location / {
                # First attempt to serve request as file, then
                # as directory then fall back to index.php
                try_files $uri $uri/ /index.php?$args;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

        location ~ \.php$ {
                try_files $uri /index.php?$args =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                # fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi_params;
        }

}
3
  • Try to remove $uri from try_files $uri /index.php?$args =404; (for php files), $uri is your PHP file, and you try to access it before index.php if file exists. Commented Nov 28, 2017 at 21:54
  • Do you have resource files used by the index.php file? I am not sure I understand why there would be other .php files within the document root. Commented Nov 28, 2017 at 22:42
  • @RichardSmith, there is always possibility of having other php in same folder, but I'm sure you know that by now, however for others who still want to know why, there might be a lib.php or config.php in same folder that are not for browsing. Commented Oct 19, 2020 at 11:47

1 Answer 1

4

After some more Googling "nginx: Map single static URL to a PHP file" helped me figure out the solution. So the new config is now:

server {
    listen 80;
    listen 443;

    ssl on;
    ssl_certificate /somecrt.crt;
    ssl_certificate_key /somekey.key;

    root /sites/;
    index index.php;

    server_name somesite.net;

    access_log /sites/logs/access.log;
    error_log /sites/logs/error.log;

    location ~ /\. { deny all; }

    location / {
            include fastcgi_params;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root/index.php;

    }

}

With this config, all requests will be sent to the single index.php.

Of course, this will include static files such as image files which will probably impact Nginx server performance. In that case, you might want to add another location block before it if you want to exclude certain kind of requests.

For example, to exclude jpgs and gifs:

   location ~ \.(jpg|gif) {
           try_files $uri =404;
   }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.