热门标签 | 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.

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


推荐阅读
  • Node.js学习笔记(一)package.json及cnpm
    本文介绍了Node.js中包的概念,以及如何使用包来统一管理具有相互依赖关系的模块。同时还介绍了NPM(Node Package Manager)的基本介绍和使用方法,以及如何通过NPM下载第三方模块。 ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了在rhel5.5操作系统下搭建网关+LAMP+postfix+dhcp的步骤和配置方法。通过配置dhcp自动分配ip、实现外网访问公司网站、内网收发邮件、内网上网以及SNAT转换等功能。详细介绍了安装dhcp和配置相关文件的步骤,并提供了相关的命令和配置示例。 ... [详细]
  • 本文介绍了在Linux下安装Perl的步骤,并提供了一个简单的Perl程序示例。同时,还展示了运行该程序的结果。 ... [详细]
  • 本文介绍了如何使用Express App提供静态文件,同时提到了一些不需要使用的文件,如package.json和/.ssh/known_hosts,并解释了为什么app.get('*')无法捕获所有请求以及为什么app.use(express.static(__dirname))可能会提供不需要的文件。 ... [详细]
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
  • 本文介绍了前端人员必须知道的三个问题,即前端都做哪些事、前端都需要哪些技术,以及前端的发展阶段。初级阶段包括HTML、CSS、JavaScript和jQuery的基础知识。进阶阶段涵盖了面向对象编程、响应式设计、Ajax、HTML5等新兴技术。高级阶段包括架构基础、模块化开发、预编译和前沿规范等内容。此外,还介绍了一些后端服务,如Node.js。 ... [详细]
  • 网络请求模块选择——axios框架的基本使用和封装
    本文介绍了选择网络请求模块axios的原因,以及axios框架的基本使用和封装方法。包括发送并发请求的演示,全局配置的设置,创建axios实例的方法,拦截器的使用,以及如何封装和请求响应劫持等内容。 ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • (三)多表代码生成的实现方法
    本文介绍了一种实现多表代码生成的方法,使用了java代码和org.jeecg框架中的相关类和接口。通过设置主表配置,可以生成父子表的数据模型。 ... [详细]
  • 本文介绍了在Mac上安装Xamarin并使用Windows上的VS开发iOS app的方法,包括所需的安装环境和软件,以及使用Xamarin.iOS进行开发的步骤。通过这种方法,即使没有Mac或者安装苹果系统,程序员们也能轻松开发iOS app。 ... [详细]
  • uniapp开发H5解决跨域问题的两种代理方法
    本文介绍了uniapp开发H5解决跨域问题的两种代理方法,分别是在manifest.json文件和vue.config.js文件中设置代理。通过设置代理根域名和配置路径别名,可以实现H5页面的跨域访问。同时还介绍了如何开启内网穿透,让外网的人可以访问到本地调试的H5页面。 ... [详细]
  • linux和jenkins的连接
    1.在浏览器中打开我们的jenkins页面。(http:ip:端口号jenkins)2.然后登陆管理员用户。3.配置节点信息1).点击【jenkins管理】【节点管理】【新建节 ... [详细]
  • testcafe的版本-1.7.0和1.7.1(最新)铬版本-78.0.3904.108运行环境 ... [详细]
author-avatar
Carry_Jia
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有