Nginx Reverse Proxy not redirecting?


#1

I’m trying to setup docker-compose with nginx as a reverse proxy targeting gogs.

If I enter the ip address of the reverse proxy in the address bar, it redirects to gogs and but the ip address is still in the address bar. If I use a host name configured in /etc/hosts then I just get the Nginx welcome page. I’ve have the full details here:

Anyone know if this has something to do with Gogs setup? I’ve filed an issue with the Nginx docker repository here:


#2

Of course… what else do you expect?

Looks like you didn’t config NGINX correctly (or even not at all…)


#3

The configuration works when I’m not running it with docker compose. I filed in issue with them as well. I think the case maybe be that with the proxy configuration has to have the host name set with with an upstream declaration as is done in this article. I’ll be testing it out soon.

Of course… what else do you expect?

Well I worded that one a bit “Captain obviously’ish”, but the point I was making is that the proxy redirect does work in that case. In other words the nginx ip address is 203.0.113.2 and the gogs address is 203.0.113.1 so the redirect is working, and we see 203.0.113.2 in the address bar.

Now all I want to do is set git.example.com in my /etc/hosts to 203.0.113.2 so that I can enter git.example.com in the address bar, and have the redirect work still, but this does not work. In other words when I use a host name that should matches the IP address that nginx does perform the redirect from, then nginx ignores the redirect.

So it should work … but it does not.

One thing I wanted to make sure of is that Gogs does have have any “special” programming where it would ignore requests under certain conditions right? In other words this has to be an nginx or a docker compose issue?


#4

Generally, you can only do one way or other:

  1. Do not touch /etc/hosts but setup NGINX to redirect git.example.com to 203.0.113.1. Besides, your DNS must know git.example.com should be resolved to 203.0.113.2 which is your NGINX IP.
  2. Set your hostname with Gogs IP (in your case 203.0.113.1) and no NGINX involved.

However, if you see welcome page from NGINX, from what I know the domain name you expect is not matched with any proxy config so NGINX serves as a static website and display default HTML. Otherwise, you should see working Gogs page or 502.


#5

Gogs does not care where the request comes from, as long as it could reach the Gogs, Gogs processes it.


#6

Ideally I would set the git.example.com to the gogs ip, but IIUC I then have to use URLs like git.example.com:3000/issues? Can Dockerized Gogs accept incoming connections on port 80 now? I got my setup from a Digital Ocean article a ways back I think…


#7

That’s the entire point of using NGINX in front of Gogs (and any other alike), you can just visit port 80 and let NGINX redirects your request to backend with whatever port is. Then the URL will be very clean without port number.


#8

I’m using /etc/hosts as the DNS and it does resolve the IP correctly, as I always get the nginx container when entering in git.example.com in the address bar.

This is my docker compose file. I’ve changed the network addresses to use 115 instead of 113 so that don’t run into conflicts with the manual setup I had earlier (Not using docker compose):

    version: '3'
    services:
      gogs-nginx:
        build: ./proxy
        ports:
          - "80:80"
        networks:
          mk2net:
            ipv4_address: 203.0.115.2
      gogs:
        image: gogs/gogs
        ports:
          - "3000:3000"
        volumes: 
          - gogs-data:/data
        networks: 
          mk2net:
            ipv4_address: 203.0.115.3
    volumes:
      gogs-data:
        external: true
    networks:
      mk2net:
        ipam:
          config:
            - subnet: 203.0.115.0/24

The build for the proxy nginx container looks like this:

FROM nginx
COPY gogs.conf /etc/nginx/conf.d/gogs.conf
EXPOSE 80

And the gogs.conf nginx proxy configuration looks like this:

    server {
        listen 80;
        server_name 203.0.115.2;
        proxy_set_header X-Real-IP  $remote_addr; # pass on real client IP
        location / {
            proxy_pass http://203.0.115.3:3000;
        }
    }

So in other words listen on 203.0.115.2 and forward to 203.0.115.3 where gogs in running.

Finally /etc/hosts has this line in it such that the address 203.0.115.2 gets resolved per git.example.com:

203.0.115.2 git.example.com

So now git.example.com points to the Nginx proxy.

Now when I docker-compose up it brings up all the containers (Nginx and Gogs):

ole@mki:~/Gogs/.gogs/docker$ docker-compose up
Starting docker_gogs_1       ... done
Starting docker_gogs-nginx_1 ... done

If I visit 203.0.115.2 in the address bar I get the “Welcome to Nginx” screen. The proxy does not trigger. If I visit:

 http://203.0.115.3:3000

I redirects to:

 http://203.0.115.3:3000/user/login

So Gogs is running fine, however I cannot get the proxy part working correctly.


#9

Try server_name git.example.com


#10

Tried it. Still only getting the nginx page:

    server {
        listen 80;
        server_name git.example.com;
        proxy_set_header X-Real-IP  $remote_addr; # pass on real client IP
        location / {
            proxy_pass http://203.0.115.3:3000;
        }
    }

Testing with curl:

    ole@mki:~/Gogs/.gogs/docker$ curl git.example.com
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>

    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>

    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>

#11

Running out of ideas