如何创build我的nginx.conf

问题是API和前端没有链接。 当我启动nginx容器时,我可以使用--link命令。 但我仍然需要编辑我的nginx.conf。

目前我有

 worker_processes 4; events { worker_connections 1024; } http { upstream node-app { least_conn; server nodejs:8888 weight=10 max_fails=3 fail_timeout=30s; } server { listen 80; location / { proxy_pass http://node-app; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } } } 

我的nodejs容器有--name nodejs和它的端口8888上运行( -p 8888:8888 )它的dockerfile看起来像这样:

 FROM node # Create app directory RUN mkdir -p /usr/src/www WORKDIR /usr/src/www # copy COPY node_modules /usr/src/www/node_modules COPY gulpfile.js /usr/src/www/gulpfile.js COPY gulp.config.js /usr/src/www/gulp.config.js COPY server.js /usr/src/www/server.js EXPOSE 8080 CMD [ "node", "server.js" ] 

我开始执行:

 docker run -d -p 8888:8888 --name nodejs localhost:5000/test/nodejs-image:1 

我可以在localhost:8888 / api上访问我的api

这是nginx的dockerfile(dist是在npm安装之后为angular创build的)

 FROM nginx COPY dist /usr/share/nginx/html/dist 

当我只是执行:

 docker run -d -p 80:80 --name nginx localhost:5000/test/nginx-image:1 

我能够访问我的本地主机上的静态文件:80 / dist / …

但我必须链接两个容器+更改nginx.conf:

 docker run -d -p 80:80 --name nginx --link nodejs:nodejs -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf localhost:5000/test/nginx-image:1 

我的server.js包含像这样的东西:

 app.post('/api/login', requestProxy({ url: xxx + "/login" })); 

有人可以修复我的nginx.conf或有另一个错误?

一个变化:在您的nginx.conf中,将proxy_pass http:// node-app更改为proxy_pass http:// nodejs:8888

当你链接你的conatiner “–link nodejs:nodejs”时,docker会在/ etc / hosts文件中创build和input“conatiner_name container_ip”,这样你就可以使用它的名字来连接(ping)链接的容器。