small_server_thingies/nginx/subdomains_and_reverse_proxy.md
2022-01-29 19:47:39 +01:00

1.5 KiB

Subdomain and reverse proxy

Now that I have my own domain, life is pretty cool. I can leave behind the harsh days of typing my server IPs in the browser, and point to my services with nice names.

Through porkbun's UI, I can point my domain name to any ip. But, I can't specify anything related to ports! After some research, I found out what I could do is to specify different subdomains in porkbun, and set up a reverse proxy in nginx to direct traffic thanks to this subdomains. Basically, I tell porkbun to direct each subdomain to my IP, and then I set up the nginx running in navaja to point to different docker containers depending on the subdomain that is getting requested.

This is an example on how to set the reverse proxy config in nginx:

    server {
        listen 80;
        server_name git.contrapeso.xyz;
        location / {
            proxy_set_header Host $host;
            proxy_pass       http://gogs_web_1:3000;
            proxy_redirect off;
        }
    }

Where git.contrapeso.xyz is the subdomain and the contents of proxy_pass is where requests should be redirected.

Something not obvious to take into account: since my nginx is running within a docker container, networking within navaja is a bit tricky. The way I got it right is to:

  • Make sure that the nginx container is running in the same network as the one where traffic should be redirected.
  • Redirect using the container name and the listening port within the container, not the redirecting one in the host.