Docker Nginx Proxy Pass Game Server



我正在尝试设置一个Nginx docker容器,以便在数字海洋液滴上运行我的个人网站和游戏服务器。 我希望它example.com是个人网站,gmod.example.com是游戏服务器。个人网站运行良好,我能够获得一个示例节点服务器作为游戏服务器配置的调试尝试,但游戏服务器无法正常工作。 我不确定我的配置出了什么问题。

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

例子.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
}

gmod.example.com.conf:

upstream gmod {
server gmod:27015;
}
server {
listen 27015;
listen [::]:27015;
server_name gmod.example.com www.gmod.example.com;
access_log /etc/nginx/logs/gmod.access;
error_log /etc/nginx/logs/gmod.error;
location / {
proxy_pass http://gmod;
}
}

游戏服务器的 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}

使用当前设置,当我导航到 gmod.example.com 时,它会给我一个 404(这是预期的配置,但最终,我希望它正常工作(。 导航到 gmod.example.com:27015 会在浏览器中502 Bad Gateway。访问日志显示除了我遇到错误的网关外,什么也没发生。 游戏客户端只是报告根本没有服务器。任何帮助将不胜感激。理想情况下,答案是可扩展的,现在我只运行一台服务器,但将来,我希望运行多种不同类型的服务器。

在数字海洋中,我有example.comtest.example.comgmod.example.com的 A 记录(以及每个带有 www. 前缀的记录(。 他们也都指向液滴。

我相信您需要更改配置,因为当您实际需要的是使用 UDP/TCP 进行通信时,您尝试通过 HTTP Web 协议与游戏服务器进行通信。

这是使用ngx_stream_core_module在此处查看更多

内容来完成的另外,仅供参考,如果您使用的是Nginx预构建的安装,则无需执行任何操作,只需 将stream {}添加到http {}之外的nginx.conf文件中

nginx.conf

http {
#web stuff here e.g. Website config
}

stream {        
server {
listen 27015;    
proxy_pass gmod;
}
upstream gmod {
server 127.0.0.1:27015;
}  
}

最新更新