小知识:使用Nginx实现根据 IP 匹配指定 URL

业务需求

业务和开发同事需要我这边做一条规则,所有访问 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 {
02server 127.0.0.1:8080;
03}
04server
05{
06listen    80;
07server_name test.com;
08index index.html index.php;
09root /tmp/test.com;
10error_page 404 http://test.com/404.html;
11error_page 502 http://test.com/502.html;
12error_page 500 http://test.com/500.html;
13location ~* \.(gif|jpg|jpeg|png|css|js|ico|txt|svg|woff|ttf|eot)$
14{
15rewrite ^(.*)$ /static$1 break;
16root /tmp/test.com; #
17expires 1d;
18}
19location ~* \.(html|htm)$
20{
21rewrite ^(.*)$ /static$1 break;
22roo /tmp/test.com; #
23expires 900s;
24}
25location / {
27include /opt/conf/nginx/proxy.conf;
28}

修改后的 Nginx 配置

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

在实现需求的过程中出现的问题

把 if 指令 和 proxy_pass 都放在 location 下面的话,if 指令里面的内容不会执行,只会执行 proxy_pass。

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

if 指令下面使用 proxy_pass 指令问题

像下面这样使用会报错,错误的方式:

1if ($remote_addr ~* “192.168.0.50|192.168.0.51|192.168.0.56”) {
3}

正确的方式:

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

或是

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

如果你是直接另外启动一个 location 的话,比如启动如下 location :

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

这样的方式也是不支持的,当用 IP 192.168.0.50 访问的时候,没有达到我们的业务需求,会报错 400

注:各位有其他好的建议,欢迎探讨。

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:Linux ls命令的使用

2023-5-8 2:53:04

建站知识

小知识:一行代码教你如何隐藏Linux进程

2023-5-8 3:01:20

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索