R
R
RJs452014-11-21 14:29:33
Nginx
RJs45, 2014-11-21 14:29:33

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.
How to make domains "look" in a directory:
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

3 answer(s)
B
Boris Benkovsky, 2014-11-21
@benbor

nginx.org/ru/docs/http/server_names.html
in each server section specify the location you need

S
Sergey Petrikov, 2014-11-21
@RicoX

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;

A
Anatoly, 2014-11-21
@taliban

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 question

Ask a Question

731 491 924 answers to any question