Nginx重寫規(guī)則(2)
發(fā)布時(shí)間:2020-04-15 點(diǎn)擊數(shù):7012
需求:研發(fā)提出了一些路由跳躍的要求。
解決問題:在特殊情況下,如果業(yè)務(wù)和研發(fā)無法快速處理,則需要運(yùn)維快速跳轉(zhuǎn)來解決臨時(shí)問題并避免出現(xiàn)較大的漏洞。
案例一:
臨時(shí)將請求路徑/colorv1/getcolorv1 的請求轉(zhuǎn)發(fā)到 v2,且?guī)е鴨柼柡竺娴膮?shù) :
location ~ ^/(.)/(.)/colorv1/getcolorv1$ {
proxy_pass http://category-color-api/$1/$2/colorv2/getcolorv2?$args;
}
案例二:
任意開頭和結(jié)尾的uri,重定向至"http://test.jenkins.com/jenkins"
location / {
rewrite ^/(.*)$ http://test.jenkins.com/jenkins;
}
location /jenkins {
proxy_pass http://127.0.0.1:8080/jenkins;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for;
}
案例三
如果訪問的URL以.sh .bash 結(jié)尾,返回狀態(tài)碼403
location ~ .*.(sh|bash)?$ {
return 403;
}
案例四
nginx 多判斷條件語句如何實(shí)現(xiàn)? 比如,同時(shí)滿足2個(gè)條件的請求,才轉(zhuǎn)發(fā)。
nginx的配置中不支持if條件的邏輯中包含&& 或 || 的運(yùn)算 ,而且不支持if的嵌套語法;
否則會(huì)報(bào)錯(cuò)誤:nginx: [emerg] invalid condition; 對此情況我們可以用變量的方式來實(shí)現(xiàn)。
如: 請求到/color 的訪問,請求方法是post,且refer 為abc.com;返回200:且顯示內(nèi)容{"code": 1, "msg": "發(fā)布成功"}, 其他不滿足這兩個(gè)條件的請求,轉(zhuǎn)發(fā)到gateway;
location ~ /color$ {
set $post 0; ##定義變量
if ($request_method ~* "POST") {
set $post "${post}1";
}
if ($http_referer ~* "abc.com") {
set $post "${post}2";
}
if ($post = "012") {
return 200 '{"code": 1, "msg": "發(fā)布成功"}';
}
proxy_pass http://$gateway/a-b-api$request_uri;
}
案例五
禁止Scrapy等工具和UA為空的抓取
if ($http_user_agent ~* (Scrapy|Curl|HttpClient|^$)) {
return 403;
}
附:nginx配置完成之后,curl常用驗(yàn)證方法
當(dāng)我們做了一些條件的限制,在本地curl驗(yàn)證通過方可上線:
-H "referer:abc" // 設(shè)置referer
-A "okhttp/3.12.0" // 設(shè)置請求 http_user_agent
-X POST //請求方法post
-d ‘{}’ //請求參數(shù)
curl -H "referer:" -A "okhttp/3.12.0" -X POST -d '{"user": "admin", "passwd":"123456
解決問題:在特殊情況下,如果業(yè)務(wù)和研發(fā)無法快速處理,則需要運(yùn)維快速跳轉(zhuǎn)來解決臨時(shí)問題并避免出現(xiàn)較大的漏洞。

臨時(shí)將請求路徑/colorv1/getcolorv1 的請求轉(zhuǎn)發(fā)到 v2,且?guī)е鴨柼柡竺娴膮?shù) :
location ~ ^/(.)/(.)/colorv1/getcolorv1$ {
proxy_pass http://category-color-api/$1/$2/colorv2/getcolorv2?$args;
}
案例二:
任意開頭和結(jié)尾的uri,重定向至"http://test.jenkins.com/jenkins"
location / {
rewrite ^/(.*)$ http://test.jenkins.com/jenkins;
}
location /jenkins {
proxy_pass http://127.0.0.1:8080/jenkins;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for;
}
案例三
如果訪問的URL以.sh .bash 結(jié)尾,返回狀態(tài)碼403
location ~ .*.(sh|bash)?$ {
return 403;
}
案例四
nginx 多判斷條件語句如何實(shí)現(xiàn)? 比如,同時(shí)滿足2個(gè)條件的請求,才轉(zhuǎn)發(fā)。
nginx的配置中不支持if條件的邏輯中包含&& 或 || 的運(yùn)算 ,而且不支持if的嵌套語法;
否則會(huì)報(bào)錯(cuò)誤:nginx: [emerg] invalid condition; 對此情況我們可以用變量的方式來實(shí)現(xiàn)。
如: 請求到/color 的訪問,請求方法是post,且refer 為abc.com;返回200:且顯示內(nèi)容{"code": 1, "msg": "發(fā)布成功"}, 其他不滿足這兩個(gè)條件的請求,轉(zhuǎn)發(fā)到gateway;
location ~ /color$ {
set $post 0; ##定義變量
if ($request_method ~* "POST") {
set $post "${post}1";
}
if ($http_referer ~* "abc.com") {
set $post "${post}2";
}
if ($post = "012") {
return 200 '{"code": 1, "msg": "發(fā)布成功"}';
}
proxy_pass http://$gateway/a-b-api$request_uri;
}
案例五
禁止Scrapy等工具和UA為空的抓取
if ($http_user_agent ~* (Scrapy|Curl|HttpClient|^$)) {
return 403;
}
附:nginx配置完成之后,curl常用驗(yàn)證方法
當(dāng)我們做了一些條件的限制,在本地curl驗(yàn)證通過方可上線:
-H "referer:abc" // 設(shè)置referer
-A "okhttp/3.12.0" // 設(shè)置請求 http_user_agent
-X POST //請求方法post
-d ‘{}’ //請求參數(shù)
curl -H "referer:" -A "okhttp/3.12.0" -X POST -d '{"user": "admin", "passwd":"123456
78"}' http://test.abc.com/1/7.3/color?client=064bfc8ch577e5f6
以上是云網(wǎng)時(shí)代小編關(guān)于Nginx重寫規(guī)則的分享,希望能對大家有所幫助,云網(wǎng)時(shí)代為大家提供專業(yè)的深圳服務(wù)器租用,深圳服務(wù)器托管,深圳主機(jī)租用,云服務(wù)器租用等服務(wù)器資源,更多詳情歡迎咨詢客服了解。