【问题标题】:nginx: [emerg] "location" directive is not allowed here in /etc/nginx/nginx.conf:16nginx: [emerg] "location" 指令在 /etc/nginx/nginx.conf:16 中是不允许的
【发布时间】:2020-02-09 09:44:58
【问题描述】:

我是 NGINX 的新手,我正在尝试设置最小的工作。所以我尝试用 nginx 和主管运行 https(通过这个例子)。但我无法正确配置 Nginx 并收到以下错误:

Error Code: nginx: [emerg] "location" directive is not allowed here in /etc/nginx/nginx.conf:16 nginx: configuration file /etc/nginx/nginx.conf test failed . 



 GNU nano 2.0.9          File: /etc/nginx/nginx.conf                           

#user  nginx;
worker_processes  1;

#error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

#pid        /var/run/nginx.pid;

include /etc/nginx/modules.conf.d/*.conf;

events {
    worker_connections  1024;
}
location ~^/wp-json/ {
    rewrite ^/wp-json/(.*?)$ /?rest_route=/$1 last;
}
http {
    include   mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    #tcp_nodelay        on;

    #gzip  on;

 #gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    server_tokens off;

    include /etc/nginx/conf.d/*.conf;
}

# override global parameters e.g. worker_rlimit_nofile
include /etc/nginx/*global_params;

看起来是正确的。 server 指令应该在 http 中。 http 是父指令。我做错了什么?

【问题讨论】:

  • 关于location 的文档明确提到位置只能在server 上下文中定义。 @Chris 的回答是正确的。此外,对于 wordpress,任何虚拟主机都有大量的 nginx 示例。

标签: nginx centos plesk


【解决方案1】:

不太清楚你所说的“服务器指令在 http”是什么意思 - 我假设你指的是 /etc/nginx/conf.d/*.conf 中包含的文件中的内容。

无论如何,如果您想在其中一个包含的文件中增强server 指令,您必须在那里进行。如果您想定义一个新的服务器,您需要在http 中添加一个块。在该块中,您将添加您的 location 块。

http {
  #...
  server {
    #...
    location ~^/wp-json/ {
      #...
      rewrite ^/wp-json/(.*?)$ /?rest_route=/$1 last;
      #...
    }
    #...
  }
  #...
}

如果您刚刚开始,也许这是一个很好的资源:@​​987654321@?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    相关资源
    最近更新 更多