如何設置nginx重定向

原文: https://skyfi.github.io/2018/09/21/%E5%A6%82%E4%BD%95%E8%AE%BE%E7%BD%AEnginx%E9%87%8D%E5%AE%9A%E5%90%91/css

nginx 是一個靈活且高效的網絡服務器,若是想要在nginx服務器中重定,你能夠從下面挑選一個適合的方式。html

簡單且快速的 return

這是一個很是簡單的設置方式,只須要個return語句就能夠了nginx

1
return 301 https://example.com$request_uri;

 

你須要把這段代碼放到nginx配置文件的server代碼塊中,301是永久重定向,你也能夠設置成302作一個臨時重定向(不建議)。git

一個完整的例子:github

1
2
3
4
5
6
server {
listen 80;
listen [::]:80;
hostname example.com www.example.com;
return 301 https://example.com$request_uri;
}

 

正則表達式 rewrite

若是return不能知足你的複雜業務需求,你能夠考慮下正則匹配重定向:正則表達式

1
rewrite ^/foo/(bar)/(.*)$ https://$server_name/$1/$2 permanent;

 

一樣這也是須要在server代碼塊中,其中permanent301永久跳轉,若須要302可修改成redirectapache

一個完整的例子:centos

1
2
3
4
5
6
7
server {
listen 80;
listen [::]:80;
hostname example.com www.example.com;
root /var/www/example.com/public;
rewrite ^/foo/(bar)/(.*)$ $scheme://$server_name/$1/$2 permanent;
}

 

又如:瀏覽器

1
2
3
4
5
6
7
server {
listen 80;
server_name www.fangyongle.com fangyongle.cn;
if ($host != 'www.fangyongle.com' ) {
rewrite ^/(.*)$ https://www.fangyongle.com/$1 permanent;
}
}

 

再如:服務器

1
2
3
4
5
6
7
# 根據文件類型設置過時時間
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
expires 1h;
break;
}
}

 

使用Maps

若是你有一堆須要重定向的鏈接映射,你能夠考慮在一個地方定義它,而後再經過if來手動判斷重定向。

首先定義重定向連接映射redirect-map.conf

1
2
3
4
5
map $request_uri $redirect_uri {
/about.html /about-us;
/customers.html /our-customers;
/products.html /our-products;
}

 

而後在server代碼塊使用:

1
2
3
4
5
6
7
include redirect-map.conf;
server {
[…]
if ( $redirect_uri ) {
return 301 $redirect_uri;
}
}

 

映射也能夠有一些語法:

1
2
3
4
5
6
7
8
9
10
11
12
map $request_uri $redirect_uri {
/about.html /about-us;
/customers.html /our-customers;
/products.html /our-products;
# Match any url that ends in products.html or producs.htm
~products\.html?$ /our-products;
# case-insensitive version of the above
~*products\.html?$ /our-products;
# A named capture that maps
# e.g. product-1234.html into /products/item-1234/overview
~product-(?<sku>\d+)\.html /products/item-$sku/overview;
}

 

一些實用的重定向例子

http 重定向爲 https

1
return 301 https://$host$request_uri;

統一規範域名

1
2
3
4
server_name example.com www.example.com example.net www.example.net _;
if ( $host != $server_name ) {
return 301 $scheme://$server_name$request_uri;
}

含 www 和 不含 www 之間的重定向

1
2
3
4
# non-www to www
if ( $host !~ ^www\. ) {
return 301 $scheme://www.$host$request_uri;
}
1
2
3
4
# www to non-www
if ( $host ~ ^www\.(?<domain>.+)$ ) {
return 301 $scheme://$domain$request_uri;
}

附錄

重定向中經常使用全局變量

1
2
3
4
5
$scheme		       // HTTP方法(如http,https),如:http
$host // 請求主機頭字段,不然爲服務器名稱,如:blog.fangyongle.com
$server_name // 服務器名稱,如:blog.fangyongle.com
$request_uri // 包含請求參數的原始URI,不包含主機名,如:/2018/81.html?a=1&b=2
$request_filename // 當前請求的文件的路徑名,由root或alias和URI request組合而成,如:/2013/81.html

