繁体   English   中英

如何创建我的nginx.conf

[英]How to create my 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 install等之后创建的,用于angular)

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

当我刚表演时:

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

我可以在localhost: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将使用“ conatiner_name container_ip”在/ etc / hosts文件中创建并输入,因此您可以使用其名称访问(ping)链接的容器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM