Answer the question
In order to leave comments, you need to log in
How to configure Nginx to redirect subdomains to the appropriate folders without redirecting?
Dear nginx experts!
On the Internet, there is only an example with one or two subdomains, but my situation is multi-domain and my level does not allow me to quickly solve this seemingly simple task.
You need to organize a redirect scheme like this:
<subdomain>.site.com/$request_uri -> $scheme://site.com/partners/<subdomain>/$request_uri
if ($host ~ ^(?!www\.|new\.)(.*)\.site\.com$) {...}
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name site.com ~^(?<subdomain>.+)\.site\.com$;
ssl_certificate /etc/letsencrypt/live/new.site.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/new.site.com/privkey.pem;
include snippets/ssl-params.conf;
charset utf-8;
location ~ /.well-known {
root /usr/share/nginx/html;
allow all;
break;
}
# неудавшееся перенаправление;
#if ($host ~ ^(?!www\.|new\.)(.*)\.site\.com$) {
# rewrite ^(.*)$ $scheme://site.com/partners/$subdomain/$request_uri? break;
#}
# общая статика: стили, картинки, скрипты
location /assets {
alias /var/www/html/assets;
access_log off;
expires 30d;
break;
}
# картинки и файлы статей
location /media/upload/articles {
alias /var/www/html/media/upload/articles;
access_log off;
expires 30d;
break;
}
# баннеры
location /media/upload/bn {
alias /var/www/html/media/upload/bn;
access_log off;
expires 30d;
break;
}
# статика из папок поддоменов кроме php
location ~* ^\/partners\/.*[^\.php]$ {
alias /var/www/html/partners;
access_log off;
expires 30d;
break;
}
location / {
charset utf-8;
proxy_pass http://127.0.0.1:8080;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_read_timeout 90s;
limit_req zone=proxy_global burst=100;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question