Nginx运行除index.php之外的所有php文件
因此,在我的服务器上,我有一个info.php文件,我可以很好地运行它,所有读数看起来都很好。我还可以运行其他几个文件,没有问题。buuuut,index.php作为包含所有php原始代码的文件发送给我,这非常糟糕。除此之外,它将转到基本页面,根本不提供单个文件,尤其是index.php 这是我的nginx配置:Nginx运行除index.php之外的所有php文件,php,nginx,Php,Nginx,因此,在我的服务器上,我有一个info.php文件,我可以很好地运行它,所有读数看起来都很好。我还可以运行其他几个文件,没有问题。buuuut,index.php作为包含所有php原始代码的文件发送给我,这非常糟糕。除此之外,它将转到基本页面,根本不提供单个文件,尤其是index.php 这是我的nginx配置: server { server_name somedamnserver; root /var/www; index index.php
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文件似乎设置正确,但我也可以将它们链接到这里
我在这里束手无策,我简直不明白为什么这个服务器恶霸会这样对我(这是由两个问题引起的 首先,当您执行
try_files$uri$uri//index.php;
时,由于index.php是最后一个元素,nginx会重新处理整个服务器块
其次,因为第一个位置块是与/index.php
匹配的/code>,所以第二个位置块被重新处理为原始文件
几乎可以肯定的是,您不想像以前那样编写块。您应该明确列出哪些是静态内容类型,这些类型将显示为原始文件并发送到服务器。其他所有内容都应传递到PHP后端。例如
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;
}
请注意,在最后一块中,有一个小把戏来保存查询字符串,您可能需要也可能不需要
顺便说一句,启用重写\u登录;
可能会帮助您解决类似的问题。研究日志后,我发现了实际问题所在,感谢Danack建议使用重写日志选项。
因此,问题主要是PHP依赖性,更准确地说,是在php5 json和php5 mysql中(不知道它们是如何卸载的),然后以mysql服务器设置问题结束,这显然意味着127.0.0.1和localhost不是一回事
显然,页面为空白或不存在的原因是,在我的安装中,页面缺失或只是空白
希望这能帮助其他人,我为此奋斗了一个小星期
更新:
再次感谢Danack,他基本上编写了此配置。这是我当前使用的工作配置,尽管不是很安全,但它可以工作。需要两种类型的文件类型解决方案,否则您将无法访问该静态内容。已启用自动Gzip,但建议对其添加限制,甚至列出它可以拉拉链
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;
}
}
遗憾的是,这并没有解决问题,但我会尝试重写日志,看看是否能从中得到有用的东西。