最近剛學ci框架,做了個簡單的項目,在本地搭伺服器的環境都調通了,但是部署到遠端伺服器時:
http://example.com/(index.php)/ 可以訪問(為配置的預設controller-class)
http://example.com/(index.php)/[controller-class]/[controller-method] 不可以存取(提示404錯誤!)
最後百度原因:
對於/index.php/abc這種url,Apache和Lighttpd會按」index.php?abc」來解釋,而nginx會認為是請求名字是「index.php」的目錄下的abc檔案的內容。所以CI在nginx下不配置rewrite是無法運作的,但在Apache和Lighttpd則正常。
解決方案(要點加粗,重點標紅):
<span> 1</span><span> server { </span><span> 2</span> listen 80<span>; </span><span> 3</span> server_name example.com<span>; </span><span> 4</span> root /data/wwwroot/example/<span> 5</span> index index.php index.html index.<span>htm; </span><span> 6</span><span> 7</span> location ~* \.(css|js|swf|htm|jpg|png|gif|json|atlas)?<span>$ { </span><span> 8</span><span> expires 1d; </span><span> 9</span> add_header Pragma <span>public</span><span>; </span><span>10</span> add_header Cache-Control "public"<span>; </span><span>11</span><span> } </span><span>12</span><span>13</span> location /<span>controller-class</span>/<span> { </span><span>14</span><span>if</span> (!-e <span>$request_filename</span><span>) { </span><span>15</span> rewrite ^/<span>controller-class</span>/(.*)$ /<span>controller-class</span>/index.php?q=<span>$uri</span>&<span>$args</span><span>; </span><span>16</span><span> } </span><span>17</span><span> } </span><span>18</span><span>19</span> location ~ \.<span>php$ { </span><span>20</span> fastcgi_pass 127.0.0.1:9000<span>; </span><span>21</span> fastcgi_index index.<span>php; </span><span>22</span> fastcgi_param SCRIPT_FILENAME <span>$document_root$fastcgi_script_name</span><span>; </span><span>23</span> fastcgi_param PHP_VALUE open_basedir=<span>$document_root</span>:/tmp/:/proc/<span>; </span><span>24</span><span>include</span><span> fastcgi_params; </span><span>25</span><span> } </span><span>26</span><span>27</span> }
參考資料:http://www.2cto.com/os/201301/185926.html4Nginx+CI問題】
以上就介紹了Nginx+CI出現404錯誤,包含了面向的內容,希望對PHP教學有興趣的朋友有幫助。