一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

云服務(wù)器|WEB服務(wù)器|FTP服務(wù)器|郵件服務(wù)器|虛擬主機(jī)|服務(wù)器安全|DNS服務(wù)器|服務(wù)器知識(shí)|Nginx|IIS|Tomcat|

服務(wù)器之家 - 服務(wù)器技術(shù) - Nginx - 使用Nginx實(shí)現(xiàn)根據(jù) IP 匹配指定 URL

使用Nginx實(shí)現(xiàn)根據(jù) IP 匹配指定 URL

2019-10-23 14:25hebedich Nginx

最近的一個(gè)項(xiàng)目,需要特定的IP訪問某專題頁面的時(shí)候跳轉(zhuǎn)到網(wǎng)站首頁,思考了下,直接使用NGINX實(shí)現(xiàn),分享給大家。

業(yè)務(wù)需求

業(yè)務(wù)和開發(fā)同事需要我這邊做一條規(guī)則,所有訪問 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 訪問 http://test.com/fuck/index.html 依然還是 http://test.com/fuck/index.html。這樣就可以在生產(chǎn)上做隔離,不影響其他用戶的服務(wù)。

注:因?yàn)槟壳吧a(chǎn)上的 Nginx 沒有做 lua 支持,所以就無法通過使用 lua 來實(shí)現(xiàn)該需求,也沒有安裝 geoip ,所以也無法用模塊來支持,只能原生的。

原始的 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)需求的過程中出現(xià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 指令問題

像下面這樣使用會(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 訪問的時(shí)候,沒有達(dá)到我們的業(yè)務(wù)需求,會(huì)報(bào)錯(cuò) 400

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美人畜 | 亚洲四虎永久在线播放 | 色狼屋 | 欧美一区二区视频 | 无码天堂亚洲国产AV久久 | 91欧洲在线视精品在亚洲 | 久久精品视频uu | 国自产在线精品免费 | 免费操比视频 | 欧美日韩va | 四虎国产免费 | 国产微拍精品一区 | 91庥豆果冻天美精东蜜桃传媒 | 国自产在线精品免费 | 成人影院www在线观看 | 成人一级黄色大片 | 任我行视频在线观看国语 | 99久精品 | futa百合高肉全h | 按摩师他揉我奶好爽捏我奶 | 白丝美女用胸伺候主人 | 国产 日韩 欧美 综合 | 91精品综合久久久久久五月天 | 色涩导航 | 国产成人精品s8sp视频 | 扒开双腿猛进入爽爽视频ai | 99r视频 | 久久精品一卡二卡三卡四卡视频版 | 星星动漫在线观看无删减 | 短篇小说肉| 2014天堂| 成品人视频免费观看 | 国产精品对白刺激久久久 | 激情亚洲天堂 | 免费在线观看小视频 | 95在线观看精品视频 | 免费日批 | 日韩一区二区三区免费 | 极品蜜桃臀美女啪啪 | 亚洲国产综合久久久无码色伦 | 四虎影院在线免费观看视频 |