title: NGINX reverse proxy to .netcore app gives bad gateway 502
body: <p>I have an issue with Nginx configuration for reverse proxy. I try to get to the backend using the port http://localhost:5002/ but it gives bad gateway. The container with Nginx is HTTPS secured and the Angular app works and i can reach it from outside the server.</p>
  <pre><code>    # Expires map
  map $sent_http_content_type $expires {
      default                    off;
      text/html                  epoch;
      text/css                   max;
      application/javascript     max;
      ~image/                    max;
  }
  
  
  server {
    listen              443 ssl http2;
    listen              [::]:443 ssl http2;
    server_name         XXX.app *.XXX.app;
    ssl_certificate     /etc/nginx/conf.d/XXX.app.pem;
    ssl_certificate_key /etc/nginx/conf.d/XXX.app.key;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;
    sendfile            on;
    default_type        application/octet-stream;
      
    root /usr/share/nginx/html;
    index index.html;
    try_files $uri $uri$args $uri$args/ $uri/ /index.html;
  
    error_log /var/log/nginx/angular4_error.log;
    access_log /var/log/nginx/angular4_access.log;
    
    location /api {
      proxy_pass http://localhost:5002;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection keep-alive;
      proxy_set_header Host $host;
      proxy_cache_bypass $http_upgrade;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
    }
    
    gzip on;
    gzip_http_version 1.0;
    gzip_disable      &quot;MSIE [1-6]\.&quot;;
    gzip_min_length   256;
    gzip_vary         on;
    gzip_proxied      expired no-cache no-store private auth;
    gzip_types        text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_comp_level   9;
  }
  </code></pre>
  <p>Here is the error i get in the log when i want to login for example</p>
  <pre><code>2021/06/01 13:10:54 [error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 86.124.XXX.XX ,server: XXX.app, request: &quot;POST /api/login HTTP/2.0&quot;, upstream: &quot;http://127.0.0.1:5002/api/login&quot;, host: &quot;XXX.app&quot;, referrer: &quot;https://XXX.app/auth/login&quot;
  </code></pre>
  <p>Nginx is hosted in a container with the Angular app and the backend is in a different container.
  What i want to achieve, is to have the connection to the backend using the reverse proxy and everything containing <a href="https://XXX.app/api" rel="nofollow noreferrer">https://XXX.app/api</a> to be redirected to the backend.</p>
  <p>Tnx!</p>
  <p>Later edit: Here is my docker-compose.yml</p>
  <pre><code>version: '3.4'
  
  services:
    XXX-db:
      image: postgres:10.5
      container_name: XXX-db
      restart: always
      command: postgres -c 'max_connections=200'
      ports:
        - 5432:5432
      environment:
          POSTGRES_USER: x
          POSTGRES_PASSWORD: x
          POSTGRES_DB: x
      volumes:
        - pgdata:/var/lib/postgresql/data
      networks:
        - main
  
    app.api:
      image: xx/xx:api-dev
      container_name: XXX-web-api
      build:
        context: .
        dockerfile: Dockerfile
      volumes:
        - logs:/app/Logs
        - templates:/app/wwwroot/templates
        - feedbacks:/app/wwwroot/feedbacks
      links:
        - XXX-db
      depends_on:
        - &quot;XXX-db&quot;
      ports:
        - 5002:5002
      networks:
        - main
  
    app.web:
      build:      
        context: .
        dockerfile: Dockerfile
      volumes:
        - certs:/etc/nginx/certs
      image: xx/xx:web-dev
      container_name: XXX-web
      ports:
        - 80:80
        - 443:443
      networks:
        - main
  
  volumes:
    pgdata:
    logs:
    templates:
    feedbacks:
    certs:
  
  networks:
    main:
      driver: bridge
  </code></pre>