N
N
Nikita Tratorov2017-04-01 16:40:31
Nginx
Nikita Tratorov, 2017-04-01 16:40:31

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

so that everything goes unnoticed by the browser, i.e. without redirects.
The www and new subdomains do not need to be redirected; fit
if ($host ~ ^(?!www\.|new\.)(.*)\.site\.com$) {...}

At the same time, statics should be intercepted by nginx, and php files should be proxyed to apache below, after all locations. Those. the idea is to change the path at the beginning of the config so that other locations process it further.
Full config:
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

1 answer(s)
N
Nurlan, 2017-04-04
@daager

Try this regex^(?!www\.|new\.)(?<subdomain>[a-z\.]+)\.site.com$

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question