nginx文档和指南展示您可以在一个stream
块内执行基于 ip 的白名单/黑名单。
但是,我无法找到如何根据 ip 建立allow
/deny
连接,仅限使用 ssl 预读读取的某些主机名。
情况
我有一个 nginx 盒,它代理私有网络上的许多服务。其中一些服务应该暴露在网络外部,而有些则不应该。这个 nginx 盒代理内部和外部连接。
# TCP proxying with SSL passthrough & vhosts
stream {
map $ssl_preread_server_name $name {
public.example.com public;
private.example.com private;
default default_upstream;
}
upstream public {
server 10.0.0.2:443;
}
upstream private {
server 10.0.0.3:443;
}
upstream default_upstream {
server 10.0.0.4:443;
}
server {
listen 443;
proxy_pass $name;
ssl_preread on;
}
}
我怎样才能将基于 IP 的阻止仅应用于前往 的连接private.example.com
?
答案1
我找到了一种解决您问题的简单方法。您应该使用额外的server
块来过滤 IP。因此,您的配置应该如下所示:
# TCP proxying with SSL passthrough & vhosts
stream {
map $ssl_preread_server_name $name {
public.example.com public;
private.example.com private;
default default_upstream;
}
upstream public {
server 10.0.0.2:443;
}
upstream private {
#server 10.0.0.3:443;
server 127.0.0.1:444;
}
upstream default_upstream {
server 10.0.0.4:443;
}
server {
listen 444;
proxy_pass 10.0.0.3:443;
ssl_preread on;
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
deny all;
}
server {
listen 443;
proxy_pass $name;
ssl_preread on;
}
}
答案2
尝试使用下面的代码块。
测试过,运行良好。端口 443 上的 Telnet 将成功建立,但通过 Web 浏览器检查时,您会注意到,对于不需要的客户端,连接已被关闭。
upstream poolwithlocalhostmemberonport_444 {
server 127.0.0.1:444;
}
upstream poolwithlocalhostmemberonport_442 {
server 127.0.0.1:442;
}
ssl_preread on;
map $ssl_preread_server_name $name {
host1.com poolwithlocalhostmemberonport_444;
host2.com poolwithlocalhostmemberonport_444;
default poolwithlocalhostmemberonport_442;
}
server {
listen yourfloatingip:443;
access_log /var/log/nginxsomething.access.log customstreamlogformat;
error_log /var/log/nginx/something.error.log;
proxy_protocol on;
proxy_pass $name;
}
server {
listen 127.0.0.1:444 proxy_protocol;
set_real_ip_from 127.0.0.1; #will swap 127.0.0.1 as client addr with $proxy_protocol_addr content
allow 10.0.0.0/8;
allow 127.0.0.1;
allow 1.2.3.4;
deny all;
access_log /var/log/nginx/stream-access-internalfiltered.log customstreamlogformat;
error_log /var/log/nginx/stream-error-internalfiltered.log;
proxy_pass yournextupstream;
}
server {
listen 127.0.0.1:442 proxy_protocol;
set_real_ip_from 127.0.0.1;
access_log /var/log/nginx/stream-access-unfiltered.log customstreamlogformat;
error_log /var/log/nginx/stream-error-unfiltered.log;
proxy_pass yournextupstream;
}