2014-02-23 130 views
0

我不知道這個問題之前已經回答了多少次,但我所看到的每個答案都給出瞭解決這些問題的方法,但都沒有解決這些問題。我正在從Apache遷移到Nginx,並且在設置時面臨一些嚴重問題。我的/ etc/nginx/sites-available/default看起來像這樣...Codeigniter nginx 404錯誤

server { 
    #listen 80; ## listen for ipv4; this line is default and implied 
    #listen [::]:80 default ipv6only=on; ## listen for ipv6 

    root /usr/share/nginx/www/flo2go/; 
    index index.php index.html index.htm; 
     if ($request_filename !~ (js|css|images|robots\.txt|index\.php.*)) { 
      rewrite ^/(.*)$ /index.php/$1 last; 
     } 

    # Make site accessible from http://localhost/ 
    server_name localhost; 

    location/{ 
     # First attempt to serve request as file, then 
     # as directory, then fall back to index.html 

       try_files $uri $uri/ /index.php; 
     # Uncomment to enable naxsi on this location 
     # include /etc/nginx/naxsi.rules 
    } 
location ~ /index.php/ 
    { 
    include /etc/nginx/fastcgi_params; 
    fastcgi_index index.php; 
    fastcgi_param SCRIPT_FILENAME /usr/share/nginx/www/flo2go/index.php; 
    fastcgi_param REQUEST_URI  $request_uri; 
    fastcgi_param QUERY_STRING  $query_string; 
    fastcgi_param REQUEST_METHOD $request_method; 
    fastcgi_param CONTENT_TYPE  $content_type; 
    fastcgi_param CONTENT_LENGTH $content_length; 
    fastcgi_pass unix:/var/run/php5-fpm.sock; 
    } 
    location /doc/ { 
     alias /usr/share/doc/; 
     autoindex on; 
     allow 127.0.0.1; 
     deny all; 
    } 

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
    # 
    location ~ \.php$ { 
     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
    # # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini 
    # 
    # # With php5-cgi alone: 
    # fastcgi_pass 127.0.0.1:9000; 
    # # With php5-fpm: 
     fastcgi_pass unix:/var/run/php5-fpm.sock; 
     fastcgi_index index.php; 
     include fastcgi_params; 
    } 

} 

我嘗試了一切,以使我的web應用程序工作。我一直收到的是404:找不到頁面錯誤。該網站在Apache上完美運行,花費了近3至4小時的時間解決了這個問題後,我認爲最好在此論壇上徵求專家意見。希望有人能保釋我擺脫這種局面:(

+0

爲什麼2個位置爲php?刪除'location〜/ index.php /',如果你想'location/doc'沒用,請重新加載nginx並重試 –

回答

5

您的具體情況正確的配置必須尋找simular這樣:

server { 
    server_name yoursitename.com; 
    root /usr/share/nginx/www/flo2go/; 
    index index.php index.html index.htm; 
    location/{ 
     try_files $uri $uri/ /index.php; 
    } 

    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ { 
     expires   15d; 
    } 

    location ~ \.php$ { 
     include /etc/nginx/fastcgi_params; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME /usr/share/nginx/www/flo2go/index.php; 
     fastcgi_param REQUEST_URI  $request_uri; 
     fastcgi_param QUERY_STRING  $query_string; 
     fastcgi_param REQUEST_METHOD $request_method; 
     fastcgi_param CONTENT_TYPE  $content_type; 
     fastcgi_param CONTENT_LENGTH $content_length; 
     fastcgi_pass unix:/var/run/php5-fpm.sock; 
    } 
} 

與當前配置的主要問題是2個.PHP location塊和if這是evil

+0

謝謝,上面的配置保存了我的一天。我使用PHP 7 fpm,nginx和codeigniter。 –

5

請加上下面幾行到你的配置

if (!-e $request_filename) { 
     rewrite ^.*$ /index.php last; 
} 

它爲我工作

+0

這也適用於我。我知道這不是最好的辦法,但它會工作,直到我可以找出如何不使用if語句 – AJMaxwell

1

檢查您的命名控制器,視圖,模型和數據庫。

,如果你在你的控制器做到這一點:

$this->load->view('main'); 

那麼,你在你的控制器中寫道視圖的文件名必須是相同的:

main.php 
1

nginx的是更挑剔文件的情況下,名稱比apache。

我的控制器被稱爲Proto,並在一個名爲proto.php的文件中。我將它重命名爲Proto.php,並開始工作。

Nginx對控制器文件名的情況非常敏感。

+0

你有招數!乾杯! –