Tengo una entrada de servidor en mis configuraciones con como 50 entradas de ubicación. Necesito definir otro dominio en mi servidor con la misma configuración excepto por una ubicación.
por ejemplo yo tengo
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
}
necesito hacer algo como
location /robots.txt {
if ($host ~ ^\w+\.\w+\.\w+$) {
# subdomains
alias /srv/robots_disallow.txt;
} else {
alias /srv/robots.txt;
}
}
Si es posible, quiero evitar extraer toda la configuración en un fragmento y luego incluirla en 2 entradas del servidor, una para el dominio principal y otra para el subdominio.
Sé que el código que he copiado no funciona y lo he leídosi es malo
lo que sugiere algo
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;
}
Pero luego entiendothe "alias" directive cannot be used inside the named location
Respuesta1
Conseguirás una solución más limpia con una declaración map
y una try_files
.
Por ejemplo:
map $host $robots {
~^\w+\.\w+\.\w+$ /robots_disallow.txt;
default /robots.txt;
}
server {
...
location = /robots.txt {
root /srv;
try_files $robots =404;
}
...
}
Vereste documentopara detalles.