我已经在我的 VPS 上部署了一个 Docker Swarm 服务器,用于处理 Asp.Net Core 应用程序。我想通过 Nginx Web 服务器来提供此应用程序。
假设我的 Web 应用程序是我通过 .Net Core CLI 命令创建的原始应用程序:
dotnet new webapp mywebapp
Dockerfile(简化):
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-alpine as builder
WORKDIR /app
COPY . .
RUN dotnet publish -c Release -o publish
WORKDIR /app/publish
ENTRYPOINT ["dotnet", "MyWebApp.dll"]
我的docker-compose.yml
样子如下(简化):
version: '3'
services:
app:
image: edouard/mywebapp:latest
ports:
- 9000:80
我的 nginx 配置如下:
server {
listen 443 ssl;
server_name myservername.com;
ssl_certificate /path/to/ssl_certificate;
ssl_certificate_key /path/to/ssl_certificate_key;
location / {
proxy_pass http://localhost:9000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
server {
listen 80;
server_name myservername.com;
return 301 https://$host$request_uri;
}
如您所见,我使用 Nginx 作为反向代理服务器,将所有 HTTP/HTTPS 流量从 80 和 443 端口重定向到本地 9000 端口,Docker Swarm 将其映射到容器内的 80 端口,Kestrel 服务器正在该端口上运行。
在 上https://myservername.com
,一切都运行良好。但问题是:人们还可以在 上访问我的 Web 应用程序http://myservername.com:9000
!这是我不想要的。
我想我必须配置防火墙,以便只允许流量到 80 和 443 端口(注意让 22 端口用于 SSH 等)。我读了一些教程来了解如何做到这一点,但是,Docker Swarm 也在处理防火墙!
当我启动时sudo iptables -L -v
:
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
3417 873K DOCKER-USER all -- any any anywhere anywhere
3417 873K DOCKER-INGRESS all -- any any anywhere anywhere
31 9043 DOCKER-ISOLATION-STAGE-1 all -- any any anywhere anywhere
0 0 ACCEPT all -- any docker0 anywhere anywhere ctstate RELATED,ESTABLISHED
0 0 DOCKER all -- any docker0 anywhere anywhere
0 0 ACCEPT all -- docker0 !docker0 anywhere anywhere
0 0 ACCEPT all -- docker0 docker0 anywhere anywhere
18 7620 ACCEPT all -- any docker_gwbridge anywhere anywhere ctstate RELATED,ESTABLISHED
0 0 DOCKER all -- any docker_gwbridge anywhere anywhere
13 1423 ACCEPT all -- docker_gwbridge !docker_gwbridge anywhere anywhere
0 0 DROP all -- docker_gwbridge docker_gwbridge anywhere anywhere
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain DOCKER (2 references)
pkts bytes target prot opt in out source destination
Chain DOCKER-ISOLATION-STAGE-1 (1 references)
pkts bytes target prot opt in out source destination
0 0 DOCKER-ISOLATION-STAGE-2 all -- docker0 !docker0 anywhere anywhere
13 1423 DOCKER-ISOLATION-STAGE-2 all -- docker_gwbridge !docker_gwbridge anywhere anywhere
31 9043 RETURN all -- any any anywhere anywhere
Chain DOCKER-ISOLATION-STAGE-2 (2 references)
pkts bytes target prot opt in out source destination
0 0 DROP all -- any docker0 anywhere anywhere
0 0 DROP all -- any docker_gwbridge anywhere anywhere
13 1423 RETURN all -- any any anywhere anywhere
Chain DOCKER-USER (1 references)
pkts bytes target prot opt in out source destination
3417 873K RETURN all -- any any anywhere anywhere
Chain DOCKER-INGRESS (1 references)
pkts bytes target prot opt in out source destination
1567 101K ACCEPT tcp -- any any anywhere anywhere tcp dpt:9000
1270 698K ACCEPT tcp -- any any anywhere anywhere state RELATED,ESTABLISHED tcp spt:9000
31 9043 RETURN all -- any any anywhere anywhere
我该如何配置防火墙以使其不与Docker Swarm交互? 我找到了部分答案:
然而,我发现它非常复杂,而且令我惊讶的是 Docker 的博客上没有对这个问题的官方答案。
版本:
- VPS: Debian 10.2
- Docker 引擎:19.03.5
- Nginx:1.16.1
- Iptables:1.8.2
感谢您的帮助。
答案1
您可以尝试绑定 localhost 而不是默认的 0.0.0.0 地址:
services:
app:
image: edouard/mywebapp:latest
ports:
- '127.0.0.1:9000:80'