【问题标题】:NGINX configuration. PHP frameworks with PATHINFO 404NGINX 配置。带有 PATHINFO 404 的 PHP 框架
【发布时间】: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 设置的链接,我认为你提到的大多数其他框架也是如此......你查阅过文档吗?此外,您的主要问题是您在 locationend 中专门寻找 .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


【解决方案1】:

我更喜欢使用以下 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 在 cmets 中所述,它失败了。
  2. 上面 fast_cgi 位置块中的 try_files 指令不应该存在,因为该位置块应该由 php 单独处理。去掉那条线会更干净。

【讨论】:

  • 我删除了 $ 并且它起作用了。问题是为什么当 $ 存在时它不起作用?文件应该以“.php”结尾吗?
  • 匹配是针对整个 url,例如 index.php/welcome/index。这里最后一个字符串是索引,而不是 php
  • 有道理,谢谢。所以我们说基本上匹配任何“路径”和“.php”,对吗?
  • 我实际上浏览了另一个网站,发现罪魁祸首是 $.我赞成你的,以便它帮助其他人。
【解决方案2】:

我认为你缺少的是这样的规则

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

【讨论】:

    【解决方案3】:

    这对我有用...

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-11
      • 2019-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      相关资源
      最近更新 更多