使用Nginx實(shí)現(xiàn)根據(jù) IP 匹配指定 URL
業(yè)務(wù)需求
業(yè)務(wù)和開(kāi)發(fā)同事需要我這邊做一條規(guī)則,所有訪問(wèn) ip 為非上海、廣州 office 外網(wǎng) ip,url 為http://test.com/fuck/index.html 的請(qǐng)求都跳轉(zhuǎn)到 http://test.com/index.html 。然后所有在上海和廣州 office 的外網(wǎng) IP 訪問(wèn) http://test.com/fuck/index.html 依然還是 http://test.com/fuck/index.html。這樣就可以在生產(chǎn)上做隔離,不影響其他用戶的服務(wù)。
注:因?yàn)槟壳吧a(chǎn)上的 Nginx 沒(méi)有做 lua 支持,所以就無(wú)法通過(guò)使用 lua 來(lái)實(shí)現(xiàn)該需求,也沒(méi)有安裝 geoip ,所以也無(wú)法用模塊來(lái)支持,只能原生的。
原始的 nginx 配置
upstream service_test {
server 127.0.0.1:8080;
}
server
{
listen 80;
server_name test.com;
index index.html index.php;
root /tmp/test.com;
error_page 404 http://test.com/404.html;
error_page 502 http://test.com/502.html;
error_page 500 http://test.com/500.html;
location ~* \.(gif|jpg|jpeg|png|css|js|ico|txt|svg|woff|ttf|eot)$
{
rewrite ^(.*)$ /static$1 break;
root /tmp/test.com; #
expires 1d;
}
location ~* \.(html|htm)$
{
rewrite ^(.*)$ /static$1 break;
roo /tmp/test.com; #
expires 900s;
}
location / {
proxy_pass http://service_test;
include /opt/conf/nginx/proxy.conf;
}
修改后的 Nginx 配置
upstream service_test {
server 127.0.0.1:8080;
}
server
{
listen 80;
server_name test.com;
index index.html index.php;
root /tmp/test.com;
error_page 404 http://test.com/404.html;
error_page 502 http://test.com/502.html;
error_page 500 http://test.com/500.html;
location ~* \.(gif|jpg|jpeg|png|css|js|ico|txt|svg|woff|ttf|eot)$
{
rewrite ^(.*)$ /static$1 break;
root /tmp/test.com; #
expires 1d;
}
location ~* \.(html|htm)$
{
rewrite ^(.*)$ /static$1 break;
roo /tmp/test.com; #
expires 900s;
}
set $flag 0;
if ($request_uri ~* "^/fuck/\w+\.html$") {
set $flag "${flag}1";
}
if ($remote_addr !~* "192.168.0.50|192.168.0.51|192.168.0.56") {
set $flag "${flag}2";
}
if ($flag = "012") {
rewrite ^ /index.html permanent;
}
location / {
proxy_pass http://service_test;
include /opt/conf/nginx/proxy.conf;
}
在實(shí)現(xiàn)需求的過(guò)程中出現(xiàn)的問(wèn)題
把 if 指令 和 proxy_pass 都放在 location 下面的話,if 指令里面的內(nèi)容不會(huì)執(zhí)行,只會(huì)執(zhí)行 proxy_pass。
location / {
if ($remote_addr !~* "192.168.0.50|192.168.0.51|192.168.0.56") {
rewrite ^ /index.html permanent;
}
proxy_pass http://service_test;
include /opt/conf/nginx/proxy.conf;
}
if 指令下面使用 proxy_pass 指令問(wèn)題
像下面這樣使用會(huì)報(bào)錯(cuò),錯(cuò)誤的方式:
if ($remote_addr ~* "192.168.0.50|192.168.0.51|192.168.0.56") {
proxy_pass http://test.com/fuck;
}
正確的方式:
if ($remote_addr ~* "192.168.0.50|192.168.0.51|192.168.0.56") {
proxy_pass http://test.com$request_uri;
}
或是
if ($remote_addr ~* "192.168.0.50|192.168.0.51|192.168.0.56") {
proxy_pass http://test.com;
}
如果你是直接另外啟動(dòng)一個(gè) location 的話,比如啟動(dòng)如下 location :
location /fund {
if ($remote_addr !~* "192.168.0.50|192.168.0.51|192.168.0.56") {
rewrite ^ /index.html permanent;
}
}
這樣的方式也是不支持的,當(dāng)用 IP 192.168.0.50 訪問(wèn)的時(shí)候,沒(méi)有達(dá)到我們的業(yè)務(wù)需求,會(huì)報(bào)錯(cuò) 400
注:各位有其他好的建議,歡迎探討。
相關(guān)文章
使用google-perftools優(yōu)化nginx在高并發(fā)時(shí)的性能的教程(完整版)
如果使用googler開(kāi)發(fā)的google-perftools優(yōu)化Nginx和MySQL的內(nèi)存管理,性能將會(huì)有一定程度的提升。特別是對(duì)高并發(fā)下的服務(wù)器,效果更明顯2013-02-02
nginx通過(guò)nginx_upstream_check_module實(shí)現(xiàn)后端健康檢查
nginx的健康檢查有兩種,一種是被動(dòng)健康檢查,也就是nginx自帶健康檢查模塊ngx_http_upstream_module,另一種就是主動(dòng)健康檢查,使用第三方模塊nginx_upstream_check_module,下面就來(lái)介紹一下,感興趣的可以了解一下2024-08-08
詳解用ELK來(lái)分析Nginx服務(wù)器日志的方法
這篇文章主要介紹了用ELK來(lái)分析Nginx服務(wù)器日志的方法,ELK是三個(gè)開(kāi)源軟件的縮寫,分別表示Elasticsearch,Logstash,Kibana,需要的朋友可以參考下2016-03-03
通過(guò)Nginx解決網(wǎng)絡(luò)隔離實(shí)踐記錄詳解
這篇文章主要介紹了通過(guò)Nginx解決網(wǎng)絡(luò)隔離實(shí)踐記錄詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Nginx中運(yùn)行PHP框架Laravel的配置文件分享
這篇文章主要介紹了Nginx中運(yùn)行PHP框架Laravel的配置文件分享,本文直接給出配置示例,需要的朋友可以參考下2015-06-06
https如何通過(guò)nginx完成雙向認(rèn)證轉(zhuǎn)發(fā)
文章詳細(xì)介紹了HTTPS單向認(rèn)證和雙向認(rèn)證的概念,并提供了生成自簽證書、配置Nginx進(jìn)行雙向認(rèn)證的具體步驟,通過(guò)雙向認(rèn)證,服務(wù)端和客戶端可以互相驗(yàn)證身份,提升安全性,在測(cè)試過(guò)程中,使用瀏覽器訪問(wèn)HTTPS接口時(shí),需要安裝客戶端證書才能成功獲取數(shù)據(jù)2024-11-11
在Nginx中使用X-Sendfile頭提升PHP文件下載的性能(針對(duì)大文件下載)
這篇文章主要介紹了在Nginx中使用X-Sendfile頭提升PHP文件下載的性能,可以用在針對(duì)大文件下載的情況,下載非網(wǎng)站W(wǎng)eb目錄文件的需求,提供下載權(quán)限控制的場(chǎng)景,需要的朋友可以參考下2014-07-07

