Apache2与Nginx子域名反向代理均无法正常工作问题求助
大家好,我折腾了好几天还是没解决这个反向代理的问题,想请教下各位大佬:
我的环境是树莓派4 8GB + Ubuntu 22.04,用Apache2托管网站,同时跑了几个不同端口的Docker容器。核心需求是实现无URL变更的反向代理:把raspberry.local指向localhost:8080,scan.raspberry.local指向localhost:1234。
按照网上教程配置后,主域名的虚拟主机完全正常,但子域名死活不管用——不管是本地直接访问还是通过VPN连接,用curl测试都会提示“could not resolve host”。后来换成Nginx尝试,结果也是一模一样:主域名正常,子域名解析失败。
一、Apache2配置细节
我已经启用了proxy、proxy_http和rewrite模块,配置文件放在/etc/apache2/sites-enabled/000-default.conf(是sites-available目录下文件的软链接),具体内容如下:
# Home page : working properly <VirtualHost *:80> ServerName raspberry.local ProxyPreserveHost On ProxyRequests Off ProxyPass / http://localhost:8080/ ProxyPassReverse / http://localhost:8080/ ErrorLog ${APACHE_LOG_DIR}/error-homer.log CustomLog ${APACHE_LOG_DIR}/access-homer.log combined </VirtualHost> # Scanner : not working : "could not resolve host" when I cURL <VirtualHost *:80> ServerName scan.raspberry.local ProxyPreserveHost On ProxyRequests Off ProxyPass / http://localhost:1234/ ProxyPassReverse / http://localhost:1234/ ErrorLog ${APACHE_LOG_DIR}/error-scan.log CustomLog ${APACHE_LOG_DIR}/access-scan.log combined </VirtualHost>
执行apache2ctl configtest返回配置正常,apache2ctl -S也能正确识别到两个虚拟主机,日志文件里完全没有报错信息。
二、Nginx尝试的配置
后来我换成Nginx试了,配置文件如下:
server { listen 80; server_name raspberry.local; location / { proxy_pass http://localhost:8080; } } server { listen 80; server_name scan.raspberry.local; location / { proxy_pass http://localhost:1234; } }
结果还是老样子:主域名正常访问,子域名无法解析,就算在树莓派本地用curl访问子域名,也会提示“could not resolve host”。
三、已做的排查步骤
- 树莓派本地的
/etc/hosts、Windows/WSL的c:\Windows\System32\Drivers\etc\hosts都已经添加了正确映射:把raspberry.local和scan.raspberry.local指向树莓派的IP - 树莓派上搭了VPN,VPN配置也同步了域名映射规则
- 两个Docker容器都正常运行,直接访问
localhost:8080和localhost:1234都能打开对应服务
我搜了超多相关帖子,但没找到和我完全匹配的情况——虚拟主机配置本身没问题、日志无报错、hosts也配置正确,但就是子域名解析失败。有没有朋友遇到过类似问题?麻烦指点一下,谢谢!
备注:内容来源于stack exchange,提问作者tholeb

