docker 配置 nginx 启动 vue 项目无法连接后端项目
来源:6-27 前端代码打包与部署

少年非正式
2020-11-05
- 背景
前端部署的时候,nginx 使用 docker 来设置的。相关配置如下。
Dockerfile
FROM nginx
COPY ./dist/ /usr/share/nginx/vue_gateway_demo/dist/
COPY nginx.conf /etc/nginx/nginx.conf
DockerCompose
version: '3'
services:
gateway_vue:
image: shaonian/vue_gateway_demo:v1.0
ports:
- "8080:8080"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
Nginx.Conf
...
server {
listen 8080;
server_name 127.0.0.1;
root /usr/share/nginx/vue_gateway_demo/dist;
#access_log logs/host.access.log main;
location / {
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /prod-api/ {
proxy_pass http://127.0.0.1:8880/;
}
}
后端项目没有用 docker 起,而是本地直接启动的,即 go run main.go
- 问题
前端启动好以后,访问登录界面正常,但是无法连通后端,nginx 会出现如下报错。
gateway_vue_1 | "POST /prod-api/admin_login/login HTTP/1.1" 502 559 "http://127.0.0.1:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
gateway_vue_1 | [error] 28#28: *2 connect() failed (111: Connection refused) while connecting to upstream, client: xxx, server: localhost, request: "POST /prod-api/admin_login/login HTTP/1.1", upstream: "http://127.0.0.1:8880/admin_login/login", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/"
主要报错为:failed (111: Connection refused) while connecting to upstream
查看后端日志,其实并没有收到这个请求。
想问下如果不用本机启动 nginx,使用 docker 部署 nginx 的话,nginx.conf 如何配置才能连通本机启动的后端服务呢?
谢谢老师!
写回答
1回答
-
牛儿吃草
2020-11-09
nginx填写本机的局域网ip加端口
012020-11-09
相似问题