CodeIgniter在nginx下的配置

今天在把ci項目放到服務器上的時候,死活路由不到頁面,要麼是404,要麼都是welcome頁面,很煩。到網上搜到一大堆方法,大多不全面或者很差使,有的乾脆就一行代碼,我也是看不懂。剛剛弄了好幾個小時,總算路由到頁面了。這裏mark一下。服務器環境php

Debian 9.0 64位
nginx 1.13.5
PHP/7.1.10
CodeIgniter 3.1.6

nginx 報404錯誤的緣由

緣由是默認Nginx不支持pathinfo這種格式,當你瀏覽器裏輸入http:xxx.xxx.comindex.phppageshome的時候,Nginx會認爲你要訪問index.php目錄下的pages文件夾裏的home,因此會報404 not found錯誤。html

解決方法

解決方法就是修改nginx.conf文件,下面是個人配置:nginx

location / {
            root   html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.php;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /$document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

主要就是加了一行 try_files $uri $uri/ /index.php;具體的原理我也不是很懂,大體就是重定向之類的,懶得走進科學,反正我知道添加這一行,就好用了。
而後修改php支持pathinfo ,找到php的php.ini文件(可能在php安裝目錄的etc目錄也可能在lib文件夾下,看本身的配置),搜索:cgi.fix_pathinfo
將註釋放開,並置爲1:cgi.fix_pathinfo=1
而後在CI的application 下的config/config.conf文件裏修改三個參數:web

$config['base_url'] = 'http://1.abc.com/';
$config['index_page'] = '';
$config['uri_protocol']    = 'REQUEST_URI';

這三個參數比較關鍵,其中第一個是web根目錄對應的域名 ,index_page要爲」,不要爲默認值 ‘index.php’.
通過以上設置就ok了,url地址裏不須要寫index.php了。瀏覽器

然而在個人項目裏,nginx路由雖然不報錯了,可是ci仍是報錯404,最後發現是文件名大小寫的問題,真的很坑。
控制器的文件名稱不能小寫。服務器

捕獲.PNG

捕獲ff.PNG

參考連接app

相關文章
相關標籤/搜索