![Einzelnen Standort basierend auf dem Servernamen in Nginx ändern](https://rvso.com/image/760963/Einzelnen%20Standort%20basierend%20auf%20dem%20Servernamen%20in%20Nginx%20%C3%A4ndern.png)
Ich habe einen Servereintrag in meinen Konfigurationen mit etwa 50 Standorteinträgen. Ich muss eine andere Domäne auf meinem Server mit derselben Konfiguration bis auf einen Standort definieren
ich habe zum Beispiel
server {
# the port your site will be served on
# the domain name it will serve for
listen 443 ssl;
server_name example.com sudomain.example.com;
ssl_certificate /etc/loc/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/loc/privkey.pem; # managed by Certbot
proxy_set_header X-Forwarded-Proto https;
location /static/ {
root /srv/sites/example;
}
... # many more location defenition
}
ich muss so etwas tun wie
location /robots.txt {
if ($host ~ ^\w+\.\w+\.\w+$) {
# subdomains
alias /srv/robots_disallow.txt;
} else {
alias /srv/robots.txt;
}
}
Wenn möglich, möchte ich vermeiden, die gesamte Konfiguration in einen Snippet zu extrahieren und sie dann in zwei Servereinträge einzuschließen, einen für die Hauptdomäne und einen für die Subdomäne.
Ich weiß, dass der Code, den ich kopiert habe, nicht funktioniert und ich habe gelesenwenn es böse ist
was auf etwas schließen lässt
error_page 418 = @disallow_robots;
location /robots.txt {
alias /srv/robots.txt;
if ($host ~ ^\w+\.\w+\.\w+$) {
# subdomains
return 418;
}
}
location @disallow_robots {
alias /srv/robots_disallow.txt;
}
Aber dann bekomme ichthe "alias" directive cannot be used inside the named location
Antwort1
map
Mit einer und einer Anweisung erreichen Sie eine sauberere Lösung try_files
.
Zum Beispiel:
map $host $robots {
~^\w+\.\w+\.\w+$ /robots_disallow.txt;
default /robots.txt;
}
server {
...
location = /robots.txt {
root /srv;
try_files $robots =404;
}
...
}
Sehendieses Dokumentfür Details.