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

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

问题描述

我有一个有角度和nodejs的回购。
我在jenkins表演:

 #全球安装
npm install -g bower
npm安装-g gulp

#install
bower install
npm install

#build dist folder
gulp build

现在我在我的根目录中:

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

我正在复制我的nginx容器中的dist文件夹。所以我主持的角度。 (有一个docker文件)

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

我正在复制: gulp.config.js gulpfile .js node_modules server.js 到我的nodejscontainer。 (还有一个docker文件)

  FROM node 

#创建应用程序目录
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通信



EDIT1:
启动容器:

  docker run -d -p 8888:8888 --namenodejslocalhost:5000 / test / nodejs:1 

docker run -d - p 80:80 - 名称nginxlocalhost:5000 / test / nginx:1

EDIT2:我的nginx.conf如下所示:

  http {

上游node-app {
least_conn;
服务器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升级$ http_upgrade;
proxy_set_header连接'升级';
proxy_set_header Host $ host;
proxy_cache_bypass $ http_upgrade;
}
}
}

我的server.js看起来像:

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


解决方案

您需要公开node.js的端口容器将连接到nginx(角)容器。
请参阅 Connect使用port port文档的网络端口映射部分。



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



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

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

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



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

  http {

上游节点应用程序{
服务器nodejs:8888 weight = 10 max_fails = 3 fail_timeout = 30岁;
}

server {
listen 80;

location / {
proxy_pass http:// node-app;
proxy_http_version 1.1;
proxy_set_header升级$ http_upgrade;
proxy_set_header连接'升级';
proxy_set_header Host $ host;
proxy_cache_bypass $ http_upgrade;
}
}
}


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)

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)

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

EDIT1: Start containers:

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:

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:

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

解决方案

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.

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).

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

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.

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与我的角度(在Nginx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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