pyCthon
pyCthon

Reputation: 12341

Docker+Nginx allow upstream gunicorn from outside docker

I have a docker-compose container running Nginx that I'd like to use with Flask + Gunicorn running outside of docker on the same machine. When I run both inside Docker everything runs fine via the docker-compose.yml links, however when I run Flask + Gunicorn outside of Docker I get an error that I want to resolve. How can I fix this error?

The error I get is

2020/07/17 03:24:49 [error] 38#38: *1 connect() failed (111: Connection refused) while connecting to upstream, client: *.*.*.*, server: website.com, request: "GET / HTTP/2.0", upstream: "http://127.0.0.1:5000/", host: "website.com"

I'm running gunicorn via gunicorn --bind 0.0.0.0:5000 wsgi:app

My nginx website.conf

upstream hello_server {
    #server 127.0.0.1:5000;
    server 0.0.0.0:5000;
}

    server {
        listen 80;
        # ...

        location ^~ /static/ {
            # Path of your static files
            root /var/www;
        }

        location / {
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://frontends;
        }
    }
}

My docker-compose.yml

version: '3'

services:

  web:
    image: nginx:latest
    ports:
      - 80:80/tcp
      - 443:443/tcp

Upvotes: 1

Views: 279

Answers (1)

Adiii
Adiii

Reputation: 59896

You can use HOST IP, for example, the upstream is running on port 5000, all you need to point frontend to HOST IP for example 192.168.0.1.

 server {
        listen 80;
        location ^~ /static/ {
            # Path of your static files
            root /var/www;
        }

        location / {
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://192.168.0.1:5000;
        }
    }

or with upstream variable

upstream gunicornapp {
    server 192.168.43.84:3000;
}

    server {
        listen 80;
        location ^~ /static/ {
            # Path of your static files
            root /var/www;
        }

        location / {
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://gunicornapp;
        }
    }

Upvotes: 1

Related Questions