Nginx上游故障配置文件

时间:2018-11-24 02:43:27

标签: nginx

我正在尝试在Nginx Web服务器上启动我的节点服务,但是当我尝试执行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 个答案:

答案 0 :(得分:1)

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


nginx配置文件通常在最顶层具有eventshttp块,然后serverupstream和嵌套在{{1}中的其他指令}。像这样:

http

有时候,不是将配置显式地嵌套在events { worker_connections 768; } http { upstream foo { server localhost:8000; } server { listen 80; ... } } 块中,而是将配置分散到多个文件中,并且使用server指令将它们“合并”在一起:

include

您的配置没有向我们显示一个封闭的http { include /etc/nginx/sites-enabled/*; } 块,因此您很可能针对部分配置运行http。您应该a)将那些封闭的块添加到配置中,或者b)重命名此文件并在主nginx -t中为其发出一个include,以将所有内容组合在一起。