![nginx: [emerg] Die Direktive „location“ ist hier nicht zulässig](https://rvso.com/image/658822/nginx%3A%20%5Bemerg%5D%20Die%20Direktive%20%E2%80%9Elocation%E2%80%9C%20ist%20hier%20nicht%20zul%C3%A4ssig.png)
Die Seiten meines Webpanels erfordern eine .htaccess-Datei und da ich jetzt Nginx verwende, muss ich die .htaccess-Umschreibung so konvertieren, dass sie mit Nginx funktioniert. Ich habe einen Online-Konverter verwendethttp://winginx.com/en/htaccessund es hat mir gesagt, was ich eingeben soll, und ich erhalte eine Fehlermeldung, wenn ich Nginx neu starte, um die Änderungen zu speichern.
Ursprüngliche .htaccess-Neufassung:
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>
Konvertierung:
# 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;
}
Konfigurationsdatei mit Konvertierungscode:
# 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;
}
Fehler:
[root@radio ~]# nginx: [emerg] "location" directive is not allowed here in /usr/local/nginx/conf/conf.d/domain.com.conf:65
Alles in allem bin ich mir nicht sicher, ob ich den Code an der richtigen Stelle platziere oder was. Aber im Moment erhalte ich den oben gezeigten Fehler.
Danke schön!
Antwort1
Ihre location
Blöcke stehen am falschen Ort. Sie müssen sich innerhalb eines server
Blocks befinden.
Ich empfehle dir, dich viel genauer mit der nginx-Konfiguration auseinanderzusetzen, damit du das ganze Konzept verstehst. Sonst wirst du in Zukunft nur Probleme bekommen.