国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

云服務器|WEB服務器|FTP服務器|郵件服務器|虛擬主機|服務器安全|DNS服務器|服務器知識|Nginx|IIS|Tomcat|

香港云服务器
服務器之家 - 服務器技術 - Nginx - 使用Nginx實現根據 IP 匹配指定 URL

使用Nginx實現根據 IP 匹配指定 URL

2019-10-23 14:25hebedich Nginx

最近的一個項目,需要特定的IP訪問某專題頁面的時候跳轉到網站首頁,思考了下,直接使用NGINX實現,分享給大家。

業務需求

業務和開發同事需要我這邊做一條規則,所有訪問 ip 為非上海、廣州 office 外網 ip,url 為http://test.com/fuck/index.html 的請求都跳轉到 http://test.com/index.html 。然后所有在上海和廣州 office 的外網 IP 訪問 http://test.com/fuck/index.html 依然還是 http://test.com/fuck/index.html。這樣就可以在生產上做隔離,不影響其他用戶的服務。

注:因為目前生產上的 Nginx 沒有做 lua 支持,所以就無法通過使用 lua 來實現該需求,也沒有安裝 geoip ,所以也無法用模塊來支持,只能原生的。

原始的 nginx 配置

01upstream service_test {
02     server 127.0.0.1:8080;
03}
04server
05 {
06  listen    80;
07  server_name test.com;
08  index index.html index.php;
09  root /tmp/test.com;
10  error_page 404 http://test.com/404.html;
11  error_page 502 http://test.com/502.html;
12  error_page 500 http://test.com/500.html;
13  location ~* \.(gif|jpg|jpeg|png|css|js|ico|txt|svg|woff|ttf|eot)$
14  {
15    rewrite ^(.*)$ /static$1 break;
16    root /tmp/test.com; #
17    expires 1d;
18  }
19  location ~* \.(html|htm)$
20  {
21    rewrite ^(.*)$ /static$1 break;
22    roo /tmp/test.com; #
23    expires 900s;
24  }
25  location / {
26     proxy_pass http://service_test;
27     include /opt/conf/nginx/proxy.conf;
28  }

修改后的 Nginx 配置

01upstream service_test {
02     server 127.0.0.1:8080;
03}
04server
05 {
06  listen    80;
07  server_name test.com;
08  index index.html index.php;
09  root /tmp/test.com;
10  error_page 404 http://test.com/404.html;
11  error_page 502 http://test.com/502.html;
12  error_page 500 http://test.com/500.html;
13  location ~* \.(gif|jpg|jpeg|png|css|js|ico|txt|svg|woff|ttf|eot)$
14  {
15    rewrite ^(.*)$ /static$1 break;
16    root /tmp/test.com; #
17    expires 1d;
18  }
19  location ~* \.(html|htm)$
20  {
21    rewrite ^(.*)$ /static$1 break;
22    roo /tmp/test.com; #
23    expires 900s;
24  }
25  set $flag 0;
26  if ($request_uri ~* "^/fuck/\w+\.html$") {
27      set $flag "${flag}1";
28  }
29  if ($remote_addr !~* "192.168.0.50|192.168.0.51|192.168.0.56") {
30    set $flag "${flag}2";
31  }
32  if ($flag = "012") {
33    rewrite ^ /index.html permanent;
34  }
35  location / {
36     proxy_pass http://service_test;
37     include /opt/conf/nginx/proxy.conf;
38  }

在實現需求的過程中出現的問題

把 if 指令 和 proxy_pass 都放在 location 下面的話,if 指令里面的內容不會執行,只會執行 proxy_pass。

1location / {
2   if ($remote_addr !~* "192.168.0.50|192.168.0.51|192.168.0.56") {
3      rewrite ^ /index.html permanent;
4   }
5   proxy_pass http://service_test;
6   include /opt/conf/nginx/proxy.conf;
7}

if 指令下面使用 proxy_pass 指令問題

像下面這樣使用會報錯,錯誤的方式:

1if ($remote_addr ~* "192.168.0.50|192.168.0.51|192.168.0.56") {
2  proxy_pass http://test.com/fuck;
3}

正確的方式:

1if ($remote_addr ~* "192.168.0.50|192.168.0.51|192.168.0.56") {
2  proxy_pass http://test.com$request_uri;
3}

或是

1if ($remote_addr ~* "192.168.0.50|192.168.0.51|192.168.0.56") {
2  proxy_pass http://test.com;
3}


如果你是直接另外啟動一個 location 的話,比如啟動如下 location :

1location /fund {
2   if ($remote_addr !~* "192.168.0.50|192.168.0.51|192.168.0.56") {
3      rewrite ^ /index.html permanent;
4   }
5}

這樣的方式也是不支持的,當用 IP 192.168.0.50 訪問的時候,沒有達到我們的業務需求,會報錯 400

注:各位有其他好的建議,歡迎探討。

延伸 · 閱讀

精彩推薦
1178
主站蜘蛛池模板: 日本不卡一区二区三区在线观看 | 免费观看在线毛片 | 欧美日韩在线综合 | 日韩高清一区二区 | 国产玖玖视频 | 色二区 | 国产精品久久一区 | 欧美在线视频网 | 无毛网站 | 免费一级片在线观看 | 精品在线一区二区三区 | 欧美成人激情视频 | 黄片毛片 | 精品视频 | 亚洲一区在线观看视频 | 国产亚洲欧美一区 | 亚洲国产精品99久久久久久久久 | 国产精品美女久久久久aⅴ国产馆 | 亚洲八区 | 日韩成人av在线 | 日韩不卡 | 中文字幕在线精品 | 欧美九九九 | 这里只有精品视频 | 精品一二区 | 国产一区二区三区播放 | 日本三级视频 | 国产日韩成人 | 日本丶国产丶欧美色综合 | 国产精品亚洲第一区在线暖暖韩国 | 中文字幕视频在线 | 午夜激情影院 | 色女人av | 久久av网 | 亚洲午夜精品久久久久久高潮 | 欧美全黄 | 久久久久久久久一区二区三区 | 亚洲日本va中文字幕 | 北条麻妃99 | 欧美视频免费看 | 久久a毛片 |