I
I
ironheaddd2022-02-15 10:49:38
Nginx
ironheaddd, 2022-02-15 10:49:38

How to set dynamic root in nginx?

There is an nginx config that generally works,

server {
  listen 		80;

  server_name 	~^(?<subdomain>.+)\.domain\.com;
  add_header	Content-Type text/plain;
  index		index.php;

  root		/var/www/sites/$subdomain;

  access_log	/var/log/nginx/access.log;
  error_log	/var/log/nginx/error.log;

  location / {
    try_files $uri $uri/ /index.php$is_args$args;
  }

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

  location = /favicon.ico { log_not_found off; access_log off; }
  location = /robots.txt { log_not_found off; access_log off; allow all; }
  location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
    expires max;
    log_not_found off;
  }
}


but in order for everything to work in general, along the path /var/www/sites/$subdomain I need to rename about 80 folders from the $subdomain.domain.com format to just $subdomain (inherited), too lazy to write scripts I know how, in connection with which the question arises: how to correctly register root /var/www/sites/$subdomain; ? If I write it as root /var/www/sites/$subdomain.domain.com; , then subdomains are not opened.
PS: if you can tell me how to dynamically create log files separately for each subdomain, I will be glad and grateful.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rst0, 2022-02-20
@Rst0

server_name *.domain.com;
root /var/www/sites/$http_host;

sub.domain.com
will look for
the /var/www/sites/sub.domain.com folder

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question