简体   繁体   中英

Nginx runs all php files, except index.php

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. :(

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. eg

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.

After studying the logs I found out what the actual problem was, thanks to Danack for suggesting the rewrite log option. So the issue was mainly PHP dependencies, as in php5-json and php5-mysql to be more precise (don't know how either of those got uninstalled) and then to end with a mysql server setup issue, that apparently means that 127.0.0.1 and localhost are not the same things.

Apparently the reason the page was blank or non-existent is because they 500 error page is either missing or just blank in my installation.

Hope this helps out other people, I struggled with this for a small week now.

Update: Again cudos to Danack who basically wrote this configuration. This is the working configuration I am currently using, albeit not very safe, it works. The two types of file type solutions are needed or else you won't be able to access that static content. Automatic gzipping is enabled, but it is advised to add restrictions to it and even outline which content it zips.

server {
        server_name somedamnserver;
        root /var/www;
        rewrite_log on;

        gzip_static on;
        # This is file type association solution 1
        # location ~ \.css {
            # add_header  Content-Type    text/css;
        # }
        # location ~ \.js {
            # add_header  Content-Type    application/x-javascript;
        # }
        # This is file type association solution 2
        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";
        }
        # This will make PHP files run as intended, I have uncommented a few options as I do not need them.
        location  / {
            #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_param PATH_TRANSLATED $document_root$fastcgi_path_info;
            #fastcgi_intercept_errors on;
            include fastcgi_params;
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM