在 Debian 上用 Apache2 配置反向代理,一般步骤如下(以把 / 代理到 http://127.0.0.1:8080 为例):
sudo apt update
sudo apt install apache2
反向代理需要以下模块:
sudo a2enmod proxy
sudo a2enmod proxy_http
# 如果是 WebSocket,还需要:
sudo a2enmod proxy_wstunnel
启用后重启 Apache:
sudo systemctl restart apache2
新建一个配置文件(推荐):
sudo nano /etc/apache2/sites-available/proxy.conf
内容示例:
<VirtualHost *:80>
ServerName example.com
ProxyRequests Off
ProxyPreserveHost On
<Location />
ProxyPass http://127.0.0.1:8080/
ProxyPassReverse http://127.0.0.1:8080/
</Location>
</VirtualHost>
如果你只想代理某个路径:
ProxyPass /api/ http://127.0.0.1:8080/
ProxyPassReverse /api/ http://127.0.0.1:8080/
sudo a2ensite proxy.conf
sudo systemctl reload apache2
使用 Let’s Encrypt:
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d example.com
证书会自动配置好,并启用 443 反向代理。
确保后端返回的绝对路径正确,或使用:
ProxyPassReverse /
ProxyPass /ws/ ws://127.0.0.1:8080/
ProxyPassReverse /ws/ ws://127.0.0.1:8080/
如果你有 具体场景(如 Node / Java / Docker / 内网服务),我可以给你更精确的配置。