Nginx上游故障配置文件

Nginx上游故障配置文件,nginx,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

我正在尝试在我的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;
    }
}
events {
    worker_connections 768;
}

http {
    upstream foo {
        server localhost:8000;
    }

    server {
        listen 80;
        ...
    }
}
我试图寻找一些解决办法,但似乎没有任何办法适合我的情况

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

tldr;上游指令必须嵌入到http块中

nginx配置文件通常在最顶层包含事件和http块,然后在http中嵌套服务器、上游和其他指令。大概是这样的:

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;
    }
}
events {
    worker_connections 768;
}

http {
    upstream foo {
        server localhost:8000;
    }

    server {
        listen 80;
        ...
    }
}
有时,不是显式嵌套服务器块,而是将配置分布在多个文件中,并使用include指令将它们合并在一起:

http {
    include /etc/nginx/sites-enabled/*;
}
您的配置没有向我们显示一个封闭的http块,因此您很可能在部分配置上运行nginx-t。您应该将这些封闭块添加到配置中,或者重命名此文件,并在主nginx.conf中发布include for,以将所有内容整合在一起。

tldr;上游指令必须嵌入到http块中

nginx配置文件通常在最顶层包含事件和http块,然后在http中嵌套服务器、上游和其他指令。大概是这样的:

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;
    }
}
events {
    worker_connections 768;
}

http {
    upstream foo {
        server localhost:8000;
    }

    server {
        listen 80;
        ...
    }
}
有时,不是显式嵌套服务器块,而是将配置分布在多个文件中,并使用include指令将它们合并在一起:

http {
    include /etc/nginx/sites-enabled/*;
}

您的配置没有向我们显示一个封闭的http块,因此您很可能在部分配置上运行nginx-t。您应该将这些封闭块添加到配置文件中,或者重命名此文件,并在主nginx.conf中为其发布一个include,以将所有内容整合在一起。

谢谢!这帮助我更好地理解它。谢谢!这有助于我更好地理解它。