You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx配置报错:stream指令位置非法,求FiveM反向代理解决方案

解决FiveM反向代理Nginx配置错误问题

核心错误原因

你遇到的"stream" directive is not allowed here语法错误,根源是**stream块不能嵌套在http块内部**——Nginx的配置结构要求stream和http是同级的顶级配置块,必须放在events块之后、全局配置条目之下。

修正后的完整Nginx配置

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
}

# HTTP块:处理FiveM服务器列表查询、网页类HTTP流量
http {
    sendfile on;
    tcp_nopush on;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip on;

    server {
        listen 80; 
        listen [::]:80;
        server_name play.domain.gg; 

        location / { 
            proxy_pass http://1.1.1.1:30120;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

# Stream块:处理FiveM核心游戏的TCP/UDP流量(必须与http块同级)
stream {
    upstream fivem_backend {
        server 1.1.1.1:30120;
    }

    # 处理TCP协议流量
    server {
        listen 30120;
        proxy_pass fivem_backend;
        proxy_timeout 3h; # 适配游戏长连接会话,避免被强制断开
    }

    # 处理UDP协议流量(FiveM游戏通信核心依赖UDP)
    server {
        listen 30120 udp reuseport;
        proxy_pass fivem_backend;
        proxy_timeout 3h;
    }
}

关键修正与注意事项

  1. 配置结构修正:将stream块移出http块,作为顶级配置块,这是解决语法错误的核心操作。
  2. 连接超时设置:添加proxy_timeout 3h,防止Nginx主动断开游戏长连接会话。
  3. 防火墙与端口验证:确保服务器防火墙开放30120端口的TCP和UDP流量,同时Nginx所在机器的端口转发规则正常。
  4. FiveM服务器配置:在server.cfg中设置sv_hostname play.domain.gg,确保服务器列表显示域名而非IP。
  5. 测试与重启:执行nginx -t验证配置语法无误后,重启Nginx服务:systemctl restart nginx。
  6. 玩家连接方式:玩家直接输入play.domain.gg:30120即可连接服务器,若想省略端口,需将stream监听端口改为80/443,但需注意避免与HTTP服务冲突。

内容的提问来源于stack exchange,提问作者ViorisE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 04:10:30