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

如何解决Nginx中“upstream”指令不允许在此处的报错?

解决Nginx启动报错:"upstream" directive is not allowed here

问题原因

你的Nginx配置文件缺少最外层的main全局配置块,直接以http {开头,导致Nginx解析配置时结构混乱,误判upstream指令处于非法位置。

修复步骤

  1. 在配置文件开头添加基础的全局配置指令(可根据实际环境调整参数)
  2. 修正第二个server_name后的多余分号(避免后续触发其他语法错误)

修正后的完整配置

# 全局基础配置(参数可按需调整)
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

http {
    upstream lb{
        server directory_back1:8000;
        server directory_back2:8000;
        server directory_back3:8000;
    }

    server {
        listen 80;
        server_name df.dotapp.app;

        location / {
            proxy_pass http://directory_front:8000;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_redirect off;
        }
    }

    server {
        listen 80;
        server_name db.dotapp.app;

        location / {
            proxy_pass http://lb;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_redirect off;
        }
    }
}

验证配置

修复后执行以下命令检查配置合法性:

nginx -t

若输出nginx: configuration file /etc/nginx/nginx.conf test is successful,即可正常启动Nginx。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 06:52:35