NGINX配置。带有PATHINFO 404的PHP框架

时间:2013-03-09 22:40:53

标签: frameworks nginx php pathinfo

我通常使用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;
}
}

3 个答案:

答案 0 :(得分:2)

我更喜欢使用以下nginx配置结构。它更干净:

location / {
  try_files $uri $uri/ @phpsite;
}

location @phpsite {
  include fastcgi_params;
  ... other fast_cgi directives
}

在流行的silex项目中可以找到更复杂的设置:http://silex.sensiolabs.org/doc/web_servers.html#nginx

我在原始配置文件中看到2个问题:

location ~ \.php$ {
    try_files $uri =404;
    ...
}
  1. 在正则表达式中,'$'表示在字符串末尾匹配。所以它失败了,如prodigitalson的评论所述。
  2. 上面的fast_cgi位置块中的try_files指令不应该存在,因为该位置块应该由php单独处理。删除该行更清晰。

答案 1 :(得分:0)

我认为你缺少的是像

这样的规则
location / {
    try_files $uri $uri/ /index.php?$args;
}

如果路径不存在,将尝试调用index.php url。

或许,如果你知道尝试其他事情毫无意义,那就

location / {
    try_files /index.php?$args;
}

location ~ /index.php {
    try_files /index.php?$args;
}

答案 2 :(得分:0)

这对我有用......

location ~ ^(.*?\.php)($|/.+) {
    try_files $1 =404;

    ... fastcgi conf...
}
相关问题