Question

Hosting Server Through Nginx Reverse proxy

Posted January 18, 2020 4.9k views
NginxDocker

Hello, I have a domain example.com, and I want that to be my regular website. However, I want to run a gameserver for some friends and I hosted on gmod.example.com, and I can’t seem to get that working. I’ve tried a variety of combinations of nginx config files, and I’m not sure where the issue is. Currently, my config has gmod.example.com:27015 listening, but even if I go to that address, the game still won’t connect.

docker-compose.yml:

version: '3'

services:
    reverseproxy:
        container_name: reverseproxy
        hostname: reverseproxy
        image: nginx
        ports:
          - 80:80
          - 443:443
          - 27015:27015
        volumes:
          - ~/test/nginx/:/etc/nginx/
          - ~/test/sslcerts/:/etc/letsencrypt/
          - ~/test/websites:/var/www/
          - /etc/ssl/certs/dhparam.pem:/etc/ssl/certs/dhparam.pem
        depends_on:
          - gmod
    gmod:
        container_name: gmod
        hostname: gmod
        build:
            context: ~/test/game_servers/gmod

gmod.example.com.conf:

upstream gmod {
        server gmod:27015;
}

server {
        listen 27015;
        listen [::]:27015;
        server_name example.com www.example.com;

        location / {
                proxy_pass http://gmod;
        }
}

example.com.conf:

server {
        listen 443 ssl http2; #Certbot
        listen [::]:443 ssl http2 ipv6only=on; #Certbot
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; #Certbot
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; #Certbot

        ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
        #include /etc/nginx/snippets/header.conf;
        #include /etc/nginx/snippets/ssl.conf;

        server_name example.com www.example.com;
        root /var/www/example.com;

        index index.html;
        location / {
                include /etc/nginx/snippets/header.conf;
                try_files $uri $uri/ =404;
        }

        #for certbot renewal
        location ~ /.well-known/acme-challenge {
                allow all;
                root /data/letsencrypt;
        }
}

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        if ($host = www.example.com) {
                return 301 https://$host$request_uri;
        } #certbot

        if ($host = example.com) {
                return 301 https://$host$request_uri;
        } #certbot

        server_name example.com www.example.com;
        return 404; # certbot
}

Game Dockerfile:

FROM cm2network/steamcmd:root

ENV STEAMAPPID 4020
ENV STEAMAPPDIR /home/steam/gmod
ENV MAP gm_flatgrass
ENV GAMEMODE sandbox

# Run Steamcmd and install Gmod
RUN set -x \
        && "${STEAMCMDDIR}/steamcmd.sh" \
                +login anonymous \
                +force_install_dir ${STEAMAPPDIR} \
                +app_update ${STEAMAPPID} validate \
                +quit

USER steam

WORKDIR $STEAMAPPDIR

VOLUME $STEAMAPPDIR

# Set the entrypoint:
# 1) Update the server
# 2) Run the server
ENTRYPOINT ${STEAMCMDDIR}/steamcmd.sh \
                +login anonymous +force_install_dir ${STEAMAPPDIR} +app_update ${STEAMAPPID} +quit \
                && ${STEAMAPPDIR}/srcds_run \
                        -game garrysmod -maxplayers 16 +gamemode ${GAMEMODE} +map ${MAP}

The ports the game server uses are 27015 and 27005, both TCP and UDP. Something is clearly working because if I open a web browser and go to gmod.example.com:27015, I get a 502 Bad Gateway error.

For more context, though I don’t think it’s necessary for the problem:
The game I’m trying to run is Garry’s mod, and I’m doing it with the SteamCMD Docker Container.

I tried using my config on a sample node application, and that worked, so I don’t know how else to do this. It’s very possible the game client is programmed weird and won’t accept subdomains (when I add the subdomain to the game client, it just reformats it to my droplet-ip:27015.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi @macstation,

I could suggest a couple of things:

  • Make sure that your game server container is actually running, if you try to visit your gmod container directly does it work?

  • Check your Nginx container logs, this should give you more information regarding the exact problem rather than the generic 502 error:

docker logs nginx_container_id
  • Make sure that the two containers are part of the same network so that they communicate to each other via their hostnames

Let me know how it goes!
Regards,
Bobby