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.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100