在 Ubuntu 上用 Apache 配置反向代理,一般步骤如下(以 Ubuntu 20.04 / 22.04 为例,Apache 2.4)。
sudo apt update
sudo apt install apache2
反向代理需要以下模块:
sudo a2enmod proxy
sudo a2enmod proxy_http
# 如果是 WebSocket(如 Node.js)
sudo a2enmod proxy_wstunnel
启用后重启 Apache:
sudo systemctl restart apache2
http://example.com 反向代理到 http://127.0.0.1:3000编辑或新建配置文件:
sudo nano /etc/apache2/sites-available/example.conf
内容示例:
<VirtualHost *:80>
ServerName example.com
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
sudo a2ensite example.conf
sudo systemctl reload apache2
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d example.com
Certbot 会自动帮你配置 ProxyPass 到 HTTPS。
检查:
ProxyPassReverse / http://127.0.0.1:3000/
ProxyPass /ws/ ws://127.0.0.1:3000/
ProxyPassReverse /ws/ ws://127.0.0.1:3000/
ProxyPass / http://localhost:8080/
<VirtualHost *:80>
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/
</VirtualHost>
如果你有 具体场景(如 Docker / Spring Boot / Node / 内网服务 / HTTPS),我可以给你更精确的配置。