首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NGINX配置。使用PATHINFO 404的PHP框架

NGINX配置。使用PATHINFO 404的PHP框架
EN

Stack Overflow用户
提问于 2013-03-10 06:40:53
回答 3查看 3.8K关注 0票数 3

我通常使用apache,并想尝试一下NGINX。

我已经在我的ubuntu dev机器上安装了它,并设置了一些不同的框架和站点,并且正在开发中(codeigniter,symfony,laravel等)。

我得到的问题是,只有以.php结尾的路径才能工作。如果我尝试index.php/welcome/index它只需要404,而不是加载index.php。

我尝试将cgi.fix_pathinfo设置为1和0。

这是我当前的(许多尝试过的)站点配置。

代码语言:javascript
复制
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;
}
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-03-11 00:49:02

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

代码语言:javascript
复制
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

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

代码语言:javascript
复制
location ~ \.php$ {
    try_files $uri =404;
    ...
}

正则表达式中的

  1. '$‘表示字符串末尾的匹配。因此,正如prodigitalson的评论中所述,它失败了。
  2. 上面的位置块中的try_files指令不应该在那里,因为这个位置块应该是由fast_cgi单独处理的。去掉这一行会更干净。
票数 2
EN

Stack Overflow用户

发布于 2013-03-10 06:53:00

我想你漏掉的是一条规则

代码语言:javascript
复制
location / {
    try_files $uri $uri/ /index.php?$args;
}

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

或者,如果您知道尝试其他事情是没有意义的,那么就

代码语言:javascript
复制
location / {
    try_files /index.php?$args;
}

代码语言:javascript
复制
location ~ /index.php {
    try_files /index.php?$args;
}
票数 0
EN

Stack Overflow用户

发布于 2016-05-17 04:38:46

这对我很有效..。

代码语言:javascript
复制
location ~ ^(.*?\.php)($|/.+) {
    try_files $1 =404;

    ... fastcgi conf...
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15316742

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档