一. 瞭解nginx rewrite:php
正則表達式匹配,其中:
* ~ 爲區分大小寫匹配
* ~* 爲不區分大小寫匹配
* !~和!~*分別爲區分大小寫不匹配及不區分大小寫不匹配
文件及目錄匹配,其中:
* -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;
}
css
二. nginx location 匹配順序html
location 匹配的原型是這樣的:location [=|~|~*|^~|@] /uri/ { … }
「=」是精確匹配
「@」是命名的location ,在正常的location 匹配中不會使用,僅僅在內部跳轉中才會使用到。
「~」是區分大小寫的匹配
「~*」是不區分大小寫的匹配
「^~」表示停止正則匹配(這個平時沒太注意)
匹配步驟:
1. 帶」=」前綴的先進行匹配,若是找到了,停止查找。
2. 全部其它location 進行非正則的匹配,找到最精確匹配的那個,若是匹配到帶」^~」前綴的,則停止查找。
3. 正則查找,按照咱們配置文件中配置的location 順序進行查找。
4. 若是正則查找匹配成功,則使用此正則匹配的location ,不然,使用第二步查找的結果。
這裏要特別說明下」=」與」^~」的區別:
「=」在匹配時,則匹配帶」=」的location 。而」^~」,則會匹配全部非」=」的非正則location ,只有在確認它是最精確匹配的location 後,才生效。nginx
三. CI框架僞靜態web
CI 在 APACHE .htaccess 文件配置
RewriteEngine on
RewriteCond $1 !^(index\.php|system\.php|p_w_picpaths|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.一、讓 Apache 支持 PathInfo(Apache 版本 : 2.2.13)
在配置文件中加入
<Files *.php>
AcceptPathInfo On
</Files>
這樣 Apache 就能夠支持針對 php 文件的 PathInfo 了.
1.二、pathinfo 模式 須要 php.ini 開啓下面這個參數
cgi.fix_pathinfo=1
path_info模式:http://www.xxx.com/index.php/模塊/方法
1.三、讓 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|p_w_picpaths|js|swfupload|robots\.txt)) {
rewrite ^/(.*)$ /index.php?$1 last;
}
另外附上主目錄僞靜態規則
#rewrite ^/$ /index.php last;
#一下是防止某些文件夾被直接訪問
#rewrite ^/(?!index\.php|p_w_picpaths|robots\.txt|js|css|upimg|artDialog|style)(.*)$ /index.php/$1 last;正則表達式