如何连接我的 NodeJS 和我的 Angular(在 Nginx 中) [英] How to connect my NodeJS with my Angular (in Nginx)

查看:15
本文介绍了如何连接我的 NodeJS 和我的 Angular(在 Nginx 中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 angular 和 nodejs 的仓库.我在詹金斯表演:

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

我正在复制我的 nginx 容器中的 dist 文件夹.所以我正在托管角度.(带有 dockerfile)

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

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

我正在复制:gulp.config.js gulpfile.js node_modules server.js 到我的 nodejscontainer.(也带有 dockerfile)

I'm copying: gulp.config.js gulpfile.js node_modules server.js to my nodejscontainer. (also with a 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" ]

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

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

启动容器:

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

我的 nginx.conf 如下所示:

My nginx.conf looks like this:

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;
              }
        }
}

我的 server.js 看起来像:

My server.js looks like:

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

推荐答案

需要暴露 nginx(angular) 容器将连接的 node.js 容器的端口.请参阅 使用网络端口映射连接 docker 文档部分.

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 配置文件配置到节点容器.This question 包含与您的用例相关的示例 nginx 文件(尽管与容器无关).

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.

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

当你将 node 容器与 nginx 容器链接时,node 容器的地址将保存在/etc/hosts 中.所以 nginx 容器可以从那里访问节点的地址.

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 配置文件中,nodejs 将作为 nodejs 的容器地址访问:

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

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;
              }
        }
}

这篇关于如何连接我的 NodeJS 和我的 Angular(在 Nginx 中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