【发布时间】:2013-02-25 08:09:52
【问题描述】:
我一般用apache,想试试NGINX。
我已将它安装在我的 ubuntu 开发机器上,并设置和开发了几个不同的框架和站点(codeigniter、symfony、laravel 等)。
我遇到的问题是只有以.php 结尾的路径才有效。如果我尝试 index.php/welcome/index 它只是 404s 而不是加载 index.php。
我已经尝试将 cgi.fix_pathinfo 设置为 1 和 0。
这是我当前(许多尝试过的)站点配置。
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6
root /my/path;
index index.php index.html;
# Make site accessible from http://localhost/
server_name localhost;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/nginx/www;
#}
location ~ \.php$ {
try_files $uri =404;
# Fix for server variables that behave differently under nginx/php-fpm than typically expected
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Include the standard fastcgi_params file included with nginx
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_index index.php;
# Override the SCRIPT_FILENAME variable set by fastcgi_params
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Pass to upstream PHP-FPM; This must match whatever you name your upstream connection
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
【问题讨论】:
-
我知道 symfony 有一个示例 nginx 设置的链接,我认为你提到的大多数其他框架也是如此......你查阅过文档吗?此外,您的主要问题是您在
location的 end 中专门寻找 .php -
显而易见:wiki.nginx.org/Codeigniter。你需要“try_files $uri $uri/ /index.php;”
-
@prodigitalson 谢谢,是 php$ 导致了问题。现在删除它,它工作正常。 galchen 这只适用于使用 index.php 的 codeigniter 和其他人(symfony 没有),并且还会将所有内容转发给 root,这对开发没有好处。
-
@Atrox1449:您应该使用 vhosts 或 nginx equiv
server,而不是使用根目录中的子文件夹......这很愚蠢。然后它可以让您在每个server的基础上设置不同的规则。 -
我的意思是为它自己的配置中的每个文件夹进行重定向。正如@prodigitalson 所说,每个服务器的规则,使用虚拟主机
标签: frameworks nginx php pathinfo