未知指令 "location /"(在服务器块内)

unknown directive "location /" (inside server block)

我从看似有效的 nginx 配置中收到以下错误:

nginx: [emerg] unknown directive "location /" in /etc/nginx/conf.d/default.conf:49

这是我 default.conf 的相关部分:

upstream channels-backend {
    server api:5000;
}

server {
    listen 80;
    listen [::]:80;
    server_name _;
    server_tokens off;

    location = /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl default_server;
    server_name _;
    server_tokens off;

    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
    # include /etc/letsencrypt/options-ssl-nginx.conf;
    # ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
    # ssl_password_file /etc/letsencrypt/live/mydomain.com/global.pass;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    ssl_protocols TLSv1.2 TLSv1.1;

    access_log /var/log/nginx/access.log;
    underscores_in_headers on;

    location = /favicon.ico {
        access_log off;
        log_not_found off;
    }

    location /static {
        alias /home/docker/code/static;
    }

    location / {  # this is line 49
        try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
        proxy_read_timeout 30;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_redirect off;
        proxy_pass http://channels-backend;
    }
}

我的问题与 nginx : unknown directive "location" 不同,因为该指令位于 server 块内。

请注意,“mydomain.com”已替换为我的实际域,并且几乎相同的配置以前曾多次运行。这个配置和之前配置的主要区别在于ssl_certificate*设置指向不同的位置,没有/.well-known/acme-challenge/因为我没有在[=31]中使用certbot/let的加密=]之前。

有什么想法吗?

错误信息中:

nginx: [emerg] unknown directive "location /" in /etc/nginx/conf.d/default.conf:49

Nginx 将引用的文本作为单个指令读取,因此它无法识别 location/ 部分之间的差距。

间隙应该是一个白色-space字符(例如ASCII space 20h)。很可能您缺少 space 分隔符,或者您的编辑器插入了一个不可见的格式字符,该字符不是普通的 ASCII space.