1

我从看似有效的 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 不同:未知指令“位置”,因为该指令位于server块内。

请注意,“mydomain.com”已替换为我的实际域,并且之前几乎相同的配置已经工作过很多次。这个配置和之前的配置的主要区别是ssl_certificate*设置指向了不同的位置,并且没有,/.well-known/acme-challenge/因为我之前没有使用过certbot/let's encrypt in nginx/docker。

有任何想法吗?

4

1 回答 1

1

In the error message:

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

Nginx is reading the quoted text as a single directive, so it has not recognise the gap between the location and / parts.

The gap should be a white-space character (e.g. ASCII space 20h). It's very probably that you are missing a space separator or your editor has inserted an invisible formatting character that is not a normal ASCII space.

于 2021-02-03T14:32:43.737 回答