Egor Richman
Egor Richman

Reputation: 679

Upstream timed out error when deploying Docker Nginx FastAPI application on Google Cloud

I'm trying to deploy simple FastAPI app with Docker and Nginx proxy on Google Cloud using simple ssh-terminal window.

My nginx.conf:

access_log                  /var/log/nginx/app.log;
error_log                   /var/log/nginx/app.log;

proxy_headers_hash_max_size 512;
proxy_headers_hash_bucket_size 128;

proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $proxy_connection;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
proxy_set_header X-Original-URI $request_uri;
proxy_set_header Proxy "";

upstream app_server {
  server example.com:8000;
}

server {
  server_name               example.com;
  listen                    80;
  return 301 https://$host$request_uri;
}

server {
  listen                  443 ssl;
  server_name             example.com;
  ssl_certificate         /root/ssl/cert.pem;
  ssl_certificate_key     /root/ssl/key.pem;
  location / {
    proxy_pass "http://app_server";
  }
}

My docker-compose.yml:

version: '3.8'

services:
  reverse-proxy:
    image: jwilder/nginx-proxy
    container_name: reverse-proxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - ./nginx:/etc/nginx/conf.d
      - ./ssl/cert1.pem:/root/ssl/cert.pem
      - ./ssl/privkey1.pem:/root/ssl/key.pem
      - ./ssl/dhparam.pem:/etc/nginx/dhparam/dhparam.pem
    networks:
      - reverse-proxy

  web:
    environment: [.env]
    build: ./project
    ports:
      - 8000:8000
    command: gunicorn main:app -k uvicorn.workers.UvicornWorker -w 2 -b 0.0.0.0:8000
    volumes:
      - ./project:/usr/src/app
    networks:
      - reverse-proxy
      - back

networks:
  reverse-proxy:
    external:
      name: reverse-proxy
  back:
      driver: bridge

After run docker-compose up command and going to example.com address, I get error:

*3 upstream timed out (110: Connection timed out) while connecting to upstream...

Also, I have opened ports with Google Cloud Firewall service (checked with netstat command) and configured my VM's instance with network parameters from this article.

I don't understand why I receive 504 Gateway Time-out cause my service work with the similar configuration on a simple VPS hosting, and also it works from the inside Google Cloud VM's ssh-terminal when using curl and check localhost instead example.com domain. I want to know how to run my service on Google Cloud VM using only docker-compose util for this purpose?

Upvotes: 1

Views: 1079

Answers (1)

Elias Nichupienko
Elias Nichupienko

Reputation: 36

In Nginx config file, try to mention the web container name:

upstream app_server {
  server web:8000;
}

Upvotes: 2

Related Questions