在 Ubuntu 上用 Apache2 做转发(反向代理) 通常有两种常见场景:
把 http://你的域名/ 转发到本机或其他机器的某个端口(如 8080、3000)。
sudo apt update
sudo apt install apache2
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo systemctl restart apache2
sudo nano /etc/apache2/sites-available/proxy.conf
内容示例(转发到 8080 端口):
<VirtualHost *:80>
ServerName example.com
ProxyRequests Off
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
ErrorLog ${APACHE_LOG_DIR}/proxy_error.log
CustomLog ${APACHE_LOG_DIR}/proxy_access.log combined
</VirtualHost>
sudo a2ensite proxy.conf
sudo systemctl restart apache2
✅ 访问 http://example.com 就会转发到 127.0.0.1:8080
例如:
/api/ → http://127.0.0.1:3000// → 静态页面<VirtualHost *:80>
ServerName example.com
ProxyPass /api/ http://127.0.0.1:3000/
ProxyPassReverse /api/ http://127.0.0.1:3000/
DocumentRoot /var/www/html
</VirtualHost>
如果是生产环境:
sudo apt install certbot python3-certbot-apache
sudo certbot --apache
Certbot 会自动配置 HTTPS 并支持转发。
sudo ufw allow 80
sudo ufw allow 443
sudo apache2ctl configtest
sudo systemctl restart apache2
你可以告诉我:
我可以直接给你一份可用的配置文件。