热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何连接我的NodeJS和我的角(在Nginx)-HowtoconnectmyNodeJSwithmyAngular(inNginx)

Ivearepowithangularandnodejs.Iperformedinjenkins:我有一个有角的和无弦的回购。我在詹金斯执行:#installglob

I've a repo with angular and nodejs. I performed in jenkins:

我有一个有角的和无弦的回购。我在詹金斯执行:

# install globally
npm install -g bower
npm install -g gulp

# install
bower install
npm install

# build dist folder
gulp build

Now I have in my root:

现在我有了根:

Dockerfile.nginx  Dockerfile.nodejs  README.md  bower.json  dist  gulp.config.js  gulpfile.js  node_modules  package.json  server.js  src

I'm copying the dist folder inside my nginx container. So I'm hosting the angular. (with a dockerfile)

我正在复制nginx容器中的dist文件夹。所以这是角函数。(dockerfile)

FROM nginx
# copy folder
COPY dist /usr/share/nginx/html/dist

I'm copying: gulp.config.js gulpfile.js node_modules server.js to my nodejscontainer. (also with a dockerfile)

我复制:gulp.config。js gulpfile。js node_modules服务器。js nodejscontainer。(也dockerfile)

FROM node

# Create app directory
RUN mkdir -p /usr/src/www
WORKDIR /usr/src/www 

# copy 
COPY node_modules /usr/src/www/
COPY gulpfile.js /usr/src/www/
COPY gulp.config.js /usr/src/www/
COPY server.js /usr/src/www/

 EXPOSE 8080
CMD [ "node", "server.js" ]

I run the 2 containers but the nginx does not communicate with the nodejs

我运行了两个容器,但是nginx不与nodejs通信

EDIT1: Start containers:

EDIT1:启动容器:

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

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

EDIT2: My nginx.conf looks like this:

EDIT2:我的nginx。设计是这样的:

http {

        upstream node-app {
              least_conn;
              server nodejs:8888 weight=10 max_fails=3 fail_timeout=30s;
        }

        server {
              listen 80;
              location /dist {
                alias /usr/share/nginx/html/dist/;
               }

              location ~* /api {
              #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;
              }
        }
}

My server.js looks like:

我的服务器。js的样子:

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

2 个解决方案

#1


2  

You need to expose the port of the node.js container to which nginx(angular) container will connect. See the Connect using network port mapping section of docker documentation.

您需要公开节点的端口。nginx(角)容器将连接的js容器。请参阅docker文档中使用网络端口映射部分的连接。

Update : I think, you need to configure the nginx configuration file to the node container. This question has sample nginx file related to your use case (although, not related to containers).

更新:我认为,您需要将nginx配置文件配置到节点容器。这个问题有与您的用例相关的示例nginx文件(尽管与容器无关)。

Edit : To map the node app with the nginx, you first need to link the node container with nginx container.

编辑:要将节点应用程序与nginx映射,首先需要将节点容器与nginx容器链接。

docker run -d -p 80:80 --name "nginx" --link nodejs:nodejs localhost:5000/test/nginx:1

When you link the node container with the nginx container, the node container's address will be saved in the /etc/hosts. So the nginx container can access the node's address from there.

当您将节点容器与nginx容器链接时,节点容器的地址将保存在/etc/hosts中因此,nginx容器可以从那里访问节点的地址。

So, in nginx configuration file, the nodejs will be accessible as nodejs' container address:

因此,在nginx配置文件中,nodejs可以作为nodejs的容器地址访问:

http {

        upstream node-app {
              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;
              }
        }
}

#2


1  

One solution is to link both containers as described in @manish's answer.

一种解决方案是将两个容器链接到@manish的答案中。

But be aware that this is the legacy way of connecting containers together.

但是请注意,这是将容器连接在一起的传统方式。


From now on, you can use the new docker network feature to create a virtual network and connect both containers to that network:

从现在开始,您可以使用新的docker网络特性创建一个虚拟网络,并将两个容器连接到该网络:

docker network create mynetwork
docker run -d --net=mynetwork -p 8888:8888 --name "nodejs" localhost:5000/test/nodejs:1
docker run -d --net=mynetwork -p 80:80 --name "nginx" localhost:5000/test/nginx:1

With such a setup, your nginx config file must use

有了这样的设置,您的nginx配置文件必须使用

server nodejs:8888 weight=10 max_fails=3 fail_timeout=30s;

as you now refer to other container by their name.

现在,您可以通过其他容器的名称来引用它们。


推荐阅读
author-avatar
手机用户2502911283
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有