Answer the question
In order to leave comments, you need to log in
How to make a site available on different domains in Nginx?
There is nginx configured for several domain names. I will give domain names and their root in the nginx config
site.ru - root /var/www/site;
admin.site.ru - root /var/www/admin;
panel.site.ru - root /var/www/panel;
login.site.ru - root /var/www/login;
other domain names will also be sent to this server (we do not know in advance which ones). Let foo.ru be an example. foo.ru -> /var/www/site
admin.foo.ru -> /var/www/panel
login.foo.ru -> /var/www/login
and at the same time, there was no explicit redirect (so that the url in the browser line does not change). That is, panel.site.ru and admin.foo.ru were identical.
Answer the question
In order to leave comments, you need to log in
nginx.org/ru/docs/http/server_names.html
in each server section specify the location you need
If you don't want to do it for everyone, here's an example using variables
server {
server_name ~^(?:www\.)?(?P<host_wo_www>.+)$;
server_name_in_redirect off;
resolver 127.0.0.1;
root /var/www/$host_wo_www;
server_name "~(?.*?)\.(?[-\w]+\.domain\.com)$";
then the $subdomain variable will be available, you can use it when compiling the path, for example:
set $root_path "/var/www/runashop2/$subdomain/pp2/public";
root $root_path;
This is a torn piece from our config and redone (so as not to shine too much), so it may not work right away, but the essence is shown, use regular expressions with named parameters, and then use what matched in the path
nginx.org/en/docs/http/ server_names.html#regex_names everything is described in detail here
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question