Nginx 上游故障配置文件

Hi *_*ere 1 nginx

我正在尝试在我的 nginx 网络服务器上启动我的节点服务,但是当我尝试执行 nginx -t 时我不断收到此错误

nginx: [emerg] "upstream" directive is not allowed here in /etc/nginx/nginx.conf:3
nginx: configuration file /etc/nginx/nginx.conf test failed
Run Code Online (Sandbox Code Playgroud)

我目前的 nginx.conf 是这样的:

upstream backend {
    server 127.0.0.1:5555;
}

map $sent_http_content_type $charset {
    ~^text/ utf-8;
}

server {
    listen 80;
    listen [::]:80;

    server_name mywebsite.com;
    server_tokens off;

    client_max_body_size 100M; # Change this to the max file size you want to allow

    charset $charset;
    charset_types *;

    # Uncomment if you are running behind CloudFlare.
    # This requires NGINX compiled from source with:
    #   --with-http_realip_module
    #include /path/to/real-ip-from-cf;

    location / {
        add_header Access-Control-Allow-Origin *;
        root /path/to/your/uploads/folder;
        try_files $uri @proxy;
    }

    location @proxy {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://backend;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_redirect off;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图查找一些解决方案,但似乎对我的情况没有任何作用。

编辑:是的,我确实正确编辑了路径和占位符。

chr*_*ris 6

tldr; 该upstream指令必须嵌入在http块中。


nginx的配置文件通常具有eventshttp在最上面的水平的块,然后serverupstream和其他指令嵌套内部http。像这样的东西:

events {
    worker_connections 768;
}

http {
    upstream foo {
        server localhost:8000;
    }

    server {
        listen 80;
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

有时,不是server明确地嵌套块,而是将配置分布在多个文件中,并且该include指令用于将它们“合并”在一起:

http {
    include /etc/nginx/sites-enabled/*;
}
Run Code Online (Sandbox Code Playgroud)

您的配置未向我们显示封闭http块,因此您很可能nginx -t针对部分配置运行。您应该 a) 将这些封闭块添加到您的配置中,或者 b) 重命名此文件并include在您的主文件中发出一个for 它nginx.conf以将所有内容放在一起。