Nginx运行除index.php之外的所有php文件

时间:2014-02-26 17:34:17

标签: php nginx

所以在我的服务器上我有一个info.php文件,我可以正常运行,所有读数看起来都不错。我也可以运行其他几个文件,没有问题。 Buuuuut,index.php作为包含所有PHP原始代码的文件发送给我,这非常糟糕。除此之外,进入基页,根本不提供单个文件,特别是index.php。

这是我的nginx配置:

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;
        }
}

我的php.ini和fpm / pool.d / www.conf文件似乎设置正确,但我也可以在这里链接它们。

我在这里的智慧结束,我完全不明白为什么这个服务器的恶霸会对我这么做。 :(

2 个答案:

答案 0 :(得分:1)

这是由两个问题引起的。

首先,当你执行try_files $uri $uri/ /index.php;因为index.php是最后一个元素时,nginx会重新处理整个服务器块。

其次,因为第一个位置块是/,它与/index.php匹配,在第二个位置块重新处理为原始文件。

你几乎肯定不想像你那样写你的积木。您应该明确列出您的静态内容类型,它们将作为原始文件显示给服务器。其他所有内容都应该传递给PHP后端。 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;
 } 

请注意,在最后一个块中,有一个小的jiggery pokery来保存查询字符串,您可能需要也可能不需要。

btw启用rewrite_log on;可以帮助您修复类似的问题。

答案 1 :(得分:0)

在研究了日志后,我发现了实际问题是什么,感谢Danack建议重写日志选项。 所以问题主要是PHP依赖,如php5-json和php5-mysql更精确(不知道其中任何一个被卸载)然后以mysql服务器设置问题结束,这显然意味着127.0。 0.1和localhost不是一回事。

显然页面空白或不存在的原因是因为他们500错误页面丢失或者我的安装中只是空白。

希望这对其他人有所帮助,我现在已经为这一周努力了。

<强>更新 再次向Danack致敬,他们基本上写了这个配置。这是我目前正在使用的工作配置,虽然不是很安全,但它可以工作。需要两种类型的文件类型解决方案,否则您将无法访问该静态内容。启用了自动gzipping,但建议对其添加限制,甚至概述它拉链的内容。

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;
        }
}
相关问题