nginx:[emerg]此处不允许使用“location”(/“server”)指令

nginx:[emerg]此处不允许使用“location”(/“server”)指令

我尝试为两个域创建 2 个单独的 NGINX conf 文件。每个域都有一个 test. 子域,并且出于规范原因,www 应永久重定向到 now-www。正确的做法是什么?以下代码也出现错误。

sudo nginx -T 
nginx: [emerg] "location" directive is not allowed here in /etc/nginx/sites-enabled/example.com:33

这是我的 example.com 的 conf 文件,example2 的第二个文件几乎相同。

server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name example.com 123.456.7.8;

#(For WordPress permalinks)
try_files $uri $uri/ /index.php$is_args$args;
}

# Redirect all traffic to www to non-www for SEO canonical reasons
server {
listen 80;
listen [::]:80;
server_name www.example.com;
location / {
return 301 https://www.example.com/$request_uri;
}
}
# Direct all traffic to the subdomain to a separate folder
server {
listen 80;
listen [::]:80;
root /var/www/test/example.com/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name test.example.com;

#(For WordPress permalinks)
try_files $uri $uri/ /index.php$is_args$args;
}

# include /etc/nginx/naxsi.rules
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location = /favicon.ico { log_not_found off; access_log off;
}
location = /robots.txt { log_not_found off; access_log off; allow all;
}
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$
{
expires max;

#To enable leverage browser caching log_not_found off;
}

答案1

您的配置中有语法错误

...您收到的两个错误消息应该都很清楚。

如果您在配置中使用了缩进,问题就会变得明显且容易发现:

  • 从第 13 行开始的部分server{}没有结尾}
  • 另外,如果您仅添加}第 20 行,则location{}第 33-48 行的部分将成为孤立的。

带缩进的原始配置

您收到此错误消息,指出第 22 行的问题。

nginx: [emerg] "server" directive is not allowed here in 
/etc/nginx/sites-enabled/example.com:22

这不是一个解决方案,而只是为了演示如何解释当前配置:

01: server {
02:     listen 80;
03:     listen [::]:80;
04:     root /var/www/example.com/html;
05:     index index.php index.html index.htm index.nginx-debian.html;
06:     server_name example.com 123.456.7.8;
07:
08:     #(For WordPress permalinks)
09:     try_files $uri $uri/ /index.php$is_args$args;
10: }
11: 
12: # Redirect all traffic to www to non-www for SEO canonical reasons
13: server {
14:     listen 80;
15:     listen [::]:80;
16:     server_name www.example.com;
17:     location / {
18:         return 301 https://www.example.com/$request_uri;
19:     }
20: 
21:     # Direct all traffic to the subdomain to a separate folder
22:     server {
23:         listen 80;
24:         listen [::]:80;
25:         root /var/www/test/example.com/html;
26:         index index.php index.html index.htm index.nginx-debian.html;
27:         server_name test.example.com;
28: 
29:         #(For WordPress permalinks)
30:         try_files $uri $uri/ /index.php$is_args$args;
31:     }
32: 
33:     # include /etc/nginx/naxsi.rules
34:     location ~ \.php$ {
35:         include snippets/fastcgi-php.conf;
36:         fastcgi_pass unix:/run/php/php7.4-fpm.sock;
37:     }
38:     location = /favicon.ico { 
39:         log_not_found off; access_log off;
40:     }
41:     location = /robots.txt { 
42:         log_not_found off; access_log off; allow all;
43:     }
44:     location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
45:         expires max;
46: 
47:         #To enable leverage browser caching log_not_found off;
48:     }

首次尝试修复此问题

...添加}(第 20 行)没有帮助,因为它没有考虑我的第二条建议!

nginx: [emerg] "location" directive is not allowed here in 
/etc/nginx/sites-enabled/example.com:33

现在,您仍然有孤立的location{}指令,位于第 34-47 行:

13: server {
14:     listen 80;
15:     listen [::]:80;
16:     server_name www.example.com;
17:     location / {
18:         return 301 https://www.example.com/$request_uri;
19:     }
20: }
21: # Direct all traffic to the subdomain to a separate folder
22: server {
23:     listen 80;
24:     listen [::]:80;
25:     root /var/www/test/example.com/html;
26:     index index.php index.html index.htm index.nginx-debian.html;
27:     server_name test.example.com;
28: 
29:     #(For WordPress permalinks)
30:     try_files $uri $uri/ /index.php$is_args$args;
31: }
32: 
33: # include /etc/nginx/naxsi.rules
34: location ~ \.php$ {
35:     include snippets/fastcgi-php.conf;
36:     fastcgi_pass unix:/run/php/php7.4-fpm.sock;
37: }
38: location = /favicon.ico { 
39:     log_not_found off; access_log off;
40: }
41: location = /robots.txt { 
42:     log_not_found off; access_log off; allow all;
43: }
44: location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
45:     expires max;
46:     #To enable leverage browser caching log_not_found off;
47: }

解决方案是将这些location{}部分里面其中一个server{}部分。这里没有人能说出哪一个是正确的,因为你没有在问题中解释这一点:它们与你关于在单独的文件中处理两个域的问题完全无关。


最后,清晰的模板配置

此配置示例尝试解决标题中的问题。这是针对的,/etc/nginx/sites-enabled/example.com另一个可能是/etc/nginx/sites-enabled/example.net,如果您将所有的替换example.comexample.net

server {
    listen 80;
    server_name example.com www.example.com test.example.com; 

    # HTTP to HTTPS redirections for all the subdomains
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name www.example.com;
    # ssl_* directives here

    # www to non-www for SEO canonical reasons
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com;
    # ssl_* directives here

    root /var/www/example.com/html;
}

server {
    listen 443 ssl;
    server_name test.example.com;
    # ssl_* directives here

    root /var/www/example.com/test;
}

只需在此处添加location{}所需的部分即可。我还删除了listenIPv6 的附加指令、index指令等,因为它们可能会让您眼花缭乱。一旦您消除了所有噪音,解决方案就非常简单了,不是吗?然后,您就可以微调其余部分了。

答案2

因此,将我的位置添加到服务器块内后,我猜它最终应该如下所示,(现在没有 SLL,因为我稍后会使用 Certbot 添加它,并且 Certbot 无论如何都会自动更改我的代码)。

server {
    listen 80;
    server_name www.example.com;

    # www to non-www for SEO canonical reasons
    return 301 http://example.com$request_uri;
}

server {
    listen 80;
    server_name example.com;

    root /var/www/example.com/html;
    index index.php index.html index.htm index.nginx-debian.html;

    # PHP
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }
    location = /favicon.ico {
        log_not_found off; access_log off;
    }
    location = /robots.txt {
        log_not_found off; access_log off; allow all;
    }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
    }

    # (For WordPress permalinks)
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
}

server {
    listen 80;
    server_name test.example.com;

    root /var/www/example.com/test;
    index index.php index.html index.htm index.nginx-debian.html;

    # PHP
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }
    location = /favicon.ico {
        log_not_found off; access_log off;
    }
    location = /robots.txt {
        log_not_found off; access_log off; allow all;
    }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
    }

    # (For WordPress permalinks)
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
}

相关内容