NGINX 将端点或 url 限制为特定 IP
NGINX Restrict endpoint or url to specific IP
我知道这个问题看起来有点奇怪,但让我解释一下我想要实现的目标:
设想:
2 个网站
green.com/endpoint (Using NGINX)
red.com
是否在 nginx 中使用任何配置,它让我只接受来自主机 red.com 的连接?
green.com/endpoint应该做的是:
1. Analyze the request
2. If request comes from red.com -> ACCEPTED
2.1 If request does not come from red.com -> REJECTED
编辑:(我的解决方案)
首先编辑您的 conf.d/green.conf
文件,将任何端点限制在允许的 IP 列表中,请考虑:
dig red.com
(或您允许的域)并获取 public IP
location ~* /endpoint {
proxy_pass http://localhost:1234;
#return 200 "$http_x_forwarded_for\n";
set $allow false;
if ($http_x_forwarded_for ~* 123.123.123.123) {
set $allow true;
}
if ($allow = false) {
return 403;
}
}
使用此配置,我们只允许域 red.com 访问 green.com/endpoint
允许从特定域 (IP) 访问特定端点 (URL) 的解决方案是:
location ~* /endpoint {
proxy_pass http://localhost:1234;
#return 200 "$http_x_forwarded_for\n";
set $allow false;
if ($http_x_forwarded_for ~* 123.123.123.123) {
set $allow true;
}
if ($allow = false) {
return 403;
}
}
我知道这个问题看起来有点奇怪,但让我解释一下我想要实现的目标: 设想: 2 个网站
green.com/endpoint (Using NGINX)
red.com
是否在 nginx 中使用任何配置,它让我只接受来自主机 red.com 的连接?
green.com/endpoint应该做的是:
1. Analyze the request
2. If request comes from red.com -> ACCEPTED
2.1 If request does not come from red.com -> REJECTED
编辑:(我的解决方案)
首先编辑您的 conf.d/green.conf
文件,将任何端点限制在允许的 IP 列表中,请考虑:
dig red.com
(或您允许的域)并获取 public IP
location ~* /endpoint {
proxy_pass http://localhost:1234;
#return 200 "$http_x_forwarded_for\n";
set $allow false;
if ($http_x_forwarded_for ~* 123.123.123.123) {
set $allow true;
}
if ($allow = false) {
return 403;
}
}
使用此配置,我们只允许域 red.com 访问 green.com/endpoint
允许从特定域 (IP) 访问特定端点 (URL) 的解决方案是:
location ~* /endpoint {
proxy_pass http://localhost:1234;
#return 200 "$http_x_forwarded_for\n";
set $allow false;
if ($http_x_forwarded_for ~* 123.123.123.123) {
set $allow true;
}
if ($allow = false) {
return 403;
}
}