简体   繁体   English

当尝试连接到节点应用程序作为反向代理时,Nginx连接被拒绝

[英]Nginx connection refused, when trying to connect to node app as reverse proxy

I trying to build a webapp with docker containers and I am getting connection refused when trying to run Nginx as a reverse proxy to my node app. 我尝试使用docker容器构建一个webapp,当我尝试将Nginx作为我的节点应用程序的反向代理运行时,我得到连接被拒绝。 I am not sure if it is a nginx server config issue or a docker-compose config issue. 我不确定它是否是nginx服务器配置问题或docker-compose配置问题。

[error] 5#5: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: foo.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:7770/", host: "foo.com"

I get this error when hitting foo.com, the weird thing is that my app works when the port number is referenced, so foo.com:7770 runs the app. 我在点击foo.com时遇到这个错误,奇怪的是我的应用程序在引用端口号时工作,所以foo.com:7770运行应用程序。

My nginx server config: 我的nginx服务器配置:

server {
    listen       80;
    server_name  foo.com;

    port_in_redirect off;
    autoindex on;

    location / {
        proxy_pass http://127.0.0.1:7770;
        proxy_redirect off;
        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 Docker Compose File: (There might be some redundant things in here) 我的Docker撰写文件:(这里可能有一些多余的东西)

version: "2"
services:
  nginx:
    build: ./nginx
    ports:
      - "80:80"
    depends_on:
      - app
    links:
      - app
  app:
    build:
      context: .
      dockerfile: DockerFile
    ports:
      - "7770:7770"
    links:
      - mongo
    depends_on:
      - mongo
  mongo:
    image: mongo
    ports:
      - "27017:27017"
    volumes_from:
      - mongodata
    depends_on:
      - mongodata
  mongodata:
    image: tianon/true
    volumes:
      - /data/db

My Node Dockerfile: 我的节点Dockerfile:

FROM node:latest

ADD package.json /tmp/package.json
RUN cd /tmp && npm install
RUN mkdir -p /opt/app && cp -a /tmp/node_modules /opt/app/

WORKDIR /opt/app
ADD . /opt/app

EXPOSE 7770

CMD ["npm", "start"]

My ngnix Dockerfile 我的ngnix Dockerfile

FROM nginx:1.10

COPY default.conf /etc/nginx/conf.d/default.conf

On npm start this will run: 在npm开始,这将运行:

app.get('*', function(req, res) {
  res.sendFile(path.join(__dirname, 'index.html'));
});

app.listen(7770, function(err) {
  if (err) {
    console.log(err);
    return;
  }

  console.log('Listening at http://localhost:7770');
});

This is my first run at docker, so I may have muddled up a few things. 这是我第一次在码头工作,所以我可能搞砸了一些事情。 Also I am pointing foo.com to 127.0.0.1 in /private/etc/hosts. 另外,我将foo.com指向/ private / etc / hosts中的127.0.0.1。

c-holmes, C-霍姆斯

First of all, you need to remember each container has its own network stack, so you cannot use localhost inside the container to reach a service running in the docker host. 首先,您需要记住每个容器都有自己的网络堆栈,因此您无法在容器内使用localhost来访问在docker主机中运行的服务。

For this specific project, you will need to point the proxy_pass directive in your Nginx server config to a value what reach the app container. 对于此特定项目,您需要将Nginx服务器配置中的proxy_pass指令指向到达app容器的值。 Something like: 就像是:

proxy_pass http://app:7770;

You will need to do right that because in docker-compose context your container name will be mapped to an internal DNS entry. 您需要做正确的事,因为在docker-compose上下文中,您的容器名称将映射到内部DNS条目。 With that, you will not need to publish 7770 of the app container to the outside world and if your MongoBD will be accessed just by your app container, you will not need to publish the 27017 port either. 这样,您就不需要将7770的应用程序容器发布到外部世界,如果您的应用程序容器只能访问您的MongoBD,您也不需要发布27017端口。

If you want to route traffic from nginx to app you have to use ip address or dns of the app container on proxy_pass . 如果要将流量从nginx路由到app ,则必须在proxy_pass上使用ip地址或app容器的dns。 With Docker Compose services can discover each other with service names, so change in nginx conf 使用Docker Compose服务可以使用服务名称发现彼此,因此更改nginx conf

proxy_pass http://app:7770;

You don't need to publish port 7770 to outside world. 您不需要将端口7770发布到外部世界。 Also for mongo you don't need to publish 27017 port. 同样对于mongo,您不需要发布27017端口。

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

相关问题 在Node / Angular / Express App中使用nginx作为反向代理的OPTIONS标头拒绝连接 - Connection refused on OPTIONS header in Node/Angular/Express App with nginx as reverse proxy Apache 反向代理到节点 - 连接被拒绝:AH00957 - Apache reverse proxy to Node - Connection refused: AH00957 生产流星应用程序:NginX代理-失败(111:连接被拒绝) - Production meteor app: NginX proxy - failed(111: connection refused) 即使使用长超时配置,具有 NGINX 反向代理的节点似乎也会终止连接 - Node with NGINX reverse proxy seems to kill connection even when configured using long timeout Node.js-Websocket在由Nginx反向代理提供服务时拒绝打开连接 - Node.js - Websocket refuses to open a connection when served by Nginx reverse proxy 尝试将 Traefik 设置为 node.js 和 nginx 的反向代理 - Trying to set up Traefik as a reverse proxy for node.js and nginx nginx 拒绝连接 ec2 实例上的节点应用程序 - nginx refused to connect node app on ec2 instance Nginx反向代理服务Node.js应用程序静态文件 - Nginx reverse proxy Serving Node.js app static file 为 Node.js 应用程序配置 Nginx 反向代理 - Configuring Nginx Reverse Proxy for Node.js app Nginx 作为 CentOS 7 上 Node.js 应用程序的反向代理 - Nginx as reverse proxy for Node.js app on CentOS 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM