Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx runs all php files, except index.php

Tags:

php

nginx

So on my server I have an info.php file, which I can run fine and all readout looks good. I can also run several other files, no questions asked. Buuuuut, index.php gets sent to me as a file containing all the PHP raw code, which is very bad. On top of this going to the base page, doesn't serv up a single file at all, especially not index.php.

Here is my nginx config:

server {
        server_name somedamnserver;
        root /var/www;
        index index.php index.html;

        location / {
                # This is cool because no php is touched for static content
                try_files $uri $uri/ /index.php;
        }

        location ~ \.php$ {
                #fastcgi_split_path_info ^(.+\.php)(/.+)$;
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                #fastcgi_intercept_errors on;
                include fastcgi_params;
        }
}

My php.ini and fpm/pool.d/www.conf files seem to be set up correctly, but I can also link them here.

I am at my wits end here, I can simply not understand why this bully of a server would do this to little me. :(


1 Answers

It's being caused by two issues.

First, when you do try_files $uri $uri/ /index.php;because index.php is the last element, nginx reprocesses the whole server block.

Second, because the first location block is / which matches /index.php, on the second reprocessed as a raw file.

You almost certainly don't want to write your blocks like you have done. You should be explicitly listing what are your static content types, which get displayed as raw files to the server. Everything else should be passed to the PHP backend. e.g.

location ~* ^[^\?\&]+\.(html|jpg|jpeg|json|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|svg|woff|ttf)$ {
    try_files $uri /index.php?file=$1;

    #access_log off;
    expires 24h;
    add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}


location  / {
            #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
            set $originalURI  $uri;
            try_files $uri /index.php /50x_static.html;
            fastcgi_param  QUERY_STRING  q=$originalURI&$query_string;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            #fastcgi_intercept_errors on;
            include fastcgi_params;
 } 

Please note in the last block, there is a little jiggery pokery to preserve the query string, which you may or may not need.

btw enabling rewrite_log on; may help you fix similar issues like these.

like image 195
Danack Avatar answered Mar 04 '26 17:03

Danack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!