nginx 部分經常使用全局變量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
$remote_addr		//獲取客戶端ip
$binary_remote_addr //客戶端ip(二進制)
$remote_port //客戶端port,如:50472
$remote_user //已經通過Auth Basic Module驗證的用戶名
$host //請求主機頭字段,不然爲服務器名稱,如:blog.fangyongle.com
$request //用戶請求信息,如:GET ?a=1&b=2 HTTP/1.1
$request_filename //當前請求的文件的路徑名,由root或alias和URI request組合而成,如:/2013/81.html
$status //請求的響應狀態碼,如:200
$body_bytes_sent // 響應時送出的body字節數數量。即便鏈接中斷,這個數據也是精確的,如:40
$content_length // 等於請求行的「Content_Length」的值
$content_type // 等於請求行的「Content_Type」的值
$http_referer // 引用地址
$http_user_agent // 客戶端agent信息,如:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36
$args //與$query_string相同 等於當中URL的參數(GET),如a=1&b=2
$document_uri //與$uri相同 這個變量指當前的請求URI,不包括任何參數(見$args) 如:/2018/81.html
$document_root //針對當前請求的根路徑設置值
$hostname //如:centos53.localdomain
$http_cookie //客戶端cookie信息
$cookie_COOKIE //cookie COOKIE變量的值
$is_args //若是有$args參數,這個變量等於」?」,不然等於」",空值,如?
$limit_rate //這個變量能夠限制鏈接速率,0表示不限速
$query_string // 與$args相同 等於當中URL的參數(GET),如a=1&b=2
$request_body // 記錄POST過來的數據信息
$request_body_file //客戶端請求主體信息的臨時文件名
$request_method //客戶端請求的動做,一般爲GET或POST,如:GET
$request_uri //包含請求參數的原始URI,不包含主機名,如:/2018/81.html?a=1&b=2
$scheme //HTTP方法(如http,https),如:http
$uri //這個變量指當前的請求URI,不包括任何參數(見$args) 如:/2018/81.html
$request_completion //若是請求結束,設置爲OK. 當請求未結束或若是該請求不是請求鏈串的最後一個時,爲空(Empty),如:OK
$server_protocol //請求使用的協議,一般是HTTP/1.0或HTTP/1.1,如:HTTP/1.1
$server_addr //服務器IP地址,在完成一次系統調用後能夠肯定這個值
$server_name //服務器名稱,如:blog.fangyongle.com
$server_port //請求到達服務器的端口號,如:80

Rewrite正則相關指令詳解

nginx的rewrite至關於apache的rewriterule(大多數狀況下能夠把原有apache的rewrite規則加上引號就能夠直接使用),它能夠用在server,locationIF條件判斷塊中,命令格式以下:

1
rewrite <regex> <replacement> <flag>

 

正則表達式匹配

  • ~爲區分大小寫匹配
  • ~*爲不區分大小寫匹配
  • !~!~*分別爲區分大小寫不匹配及不區分大小寫不匹配

文件及目錄匹配判斷

  • -f!-f用來判斷是否存在文件
  • -d!-d用來判斷是否存在目錄
  • -e!-e用來判斷是否存在文件或目錄
  • -x!-x用來判斷文件是否可執行

flag標記

  • last - 基本上都用這個Flag。
  • break - 停止rewirte,不在繼續匹配
  • redirect - 返回臨時重定向的HTTP狀態302
  • permanent - 返回永久重定向的HTTP狀態301

使用lastbreak實現URI重寫,瀏覽器地址欄不變。並且二者有細微差異:

  • 使用alias指令必須用last標記
  • 使用proxy_pass指令時,須要使用break標記
  • last標記在本條rewrite規則執行完畢後,會對其所在server{......}標籤從新發起請求,而break標記則在本條規則匹配完成後,終止匹配。
相關文章
相關標籤/搜索