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

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

我的 Web 面板页面需要 .htaccess 文件,由于我现在使用 Nginx,因此我必须转换 .htaccess 重写以适用于 Nginx。我使用了在线转换器http://winginx.com/en/htaccess它告诉我要输入的内容,但当我重新启动 Nginx 以保存更改时收到错误。

原始.htaccess 重写:

Options -Indexes
# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)(\.)(.*)$ index.php?url=$1.$3 [L,QSA]
RewriteRule ^ajax$ _res/ajax.php [QSA]
#RewriteRule ^(.*)$ index.php?t=$1 [L,QSA]
</IfModule>

转换:

# nginx configuration
location = /favicon.ico {
}
autoindex off;
location / {
if (!-e $request_filename){
rewrite ^/(.*)(\.)(.*)$ /index.php?url=$1.$3 break;
}
}
location = /ajax {
rewrite ^(.*)$ /_res/ajax.php;
}

带有转换代码的配置文件:

# redirect from non-www to www 
# uncomment, save file and restart Nginx to enable
#server {
#            listen   80;
#            server_name domain.com;
#            return 301 $scheme://www.domain.com$request_uri;
#       }

server {
# Cloudflare
set_real_ip_from 199.27.128.0/21;
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20; 
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 104.16.0.0/12;
real_ip_header CF-Connecting-IP;

  server_name domain.com www.domain.com;

# ngx_pagespeed & ngx_pagespeed handler
#include /usr/local/nginx/conf/pagespeed.conf;
#include /usr/local/nginx/conf/pagespeedhandler.conf;
#include /usr/local/nginx/conf/pagespeedstatslog.conf;


  # limit_conn limit_per_ip 16;
  # ssi  on;

  access_log /home/nginx/domains/domain.com/log/access.log combined buffer=32k;
  error_log /home/nginx/domains/domain.com/log/error.log;

  root /home/nginx/domains/domain.com/public;

  location / {

# block common exploits, sql injections etc
#include /usr/local/nginx/conf/block.conf;

  # Enables directory listings when index file not found
  #autoindex  on;

  # Shows file listing times as local time
  #autoindex_localtime on;

  # Enable for vBulletin usage WITHOUT vbSEO installed
  #try_files        $uri $uri/ /index.php;

  }

  include /usr/local/nginx/conf/staticfiles.conf;
  include /usr/local/nginx/conf/php.conf;
  include /usr/local/nginx/conf/drop.conf;
  #include /usr/local/nginx/conf/errorpage.conf;
}

# nginx configuration
location = /favicon.ico {
}
autoindex off;
location / {
if (!-e $request_filename){
rewrite ^/(.*)(\.)(.*)$ /index.php?url=$1.$3 break;
}
}
location = /ajax {
rewrite ^(.*)$ /_res/ajax.php;
}

错误:

[root@radio ~]# nginx: [emerg] "location" directive is not allowed here in /usr/local/nginx/conf/conf.d/domain.com.conf:65

总而言之,我不确定我是否将代码放在了正确的位置。但到目前为止,我得到了如上所示的错误。

谢谢你!

答案1

你的location块放错了位置。它们必须位于server块内。

我建议你更仔细地研究 nginx 配置,以便理解整个概念。否则你将来只会遇到问题。

相关内容