1

I'm setting up the reverse proxy with Docker and Nginx.

I used the page https://example.org/ for an example.

  1. I set up the custom domain with local mapping in /etc/hosts 127.0.0.1 example.org

  2. I set up the docker-compose file as below

version: '3'
services:
  nginx:
    image: nginx:latest
    container_name: nginx-reverse-proxy
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./nginx-selfsigned.crt:/etc/nginx/nginx-selfsigned.crt
      - ./nginx-selfsigned.key:/etc/nginx/nginx-selfsigned.key
    ports:
      - 80:80
      - 443:443
  1. Then I set up the nginx.conf

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;
    server {
       listen 80 default_server;
       listen 443 ssl;
       ssl_certificate nginx-selfsigned.crt;
       ssl_certificate_key nginx-selfsigned.key;
       server_name example.org;
       add_header Last-Modified $date_gmt;
       proxy_cache_bypass 1;

       location / {
           add_header  X-Upstream  'default docker' always;
           proxy_pass https://example.org;
           proxy_ssl_server_name on;
       }
    }

    include servers/*;
}

But after I run docker compose up

Then I access the page https://example.org

I got 502 bad gateway error with a lot of headers X-Upstream: default docker added which I configured

I don't know which one is wrong in the configuration :( enter image description here

UPDATED:

I'm not finding out the issue yet.

I decided to go with proxy_pass https://ip_address rather than with the domain name. I don't understand why it work with nginx on local machine but not with nginx in docker

taile
  • 2,738
  • 17
  • 29
  • What are you trying to do here? If you just want 127.0.0.1 to redirect to example.org, you shouldn't need that entry in /etc/hosts, because the reverse proxy will handle that redirect for you. – William Edmisten Jul 15 '21 at 13:29
  • I'm trying to append more headers to the request. I did it with nginx (I installed via brew) without docker and it worked. And now I'm trying to do it with docker – taile Jul 15 '21 at 13:32
  • The current config worked for me, but I don't have that /etc/hosts entry. Maybe try removing it? Also, are you getting any errors in your nginx logs? – William Edmisten Jul 15 '21 at 13:52
  • yahh. i think the problem is at `proxy_pass https://example.org` however if I dont set it in /etc/hosts, the request will never come to nginx. I updated `proxy_pass https://google.com` it redirects to this page successfully – taile Jul 15 '21 at 14:31
  • I think I got the same issue here https://stackoverflow.com/questions/63307831/nginx-infinite-redirect-reverse-proxy-with-docker-compose – taile Jul 15 '21 at 15:07
  • I think you have an infinite loop of redirecting from 127.0.0.1 -> example.org -> 127.0.0.1 -> example.org... I tried setting up a similar route with "proxy_pass http://localhost" and also got a 502 gateway error because of this infinite loop. The nginx logs were overflowing with 502 errors. But unlike that link, I believe your loop is because you have example.org redirect to 127.0.0.1 in your /etc/hosts. Could you make requests localhost, instead of example.org? – William Edmisten Jul 15 '21 at 18:05

0 Answers0