Nginx上游故障配置文件

4

我想在我的nginx web服务器上启动我的node服务,但是当我尝试执行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

我目前的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;
    }
}

我尝试查找一些解决方案,但似乎都不适用于我的情况。
编辑:是的,我已经正确编辑了路径和占位符。
1个回答

9
简而言之,upstream指令必须嵌套在http块内。
nginx配置文件通常具有最顶层的eventshttp块,然后是serverupstream和其他指令嵌套在http内。类似于这样:
events {
    worker_connections 768;
}

http {
    upstream foo {
        server localhost:8000;
    }

    server {
        listen 80;
        ...
    }
}

有时候,不是显式地嵌套 server 块,而是将配置文件分散到多个文件中,使用 include 指令来“合并”它们:
http {
    include /etc/nginx/sites-enabled/*;
}

您的配置中没有显示一个封闭的http块,所以您很可能正在针对部分配置运行nginx -t。您应该要么a)将这些封闭块添加到您的配置中,要么b)将此文件重命名并在您的主nginx.conf中发出include来将所有内容汇总。


谢谢!那帮助我更好地理解了它。 - Hi There
很高兴能帮忙! - chris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接