1.Nginx Rewrite 基本標記(flags)
last 相當於Apache里的[L]標記,表示完成rewrite
break 本條規則匹配完成之后,終止匹配,不再匹配后面的規則。
redirect 返回302臨時重定向 地址欄會顯示跳轉后的地址
permanent 返回301永久重定向 地址欄會顯示跳轉后的地址
2、正則表達式:
1)變量名,錯誤的值包括:空字符串“”,或者任何以0開始的字符串。
(2)變量比較可以使用“=”和“!=”(等於和不等於)運算符
(3)正則表達式模式匹配可以使用“”和“*”符號
(4)~ 為區分大小寫匹配
(5)~* 為不區分大小寫匹配
文件以及目錄匹配:
(6)!和!*分別為區分大小寫不匹配及不區分大小寫不匹配
(7)-f和!-f用來判斷是否存在文件
(8)-d和!-d用來判斷是否存在目錄
(9)-e和!-e用來判斷是否存在文件或目錄
(10)-x和!-x用來判斷文件是否可執行:
3、案例:
3.1)需要將網站以https形式訪問
server {
listen 80;
server_name www.xxx.com;
rewrite ^(.*)$ https://$host$1 permanent;
}
小提示:百度是通過index.html刷新網頁,更巧妙一些。
```html
<html>
<meta http-equiv="refresh" content="0;url=https://baidu.com/">
</html>
3.2)Nginx Redirect將所有xxx.com與abc.xxx.com域名全部自跳www.xxx.com
server {
listen 80;
server_name xxx.com abc.xxx.com;
index index.html index.php;
root /var/InfiNET/web/;
if ($http_host !~ "^www\.xxx\.com$") {
rewrite ^(.*) [url]http://www.xxx.com$1 redirect;
}
}
3.3)如果虛擬站點只允許https訪問時,用http訪問時nginx會報出497錯誤碼,用戶習慣用http訪問,后面通過497狀態碼讓它自動跳到443端口
```bash
server {
listen x.x.x.x:443; #ssl端口
listen x.x.x.x:80;
server_name xxx.com;
ssl on;
#指定PEM格式的證書文件
ssl_certificate /xxx/xxx.pem;
#指定PEM格式的私鑰文件
ssl_certificate_key /xx/xxx.key;
#讓http請求重定向到https請求
error_page 497 https://$host$uri?$args;
}
3.4)location匹配查詢資源
示例 1:
location = / {
# matches the query / only.
# 只匹配 / 查詢。
}
匹配任何查詢,因為所有請求都已 / 開頭。但是正則表達式規則和長的塊規則將被優先和查詢匹配
示例 2:
location ^~ /images/ {
# matches any query beginning with /images/ and halts searching,
# so regular expressions will not be checked.
# 匹配任何已 /images/ 開頭的任何查詢並且停止搜索。任何正則表達式將不會被測試。
}
示例 3:
location ~* \.(gif|jpg|jpeg)$ {
# matches any request ending in gif, jpg, or jpeg. However, all
# requests to the /images/ directory will be handled by
}
3.4.1) 匹配任何已 gif、jpg 或 jpeg 結尾的請求。
location ~ .(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
3.4.2)匹配.php代理到后端
location ~ .*.PHP?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
3.5) Nginx exprires 緩存
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 12h;
}
# 根據文件類型
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
root /html/web/bbs;
expires 1d;
break;
}
}
#根據目錄類型
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /html/web;
expires 30d;
}
3.6)nginx防盜鏈
#Preventing hot linking of images and other file types
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
valid_referers none blocked server_names *.chinarenservice.com http://localhost baidu.com;
if ($invalid_referer) {
rewrite ^/ [img]http://www.xxx.com/images/default/logo.gif[/img];
# return 403;
}
}
3.7)Nginx禁止訪問下載某類型的文件
3.7.1)Nginx 下禁止訪問*.txt 文件,配置方法如下.代碼:
```bash
location ~* \.(txt|doc)$ {
if (-f $request_filename) {
root /html/test;
break;
}
}
3.7.2)禁止訪問某個目錄
location ~ ^/(tomcat)/ {
deny all;
}
3.7.3)禁止下載以點開頭的文件:如 .freeke;.dat;.exe
location ~ /\..+ {
deny all;
}
本文出自 “永不放棄!任志遠” 博客,請務必保留此出處http://renzhiyuan.blog.51cto.com/10433137/1898091