question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Need Help: How to configure nginx correctly, nginx responds 502

See original GitHub issue

nginx-proxy-compose.yml

version: '3.8'

services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
    ports:
      - "80:80"
    networks:
      - proxynet

networks:
  proxynet:
    name: mynetwork

Note: Run the Nginx proxy container first.

app-compose.yml

version: '3.8'

services:
  stage:
    container_name: xxx-website-stage
    image: registry.gitlab.com/xxx.us/website:latest
    build:
      context: .
      target: stage
      dockerfile: Dockerfile
    expose:
      - "80"
    restart: always
    volumes:
      - /etc/nginx/conf.d/staging.xxx.us.conf:/etc/nginx/conf.d/default.conf:ro
    environment:
      VIRTUAL_HOST: staging.xxx.us
    networks:
      - my-proxy-net

networks:
  my-proxy-net:
    external:
      name: mynetwork

/etc/nginx/conf.d/staging.xxx.us.conf

server {

  listen 80;
  listen [::]:80;

  server_name localhost;

  root /usr/share/nginx/html;

  access_log /var/log/nginx/localhost.access.log;
  error_log /var/log/nginx/localhost.error.log debug;

  index index.html index.htm;

  location / {
    try_files $uri $uri/ =404;

    expires -1;
  }

  location ~* \.(?:css|js)$ {
    access_log off;
    log_not_found off;

    add_header Cache-Control "no-cache, public, must-revalidate, proxy-revalidate";
  }

  location ~* \.(?:jpg|jpeg|gif|png|ico|xml)$ {
    access_log off;
    log_not_found off;

    add_header Cache-Control "public";
  }

  location ~* \.(?:eot|woff|woff2|ttf|svg|otf) {
    access_log off;
    log_not_found off;

    add_header Cache-Control "public";

    add_header Access-Control-Allow-Origin *;

    types {
      font/opentype otf;
    }
    types {
      application/vnd.ms-fontobject eot;
      }
    types {
      font/truetype ttf;
    }
    types {
      application/font-woff woff;
    }
    types {
      font/x-woff woff2;
    }
}

location ~ /\. {
    access_log off;
    log_not_found off;
    deny all;
  }

}

Dockerfile

# dev stage
FROM node:14.4.0-alpine AS dev

# set working directory
WORKDIR /var/www/

# install app dependencies
COPY package.json package-lock.json ./
RUN npm ci --silent

# add app
COPY . ./

# builder stage
FROM dev AS builder

RUN npm run build:app

# stage stage
FROM nginx:1.19.1-alpine AS stage

# Remove default files created by Nginx
RUN rm -rvf /usr/share/nginx/html/*
RUN rm -vf /etc/nginx/conf.d/default.conf

COPY --from=builder /var/www/build/ /usr/share/nginx/html

CMD ["nginx-debug", "-g", "daemon off;"]

sudo docker ps

CONTAINER ID        IMAGE                                          COMMAND                  CREATED             STATUS              PORTS                NAMES
...        ...   "/docker-entrypoint.…"   ...      ...       80/tcp               xxx-website-stage
...        jwilder/nginx-proxy                            "/app/docker-entrypo…"   ...   ...       0.0.0.0:80->80/tcp   docker-composes_nginx-proxy_1

/etc/hosts

127.0.0.1 localhost
127.0.0.1 staging.xxx.us

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

Update 1

Modify /etc/hosts in the server and mappings as follows.

127.0.0.1 domain.com my.domain.com

P.S. I tried the Google Group, but I don’t have enough access to post a question, so I decided to post it here.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
tkw1536commented, Jul 14, 2020

You probably want to either put them both in the same .yml file, or explicitly declare an external network they both connect to.

1reaction
menesscommented, Jul 14, 2020

@tkw1536 You saved me! I was searching and trying to fix the issue for 2 weeks! I also updated my question to help others

Read more comments on GitHub >

github_iconTop Results From Across the Web

502 bad gateway in Nginx: Top 5 reasons for it, & how to resolve
502 Bad Gateway Nginx commonly occurs when Nginx runs as a reverse proxy, and is unable to connect to backend services.
Read more >
How To Fix '502 Bad Gateway' Error In Nginx | LinuxHostSupport
The 502 Bad Gateway error is an HTTP status code that means that one server received an invalid response from another server.
Read more >
How to Fix NGINX 502 Errors Now! - Adam the Automator
In this tutorial, you'll learn how to fix the NGINX 502 errors in this practical, scenario-based tutorial featuring NGINX and a PHP-FPM ...
Read more >
How to Fix 502 Bad Gateway Error in NGINX - Ubiq BI
If your back end server is not configured properly, then it might restart repeatedly and cause NGINX to give 502 Bad Gateway response....
Read more >
502 bad gateway nginx - DigitalOcean
Then check the status agian and make sure that nginx remains running. If nginx did not start after a reboot, you could enable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found