要解决多个应用通过80端口对外提供服务的问题,可以使用反向代理服务器(如Nginx或Apache)来管理流量。以下是具体步骤:
1. 使用反向代理服务器
反向代理服务器可以根据请求的域名或路径将流量分发到不同的内部端口。
使用Nginx作为反向代理
-
安装Nginx
sudo apt-get update sudo apt-get install nginx
-
配置Nginx
编辑Nginx配置文件(通常位于/etc/nginx/sites-available/default
或/etc/nginx/nginx.conf
),添加以下内容:server { listen 80; server_name company.com; location / { proxy_pass http://localhost:3000; # 公司站点应用的内部端口 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } server { listen 80; server_name tools.company.com; location / { proxy_pass http://localhost:4000; # PDF在线转换工具的内部端口 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
这里假设:
company.com
是公司站点的域名,映射到3000端口。tools.company.com
是在线工具的域名,映射到4000端口。
-
重启Nginx
sudo systemctl restart nginx
使用Apache作为反向代理
-
安装Apache
sudo apt-get update sudo apt-get install apache2
-
启用代理模块
sudo a2enmod proxy sudo a2enmod proxy_http
-
配置Apache
编辑Apache配置文件(通常位于/etc/apache2/sites-available/000-default.conf
),添加以下内容:<VirtualHost *:80> ServerName company.com ProxyPreserveHost On ProxyPass / http://localhost:3000/ ProxyPassReverse / http://localhost:3000/ </VirtualHost> <VirtualHost *:80> ServerName tools.company.com ProxyPreserveHost On ProxyPass / http://localhost:4000/ ProxyPassReverse / http://localhost:4000/ </VirtualHost>
-
重启Apache
sudo systemctl restart apache2
2. 使用Docker Compose
如果使用Docker Compose管理容器,可以在docker-compose.yml
中定义服务并通过Nginx或Apache进行反向代理。
version: '3'
services:
web:
image: your-company-site-image
ports:
- "3000:3000"
pdf-tool:
image: your-pdf-tool-image
ports:
- "4000:4000"
nginx:
image: nginx
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
depends_on:
- web
- pdf-tool
3. DNS配置
确保域名解析正确:
company.com
指向服务器IP。tools.company.com
也指向服务器IP。
总结
通过反向代理服务器(如Nginx或Apache),你可以将多个应用通过80端口对外提供服务,并根据域名或路径将流量分发到不同的内部端口。