A
A
alexh78542019-05-16 14:44:21
Nginx
alexh7854, 2019-05-16 14:44:21

How to set up dynamic subdomains on nginx with a single front controller?

Hello.
There is a local project.
Installed nginx web server.
How to configure nginx so that when accessing a subdomain, for example sub.domain.com , the site works, but the request is sent to the front controller - domain.com .
All I need is to transfer the subdomain to the router / application and, based on the subdomain, get the necessary data from the database.

server {
  listen 80;
  listen [::]:80;

  root /home/alex/mf/dcms/public;

  index index.php index.htm index.nginx-debian.html;

  server_name dcms.lo www.dcms.lo;

  location / {
    include  /etc/nginx/mime.types;
    try_files $uri $uri/ /index.php?$query_string;
  }

  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;
  }

  location /assets {
    alias /home/alex/mf/dcms/assets/;
    try_files $uri $uri =404;
  }

  location ~ /\.ht {
    deny all;
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2019-05-16
@petermzg

Idea example

server {
    server_name ~^(?<name>\w+)\.domain\.com$;

    location /admin {
        return 301 $scheme://$name.domain.com/;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question