【问题标题】:Page shown properly, but Nginx logs shows "File not found"页面显示正常,但 Nginx 日志显示“找不到文件”
【发布时间】:2012-08-05 02:46:06
【问题描述】:

我正在使用 Nginx 和 Codeigniter 以及 php5-fpm。一切“似乎”都可以正常工作,显示页面并且一切看起来都很棒。当然这不是我在这里问这个问题的原因,我实际上有一个问题。

我面临的问题是页面抛出 404 错误,即使页面正确呈现,我仍然收到 404(在日志中)。

我得到 404 的原因(在查看 Nginx 的错误日志之后)是 Nginx 无法打开我请求的文件,因为 Nginx 尝试直接打开 PHP 文件,而不是引用 @ 987654321@ 不幸的是 Codeigniter 就是这样工作的。

例如:
请求http://website.com/<controller>/<function>/<other_params>导致Nginx日志中出现404,这背后的原因是open()因为指定的目录不存在而无法打开,而不是引用controller/method

一些重要的日志:
Nginx 错误日志:

[error] 4172#0: *482 open() "/var/www/<domain>/public/site/section/main" failed
(2: No such file or directory), client: <client_ip>, server: <domain>, request: "GET   
/site/section/main HTTP/1.1", host: "<domain>"  

正如我之前所说,Nginx 试图直接访问文件,而不是让 Codeigniter 处理它。

我的sites-enabled/ci 配置:

server
{
    server_name <domain> *.<domain>;

    access_log /var/www/<domain>/access.log;
    error_log /var/www/<domain>/error.log;
    root /var/www/<domain>/public;

    index index.php index.html index.htm;


   location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
    access_log        off;
    log_not_found     off;
    expires           360d;
}
    # enforce www (exclude certain subdomains)
#   if ($host !~* ^(www|subdomain))
#   {
#       rewrite ^/(.*)$ $scheme://www.$host/$1 permanent;
#   }

    # enforce NO www
    if ($host ~* ^www\.(.*))
    {
        set $host_without_www $1;
        rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
    }

    # canonicalize codeigniter url end points
    # if your default controller is something other than "welcome" you should change the following
    if ($request_uri ~* ^(/site(/index)?|/index(.php)?)/?$)
    {
        rewrite ^(.*)$ / permanent;
    }

    # removes trailing "index" from all controllers
    if ($request_uri ~* index/?$)
    {
        rewrite ^/(.*)/index/?$ /$1 permanent;
    }

    # removes trailing slashes (prevents SEO duplicate content issues)
   # if (!-d $request_filename)
   # {
   #     rewrite ^/(.+)/$ /$1 permanent;
   # }

    # removes access to "system" folder, also allows a "System.php" controller
    if ($request_uri ~* ^/(system|application))
    {
        rewrite ^/(.*)$ /index.php?/$1 last;
        break;
    }

    # unless the request is for a valid file (image, js, css, etc.), send to bootstrap
    #if (!-e $request_filename)
    #{
     #   rewrite ^/(.*)$ /index.php?/$1 last;
     #   break;
    #}

    # catch all
    error_page 404 /index.php;

    # use fastcgi for all php files
      location ~ \.php($|/)
    {
     #if (!-e $request_filename) {
           #         return 404;
           # }
        fastcgi_pass 127.0.0.1:9000;
    #fastcgi_pass php5-fpm-sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
#      location / {
    #       try_files $uri $uri/ @codeigniter;
    #}

  # location @codeigniter {
  #         rewrite ^(.*) /index.php?$1 last;
  #  }


    # deny access to apache .htaccess files
    location ~ /\.ht
    {
        deny all;
    }
}    

那么,Nginx 和 Codeigniter 之间的这种误解背后的原因是什么?
提前致谢。

【问题讨论】:

    标签: php codeigniter url-rewriting nginx webserver


    【解决方案1】:

    您的配置中似乎没有规则告诉 nginx 在找不到文件时尝试向 php 发送请求。有一个带有try_files@codeigniter 的注释块几乎看起来像它。理论上这就是你想让 nginx 做的事情:

    1. 检查 url 是否存在,如果有的话。
    2. 将其他所有内容发送到 codeigniter 并让其解决。

    为此,这两个块应该足够了:

    location / {
        # Check if a file exists, or route it to index.php.
        try_files $uri $uri/ /index.php;
    }
    
    location ~ \.php$ {     
       # for security, see http://forum.nginx.org/read.php?2,88845,page=3
       try_files $uri =404;
       fastcgi_split_path_info ^(.+\.php)(.*)$;
    
       fastcgi_pass 127.0.0.1:9000;
       fastcgi_index index.php;
       include fastcgi_params;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    

    另一个if 保护块不应与这些就位中断。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-09
      • 2019-09-20
      • 1970-01-01
      • 2020-11-08
      相关资源
      最近更新 更多