Ben
Ben

Reputation: 849

NGINX renders PHP files when given path, serves index as download without a path

My current NGINX & PHP-FPM setup works when given a working filepath like example.com/index.php, but still serves the index.php as a download when visiting example.com without a file.

I've tried adding a location / redirect and try_files, but it still doesn't redirect properly.

I know how it's done using .htaccess files, but I'd like to do this in NGINX to avoid changing every repository.

Here's my current conf file.

server {
    server_name example.com;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html;

    location / { 
        try_files $uri $uri/ =404;
    }   

    location ~ \.php$ {
        try_files $uri $uri/ =404;

        include fastcgi_params;
        fastcgi_pass php:9000;
        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }   
}

Upvotes: 0

Views: 300

Answers (1)

Danila Vershinin
Danila Vershinin

Reputation: 9845

Remove try_files $uri $uri/ =404; from location ~ \.php$ {.

Upvotes: 2

Related Questions