这个坑我踩了两周都没踏过去,最终还是做我旁边的大佬告诉我的,先说一下情况
有一个vue前端项目,原本的访问地址为http://localhost:9001,想用nginx反向代理将9001端口号去掉,我敲代码电脑的操纵系统为windows,并从window上运行docker,这个项目在windows上的idea中测试运行,但是设置好nginx.conf之后
也就是这样,
server {
listen 80;
server_name 127.0.0.1;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
proxy_pass http://127.0.0.1:9001;
proxy_connect_timeout 600;
proxy_read_timeout 600;
}
}
但是总是出现
网上查了好多怎么都是502,而且error.log日志中总是这样的路径找不到的错误
2020/05/13 06:31:08 [error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: 127.0.0.1, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:9001/", host: "127.0.0.1"
最后还是求助于大佬,原来是因为在windows上docker可以看作是一台虚拟机,在nginx.conf中设置的127.0.0.1:9001是访问不到宿主机上的,也就是windows。需要把nginx.conf中的127.0.0.1改成http://host.docker.internal:9001 便可以访问到宿主机上了。