最近的一个项目,需要特定的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 配置
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; }
在实现需求的过程中出现的问题
把 if 指令 和 proxy_pass 都放在 location 下面的话,if 指令里面的内容不会执行,只会执行 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 指令问题
像下面这样使用会报错,错误的方式:
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; }
如果你是直接另外启动一个 location 的话,比如启动如下 location :
location /fund { if ($remote_addr !~* "192.168.0.50|192.168.0.51|192.168.0.56") { rewrite ^ /index.html permanent; } }
这样的方式也是不支持的,当用 IP 192.168.0.50 访问的时候,没有达到我们的业务需求,会报错 400
注:各位有其他好的建议,欢迎探讨。
更多Nginx相关技术文章,请访问Nginx教程栏目进行学习!
以上是nginx根據ip跳轉不同頁面的詳細內容。更多資訊請關注PHP中文網其他相關文章!

NGINX适合高并发和低资源消耗场景,Apache适用于需要复杂配置和功能扩展的场景。1.NGINX以高性能处理大量并发连接著称。2.Apache以稳定性和丰富模块支持见长。选择时需根据具体需求决定。

NGINXisessentialformodernwebapplicationsduetoitsrolesasareverseproxy,loadbalancer,andwebserver,offeringhighperformanceandscalability.1)Itactsasareverseproxy,enhancingsecurityandperformancebycachingandloadbalancing.2)NGINXsupportsvariousloadbalancingm

通過Nginx配置SSL/TLS來確保網站安全,需要以下步驟:1.創建基本配置,指定SSL證書和私鑰;2.優化配置,啟用HTTP/2和OCSPStapling;3.調試常見錯誤,如證書路徑和加密套件問題;4.應用性能優化建議,如使用Let'sEncrypt和會話復用。

Nginx是高性能的HTTP和反向代理服務器,擅長處理高並發連接。 1)基本配置:監聽端口並提供靜態文件服務。 2)高級配置:實現反向代理和負載均衡。 3)調試技巧:檢查錯誤日誌和測試配置文件。 4)性能優化:啟用Gzip壓縮和調整緩存策略。

Nginx缓存可以通过以下步骤显著提升网站性能:1)定义缓存区和设置缓存路径;2)配置缓存有效期;3)根据不同内容设置不同的缓存策略;4)优化缓存存储和负载均衡;5)监控和调试缓存效果。通过这些方法,Nginx缓存能减少后端服务器压力,提升响应速度和用户体验。

使用DockerCompose可以簡化Nginx的部署和管理,通過DockerSwarm或Kubernetes進行擴展是常見的做法。 1)使用DockerCompose定義和運行Nginx容器,2)通過DockerSwarm或Kubernetes實現集群管理和自動擴展。

Nginx的高級配置可以通過服務器塊和反向代理實現:1.服務器塊允許在一個實例中運行多個網站,每個塊獨立配置。 2.反向代理將請求轉發到後端服務器,實現負載均衡和緩存加速。

Nginx性能調優可以通過調整worker進程數、連接池大小、啟用Gzip壓縮和HTTP/2協議、使用緩存和負載均衡來實現。 1.調整worker進程數和連接池大小:worker_processesauto;events{worker_connections1024;}。 2.啟用Gzip壓縮和HTTP/2協議:http{gzipon;server{listen443sslhttp2;}}。 3.使用緩存優化:http{proxy_cache_path/path/to/cachelevels=1:2k


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

記事本++7.3.1
好用且免費的程式碼編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Linux新版
SublimeText3 Linux最新版