【问题标题】:unknown directive "location /" (inside server block)未知指令“位置/”(在服务器块内)
【发布时间】:2021-02-03 13:38:45
【问题描述】:

我从看似有效的 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/是因为我之前没有用过certbot/let's encrypt in nginx/docker。

有什么想法吗?

【问题讨论】:

  • Nginx 认为location / 是一个指令。检查分隔 location 和 / 的空格是否是空格字符,并且没有丢失或其他一些字符代码只是看起来不可见。
  • 就是这样(看起来是一些非空格空白字符)。想让它成为答案吗?

标签: nginx nginx-config


【解决方案1】:

在错误信息中:

nginx: [emerg] /etc/nginx/conf.d/default.conf:49 中的未知指令“location /”

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 2014-03-07
    • 2021-05-10
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    相关资源
    最近更新 更多