由於CodeIgniter當初是設計在apache的,而apache對pathinfo是支援比較好的,所以一切都很nice。但當你把寫好的程式碼放到nginx上,傻眼了,可能出了CodeIgniter的welcom之外,其他都是404個錯誤。而我驚訝的發現,CodeIgniter的官方文件竟然對在Nginx上的配置隻字不提。而你百度」CodeIgniter Nginx 404」又能搜到一堆一堆的文章,奇葩的是幾乎每個文件的配置方法貌似還不大一樣。如果你搞好了還罷,搞不好就是配幾個晚上都搞不定,像我一樣。當你瀏覽器裡輸入http:xxx.xxx.comindex.phppageshome的時候,Nginx會認為你要訪問index.php目錄下的pages資料夾裡的home,所以會報404 not found錯誤。
解決方法
解決方法肯定是要修改伺服器的重定向規則,大概有兩種思路,一種是改nginx安裝目錄裡的nginx.conf文件,如果你用了虛擬主機的話就去nginx安裝目錄下的vhosts下找對應的*.conf更改即可。另外一個想法修改CI目錄下的.htaccess文件,請參閱:http://blog.csdn.net/freshlover/article/details/8977111
本文是第一種想法。在修改之前先來看下原始的conf檔:{ listen 80; server_name 1.abc.com 2.abc.com; root /a/domains/1.abc.com/public_html; index index.html index.htm index.shtml index.php; error_page 404 /404.html; #Custom rules Start #Custom rules End location = /500.html { root /usr/share/nginx/html; } location ~ \.php$ { fastcgi_pass unix:/dev/shm/php.sock; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; access_log /a/apps/nginx/logs/1.abc.com.access.log main; } location ~ /\.ht { deny all; } }1234567891011121314151617181920212223242512345678910111213141516171819202122232425PS:上面為了隱私,我把server_name改為1.abc.com了,這個對應自己的server_name.
下面介紹修改方法,在修改conf前切記將自己的conf,cp為conf.back備份一份。
下面為修改方法:
再修改conf之前,找到php的php.ini檔案(可能在php安裝目錄的etc目錄也可能在lib資料夾下,看自己的設定),搜索:cgi.fix_pathinfo
將註解放開,並置為1:cgi.fix_pathinfo=1
2,修改conf之前有個問題要明確,那就是CI的根目錄是不是web的root目錄,如果是的話,如下修改:
只需要增加如下即可:
在location ~ .php$之前增加一段:
if (!-e $request_filename) { rewrite ^.*$ /index.php last; }123123
這個意思是,如果你瀏覽器裡要存取的檔案不存在時,會自動路由至web根目錄下的index.php進行存取。
當然上面這段話也可以用下面的來代替:
location / { try_files $uri $uri/ /index.php; }123123完整的配置為:
server{ listen 80; server_name 1.sod90.com app.sod90.com; root /a/domains/1.sod90.com/public_html; index index.html index.htm index.shtml index.php; error_page 404 /404.html; #Custom rules Start #Custom rules End location = /500.html { root /usr/share/nginx/html; } #location / { # try_files $uri $uri/ /index.php; #} if (!-e $request_filename) { rewrite ^.*$ /city52/index.php last; } location ~ \.php$ { fastcgi_pass unix:/dev/shm/php.sock; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; access_log /a/apps/nginx/logs/app.sod90.com.access.log main; } location ~ /\.ht { deny all; } }123456789101112131415161718192021222324252627282930313233123456789101112131415161718192021222324252627282930313233
接著檢查CI的config檔裡以下三個參數:
$config['base_url'] = 'http://1.abc.com/';$config['index_page'] = '';$config[ 'uri_protocol'] = 'REQUEST_URI';123123
經過以上設定就ok了,url位址裡不需要寫index.php了。所有框架都放在根目錄下也不太好看。 index.php改為/xxx/index.php即可。但為了保險起見,base_url如後者比較好,然後url裡就不要再帶index.php了。連結。 sina.com.cn/s/blog_6ec2ae900101kbx9.html
location / { try_files $uri $uri/ /xxx/index.php; }123123
$config['base_url'] = 'http://1.abc.com/';11但是我的conf裡只用了下面一句:
$config['base_url'] = 'http://app.sod90.com/xxx/';11
但是我的conf裡只用了下面一句:
fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;123123