如何設置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/

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

簡單且快速的 return

這是一個非常簡單的設置方式,只需要個return語句就可以了

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

 

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

一個完整的例子:

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可修改為redirect

一個完整的例子:

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標記則在本條規則匹配完成后,終止匹配。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM