讓 nginx 支持thinkphp 的 PATH_INFO 和 URL Rewrite模式支持

情況簡介:

   我們的網站是thinkphp框架開發的,之前一直在apache+php下面運行.很想換到nginx上試試,經過百般折騰無果.無奈去官方發現是nginx不支持thinkphp的 PATH_INFO 和 URL Rewrite模式,不過還好官方也給出了相應的解決辦法。下面就是實驗的過程全記錄。


實驗環境介紹:

   系統 : centos 6.4 x86_64

   ip   : 192.168.80.141

   環境 : lnmp

   版本 : nginx-1.4.1 , PHP-5.4.0 ,mysql-5.1.62 ,thinkphp 3.0


至於lnmp 環境的搭建在這裏就不多說了,網上很多. 下面在介紹一下我的目錄存放的介紹。這個地方要仔細看 因爲跟nginx 裏面的配置有很多的聯繫。


  nginx :  /usr/local/nginx/

  php   :  /usr/local/php/

  網站根目錄: /usr/local/nginx/html/


------------------開始配置nginx支持thinkphp----------------------------

原始配置內容:

user  nobody;
worker_processes  1;
error_log  logs/error.log  info;
pid        logs/nginx.pid;
events {
    use epoll;
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.php index.html index.htm;
        }
        location /status {
            stub_status on;
            access_log   on;
            allow 192.168.80.0/24;
        }
                                                                                                                                                      
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

-----------------------------------------------------------------------------------

修改後支持thinkphp的內容:

user  nobody;
worker_processes  1;
error_log  logs/error.log  info;
pid        logs/nginx.pid;
events {
    use epoll;
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
                                                                                                                              
    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types       text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.php index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php {                   #去掉後面的$
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
             fastcgi_split_path_info ^(.+\.php)(.*)$;                             #增加這一句
             fastcgi_param PATH_INFO $fastcgi_path_info;                          #還有這一句
            #####fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;  # 這個是在配置nginx+php整合的時候就改好的$前面的是網站的主目錄
            include        fastcgi_params;
        }
        if (!-e $request_filename)
            {
            #地址作爲將參數rewrite到index.php上。
             rewrite ^/(.*)$ /index.php/$1;
            #   若是子目錄則使用下面這句,將subdir改成目錄名稱即可。
            # rewrite ^/subdir/(.*)$ /subdir/index.php/$1;
            #    }
            }
    }
server {
        listen       80;
        server_name  s.abc.org;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location /status {
            stub_status on;
            access_log   on;
            allow 192.168.80.0/24;
        }
    }
}

-----------------至此nginx已經支持了thinkphp---------------------------------

有個小的問題,nginx的狀態查看頁面。即:http://IP/status 在配置好了支持thinkphp之後,竟然無法正常工作了,所以我把它單獨建立了一個虛擬機來解決。主機名s.abc.org

錯誤的status

再來個搞定之後的測試截圖:

--------------------祝各位早日搞定----------------------------------

     


   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章