文章目錄
- 一、nginx常用的轉發規則
- location 指令說明
- location轉發使用
- 二、upstream負載均衡使用
- 三、server_name使用
- 四、其他常用配置
- 限制請求類型
- 處理靜態資源目錄遍歷問題
- 限制客戶端使用的ip或者域名
- 五、需要注意的地方
- location /api1 探討
- location ~ /api1 探討(正則表達式)
- $host 和 $remote_addr 的區別
- 其他
- Rewrite命令語法
- springboot 打印請求路徑
- springboot打印controller被調用的方法
- Controller獲取請求頭的內容
- 參考文檔
一、nginx常用的轉發規則
location 指令說明
指令 | 說明 |
---|
= | 用于不含正則表達式的 uri 前,要求請求字符串與 uri 嚴格匹配,如果匹配 成功,就停止繼續向下搜索并立即處理該請求。 |
~ | 用于表示 uri 包含正則表達式,并且區分大小寫。 |
~* | 用于表示 uri 包含正則表達式,并且不區分大小寫。 |
^~ | 用于不含正則表達式的uri前,要求Nginx服務器找到標識uri和請求字 符串匹配度最高的 location 后,立即使用此 location 處理請求,而不再使用 location 塊中的正則 uri 和請求字符串做匹配。 |
!~ | 區分大小寫不匹配。 |
!~* | 不區分大小寫不匹配 |
/a | 普通前綴匹配,優先級低于帶參數前綴匹配。 |
/ | 任何請求都會匹配 |
- 首先匹配 =
- 其次匹配^~,
- 其次是按文件中順序的正則匹配
- 最后是交給 / 通用匹配
- 當有匹配成功時候,停止匹配,按當前匹配規則處理請求。
location轉發使用
location /api1/ {proxy_pass http://localhost:8001/;}
location = /api1/ {proxy_pass http://localhost:8001/;}
- location ~ /api1 (正則表達式匹配)
location ~ /api1 { rewrite ^/api1/(.*)$ /$1 break;proxy_set_header test001 $host:$server_port;proxy_set_header test002 $remote_addr;proxy_pass http://localhost:8001;}
二、upstream負載均衡使用
upstream api{server localhost:9001;server localhost:9002;server localhost:9003;}
location /api/ { proxy_pass http://api/;}
三、server_name使用
- 看下列代碼,端口一樣,server_name不一樣
- 訪問http://www.test001.com/api/test,進入第一個server,轉發的實際為http://localhost:9001/test
- 訪問http://www.test002.com/api/test,進入第二個server,轉發的實際為http://localhost:9002/test
- 對于沒有配置的server_name,默認進入第一個server處理
- 訪問http://127.0.0.1/api/test,進入第一個server,轉發的實際為http://localhost:9001/test
- 訪問http://www.test003.com/api/test,進入第一個server,轉發的實際為http://localhost:9001/test
127.0.0.1 www.test001.com
127.0.0.1 www.test002.com
127.0.0.1 www.test003.com
server { listen 80; server_name www.test001.com; location / {root html;index index.html index.htm;}location /api/ { proxy_pass http://localhost:9001/;}error_page 500 502 503 504 /50x.html;location = /50x.html { root html;}}server { listen 80; server_name www.test002.com; location / {root html;index index.html index.htm;}location /api/ { proxy_pass http://localhost:9002/;}error_page 500 502 503 504 /50x.html;location = /50x.html { root html;}}
四、其他常用配置
限制請求類型
if($request_method !~ ^(GET|POST)$ ){return 403;
}
處理靜態資源目錄遍歷問題
if( $request_uri ~* \.\.[\\\\/] ){return 404;
}
限制客戶端使用的ip或者域名
if ( $host !~ ^127.0.0.1 ){return 500;
}
五、需要注意的地方
- 當使用 location ~ 的時候, proxy_pass結尾不能為 / ,不然會報錯
- access_log需要寫在log_format后面,不然啟動會報錯。
- access_log只能打印出請求的路徑,無法打印出代理之后的路徑。
location /api1 探討
location /api1 {proxy_pass http://localhost:8001;}
location /api1/ {proxy_pass http://localhost:8001/;}
location /api1 {proxy_pass http://localhost:8001/;}
location ~ /api1 探討(正則表達式)
location ~ /api1 { proxy_pass http://localhost:9001;}
$host 和 $remote_addr 的區別
- $host 是客戶端使用的ip或者域名,$remote_addr是客戶端真正的ip
http://127.0.0.1/api/test
http://www.test001.com/api/test
http://localhost/api/test
https://www.baidu.com/s?wd=北京
其他
Rewrite命令語法
rewrite < regex > < replacement > [flag]
regex:正則表達式
replacement :跳轉后的內容
flag:rewrite支持的flag標記
flag標記說明 | |
---|
標記 | 說明 |
last | 相當于Apache的【L】標記,表示完成rewrite |
break | 本條規則匹配完成即終止,不在匹配后面的任何規則 |
redirect | 返回302臨時重定向,瀏覽器地址欄會顯示跳轉后的URL地址,爬蟲不會更新url |
permanent | 返回301永久重定向,瀏覽器地址欄會顯示跳轉后的URL地址,爬蟲更新url |
ast和break比較 | | |
---|
| last | break |
使用場景 | 一般寫在server和if中 | 一般使用在location中 |
URL匹配 | 不重質重寫后的url匹配 | 終止重寫后的url匹配 |
rewrite ^/api1/(.*)$ /$1 break;
rewrite ^/(.*)/(.*)$ /$1 break;
rewrite ^/apitest/(.*)$ /$1 break;
springboot 打印請求路徑
logging:level:org.springframework: debug
springboot打印controller被調用的方法
logging:level:org:springframework:web:servlet:mvc:method:annotation:RequestMappingHandlerMapping: trace
Controller獲取請求頭的內容
HttpServletRequest request;
Enumeration<String> enumeration= request.getHeaderNames();
參考文檔
- springboot 打印請求的uri和請求參數
- Servlet–HttpServletRequest獲取請求信息(請求頭、請求行、參數)詳解
- Nginx配置文件
- Nginx之正則表達式、location匹配簡介以及rewrite重寫
- nginx 正則路徑匹配
- Nginx的基本使用