nginx rewrite php的CI(CodeIgniter)架構

來源:互聯網
上載者:User

標籤:ci   rewrite   path_info   偽靜態   

一. 瞭解nginx rewrite:


Regex匹配,其中:

    * ~ 為區分大小寫匹配
    * ~* 為不區分大小寫匹配
    * !~和!~*分別為區分大小寫不匹配及不區分大小寫不匹配

檔案及目錄匹配,其中:

    * -f和!-f用來判斷是否存在檔案
    * -d和!-d用來判斷是否存在目錄
    * -e和!-e用來判斷是否存在檔案或目錄
    * -x和!-x用來判斷檔案是否可執行

flag標記有:

    * last 相當於Apache裡的[L]標記,表示完成rewrite
    * break 終止匹配, 不再匹配後面的規則
    * redirect 返回302臨時重新導向 地址欄會顯示跳轉後的地址
    * permanent 返回301永久重新導向 地址欄會顯示跳轉後的地址
一些可用的全域變數有,可以用做條件判斷(待補全)

    $args
    $content_length
    $content_type
    $document_root
    $document_uri
    $host
    $http_user_agent
    $http_cookie
    $limit_rate
    $request_body_file
    $request_method
    $remote_addr
    $remote_port
    $remote_user
    $request_filename
    $request_uri
    $query_string
    $scheme
    $server_protocol
    $server_addr
    $server_name
    $server_port
    $uri

執行個體:    
1.     檔案反盜鏈並設定到期時間
    這裡的return 412 為自訂的http狀態代碼,預設為403,方便找出正確的盜鏈的請求
    “rewrite ^/ http://http://374400.blog.51cto.com/addblog.jpg;” 顯示一張防盜鏈圖片
    “access_log off;”不記錄訪問日誌,減輕壓力
    “expires 3d”所有檔案3天的瀏覽器緩衝

        location ~* ^.+/.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
        valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194;
        if ($invalid_referer) {
            rewrite ^/ http://374400.blog.51cto.com/addblog.jpg;
            return 412;
            break;
        }
                         access_log   off;
                         root /opt/htdocs/web;
        expires 3d;
        break;
         }


2.     只充許固定ip訪問網站,並加上密碼
        root  /opt/htdocs/www;
        allow   208.97.167.194;
        allow   222.33.1.2;
        allow   231.152.49.4;
        deny    all;
        auth_basic "C1G_ADMIN";
        auth_basic_user_file htpasswd;

3.     檔案和目錄不存在的時候重新導向:
        if (!-e $request_filename) {
        proxy_pass http://127.0.0.1;
        }

二. nginx location 匹配順序

location 匹配的原型是這樣的:location [=|~|~*|^~|@] /uri/ { … }
    “=”是精確匹配
    “@”是命名的location ,在正常的location 匹配中不會使用,僅僅在內部跳轉中才會使用到。
    “~”是區分大小寫匹配
    “~*”是不區分大小寫匹配
    “^~”表示中止正則匹配(這個平時沒太注意)

匹配步驟:
    1. 帶”=”首碼的先進行匹配,如果找到了,中止尋找。
    2. 所有其它location 進行非正則的匹配,找到最精確匹配的那個,如果匹配到帶”^~”首碼的,則中止尋找。
    3. 正則尋找,按照我們設定檔中配置的location 順序進行尋找。
    4. 如果正則尋找匹配成功,則使用此正則匹配的location ,否則,使用第二步尋找的結果。

    這裡要特別說明下”=”與”^~”的區別:
    “=”在匹配時,則匹配帶”=”的location 。而”^~”,則會匹配所有非”=”的非正則location ,只有在確認它是最精確匹配的location 後,才生效。


三. CI架構偽靜態

    CI 在 APACHE .htaccess 檔案配置
        RewriteEngine on  
        RewriteCond $1 !^(index\.php|system\.php|images|js|swfupload|robots\.txt)  
        RewriteRule ^(.*)$ /wj/index.php/$1 [L]
        註: RewriteRule ^(.*)$ /wj/index.php/$1 [L]裡的wj是你的CI程式目錄

    Nginx伺服器偽靜態設定首先需要設定nginx開啟 path_info,nginx模式預設是不支援path_info模式的

    1. pathinfo,一種偽靜態用法,

        1.1、讓 Apache 支援 PathInfo(Apache 版本 : 2.2.13)
            在設定檔中加入
            <Files *.php>
            AcceptPathInfo On
            </Files>
            這樣 Apache 就可以支援針對 php 檔案的 PathInfo 了.

        1.2、pathinfo 模式 需要 php.ini 開啟下面這個參數

            cgi.fix_pathinfo=1

            path_info模式:http://www.xxx.com/index.php/模組/方法


        1.3、讓 Nginx 支援 PathInfo

        在設定檔裡添加

            location ~ \.php {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                set $path_info "";
                set $real_script_name $fastcgi_script_name;
                if ($fastcgi_script_name ~ “^(.+?\.php)(/.+)$”) {
                    set $real_script_name $1;
                    set $path_info $2;
                }
            }
            fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
            fastcgi_param SCRIPT_NAME $real_script_name;
            fastcgi_param PATH_INFO $path_info;
            include conf/fastcgi.conf;
            }
            要點:
            1.~ \.php 後面不能有$  以便能匹配所有 *.php/* 形式的url
            2. 通過設定更改 SCRIPT_FILENAME

            cat fastcgi.conf
                #fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name; #注釋掉的
                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_param  SCRIPT_NAME        $fastcgi_script_name;                #注釋掉的
                fastcgi_param  REQUEST_URI        $request_uri;
                fastcgi_param  DOCUMENT_URI       $document_uri;
                fastcgi_param  DOCUMENT_ROOT      $document_root;
                fastcgi_param  SERVER_PROTOCOL    $server_protocol;
                fastcgi_param  HTTPS              $https if_not_empty;

                fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
                fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

                fastcgi_param  REMOTE_ADDR        $remote_addr;
                fastcgi_param  REMOTE_PORT        $remote_port;
                fastcgi_param  SERVER_ADDR        $server_addr;
                fastcgi_param  SERVER_PORT        $server_port;
                fastcgi_param  SERVER_NAME        $server_name;

                # PHP only, required if PHP was built with --enable-force-cgi-redirect
                fastcgi_param  REDIRECT_STATUS    200;

        2. CI rewrite 配置
            2.1    
                location / {
                        index  index.php;
                        if (!-e $request_filename) {
                        rewrite  ^/(.*)$  /index.php/$1  last;
                        break;
                        }
                }

            2.2
                假設我們的子目錄名稱為 wj
                        location /wj/ {
                        root   /var/www/html/;
                        index index.html index.htm index.php;
                        if ($request_filename !~* (index\.php|system\.php|images|js|swfupload|robots\.txt)) {
                            rewrite ^/(.*)$ /index.php?$1 last;
                        }

            另外附上主目錄偽靜態規則
                    #rewrite ^/$ /index.php last;
                    #一下是防止某些檔案夾被直接存取
                    #rewrite ^/(?!index\.php|images|robots\.txt|js|css|upimg|artDialog|style)(.*)$ /index.php/$1 last;


本文出自 “willard_SA” 部落格,請務必保留此出處http://374400.blog.51cto.com/364400/1590134

nginx rewrite php的CI(CodeIgniter)架構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.