首页 运维 nginx nginx根据ip跳转不同页面

nginx根据ip跳转不同页面

Jun 22, 2019 am 11:05 AM
ip nginx 跳转

nginx根据ip跳转不同页面

最近的一个项目,需要特定的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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1662
14
CakePHP 教程
1419
52
Laravel 教程
1313
25
PHP教程
1262
29
C# 教程
1235
24
nginx在windows中怎么配置 nginx在windows中怎么配置 Apr 14, 2025 pm 12:57 PM

如何在 Windows 中配置 Nginx?安装 Nginx 并创建虚拟主机配置。修改主配置文件并包含虚拟主机配置。启动或重新加载 Nginx。测试配置并查看网站。选择性启用 SSL 并配置 SSL 证书。选择性设置防火墙允许 80 和 443 端口流量。

docker怎么启动容器 docker怎么启动容器 Apr 15, 2025 pm 12:27 PM

Docker 容器启动步骤:拉取容器镜像:运行 "docker pull [镜像名称]"。创建容器:使用 "docker create [选项] [镜像名称] [命令和参数]"。启动容器:执行 "docker start [容器名称或 ID]"。检查容器状态:通过 "docker ps" 验证容器是否正在运行。

docker容器名称怎么查 docker容器名称怎么查 Apr 15, 2025 pm 12:21 PM

可以通过以下步骤查询 Docker 容器名称:列出所有容器(docker ps)。筛选容器列表(使用 grep 命令)。获取容器名称(位于 "NAMES" 列中)。

怎么查看nginx是否启动 怎么查看nginx是否启动 Apr 14, 2025 pm 01:03 PM

确认 Nginx 是否启动的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 检查端口 80 是否开放;3. 查看系统日志中 Nginx 启动消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

docker怎么创建容器 docker怎么创建容器 Apr 15, 2025 pm 12:18 PM

在 Docker 中创建容器: 1. 拉取镜像: docker pull [镜像名] 2. 创建容器: docker run [选项] [镜像名] [命令] 3. 启动容器: docker start [容器名]

nginx怎么查版本 nginx怎么查版本 Apr 14, 2025 am 11:57 AM

可以查询 Nginx 版本的方法有:使用 nginx -v 命令;查看 nginx.conf 文件中的 version 指令;打开 Nginx 错误页,查看页面的标题。

nginx怎么配置云服务器域名 nginx怎么配置云服务器域名 Apr 14, 2025 pm 12:18 PM

在云服务器上配置 Nginx 域名的方法:创建 A 记录,指向云服务器的公共 IP 地址。在 Nginx 配置文件中添加虚拟主机块,指定侦听端口、域名和网站根目录。重启 Nginx 以应用更改。访问域名测试配置。其他注意事项:安装 SSL 证书启用 HTTPS、确保防火墙允许 80 端口流量、等待 DNS 解析生效。

nginx服务器挂了怎么办 nginx服务器挂了怎么办 Apr 14, 2025 am 11:42 AM

当 Nginx 服务器宕机时,可执行以下故障排除步骤:检查 nginx 进程是否正在运行。查看错误日志以获取错误消息。检查 nginx 配置语法正确性。确保 nginx 具有访问文件所需的权限。检查文件描述符打开限制。确认 nginx 正在侦听正确的端口。添加防火墙规则以允许nginx流量。检查反向代理设置,包括后端服务器可用性。如需进一步帮助,请联系技术支持。

See all articles