Nginx Location 語法,與簡單配置

一、介紹Nginx是俄羅斯人編寫的十分輕量級的HTTP服務器,Nginx,它的發音爲“engine X”, 是一個高性能的HTTP和反向代理服務器,同時也是一個IMAP/POP3/SMTP 代理服務器.
二、Location語法語法:location [=|~|~*|^~] /uri/ { … }
注:
1、~   爲區分大小寫匹配
2、~* 爲不區分大小寫匹配
3、!~和!~*分別爲區分大小寫不匹配及不區分大小寫不匹配
示例一:
location  / { }
匹配任何查詢,因爲所有請求都以 / 開頭。但是正則表達式規則將被優先和查詢匹配。
示例二:
location =/ {}
僅僅匹配/
示例三:
location ~* \.(gif|jpg|jpeg)$ {
rewrite \.(gif|jpg)$ /logo.png;

注:不區分大小寫匹配任何以gif,jpg,jpeg結尾的文件
三、ReWrite語法
last - 基本上都用這個Flag。
break - 中止Rewirte,不在繼續匹配
redirect - 返回臨時重定向的HTTP狀態302
permanent - 返回永久重定向的HTTP狀態301
1、下面是可以用來判斷的表達式:
-f和!-f用來判斷是否存在文件
-d和!-d用來判斷是否存在目錄
-e和!-e用來判斷是否存在文件或目錄
-x和!-x用來判斷文件是否可執行
2、下面是可以用作判斷的全局變量
例:http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\nginx/html/test1/test2/test.php
四、Redirect語法
    server {
    listen 80;
    server_name start.igrow.cn;
    index index.html index.php;
    root html;
    if ($http_host !~ "^www\.itlearner\.com$ {
         rewrite ^(.*) http://www.itlearner.com$1 redirect;
    }
    }
五、防盜鏈location ~* \.(gif|jpg|swf)$ {
  valid_referers none blocked start.igrow.cn sta.igrow.cn;
  if ($invalid_referer) {
  rewrite ^/ http://$host/logo.png;
  }
}
六、根據文件類型設置過期時間
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
   expires    1h;
   break;
}
}
七、禁止訪問某個目錄
location ~* \.(txt|doc)${
  root /data/www/wwwroot/linuxtone/test;
deny all;
}

++ 一些可用的全局變量
$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

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