nginx:在/etc/nginx/nginx.conf:16中不允许[emerg]“位置”指令

时间:2019-10-11 21:06:19

标签: nginx centos plesk

我是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;

看起来正确。服务器指令应该在http中。而http是父指令。我在做什么错了?

1 个答案:

答案 0 :(得分:0)

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

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

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

如果您刚刚开始使用,也许这是一个很好的资源:http://nginx.org/en/docs/beginners_guide.html