2014-02-23 106 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

你有招数!干杯! –