dshm8998473 2012-08-08 02:42
浏览 92

页面显示正确,但Nginx日志显示“找不到文件”

I'm using Nginx and Codeigniter alongside php5-fpm. Everything "seems" to work just fine, pages are shown and everything looks great. Ofcource this isn't the reason I'm asking this question here, I actually have a problem.

The problem I'm facing is that 404 error is thrown with page, even though the page is rendered properly, I'm stilling getting 404's(in the logs).

The reason why I'm getting 404's (after looking at Nginx's error logs), is that Nginx can't open the file I'm requesting, because Nginx tries to open the PHP files directly, instead of referring to the controller/method and unfortunately Codeigniter works that way.

For example:
requesting http://website.com/<controller>/<function>/<other_params> results in 404 in Nginx logs, the reason behind this is that open() can't open the specified directory because it doesn't exists, instead of referring to the controller/method.

Some important logs:
Nginx error log:

[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>"  

as I said before, Nginx is trying to access the file directly, instead of making Codeigniter deal with it.

my sites-enabled/ci configurations:

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

So, what's the reason behind this misunderstanding between Nginx and Codeigniter?
Thanks in advance.

  • 写回答

1条回答 默认 最新

  • duandeng1824 2012-08-08 06:30
    关注

    It seems like there's no rule in your config that tells nginx to try sending requests to php when the file is not found. There's a commented out block with try_files and @codeigniter that almost look like it. In theory this is what you want nginx do:

    1. Check if the url exists, if does serve it.
    2. Send everything else to codeigniter and let it sort out.

    For this, these two blocks should be enough:

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

    The other if guarded block shouldn't break with these in place.

    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用