2012-08-08 46 views
0

我使用Nginx和Codeigniter以及php5-fpm。一切似乎「工作得很好,頁面顯示,一切都很好。這不是我在這裏問這個問題的原因,我確實有一個問題。頁面顯示正常,但Nginx日誌顯示「File not found」

我面臨的問題是404錯誤與頁面引發,即使頁面呈現正常,我仍然404(在日誌中)。

爲什麼我得到404的(之後看Nginx的錯誤日誌),是Nginx無法打開我請求的文件,因爲Nginx試圖直接打開PHP文件,而不是參考controller/method,不幸的是Codeigniter的工作方式。

例如:
請求http://website.com/<controller>/<function>/<other_params>導致404 Nginx的日誌,這背後的原因是,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的試圖直接訪問的,而不是使笨處理它的文件。

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的和笨之間的這種誤解背後的原因是什麼?
在此先感謝。

回答

2

似乎你的配置中沒有規則告訴nginx嘗試發送請求到php當文件沒有找到。有一個註釋塊try_files@codeigniter幾乎看起來像它。理論上這是你想要nginx做的:

  1. 檢查網址是否存在,如果確實提供它。
  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把守塊不應與這些突破。

相關問題