M
M
MadDeee2017-10-26 13:47:39
Nginx
MadDeee, 2017-10-26 13:47:39

How to redirect to domain names?

There is an external IP and several internal IPs. Sites weigh on internal IPs and there are 2 domain names site1.ru and site2.ru and each domain is configured to an external IP (1.1.1.1) How to configure nginx so that it can recognize domains and redirect to the appropriate IP?

For example:
I go to the site1.ru domain, I get to a site whose internal IP is 2.2.2.1
I go to the site2.ru domain, I get to a site whose internal IP is 2.2.2.2

At the moment, the situation is this. When you go to the site1.ru domain, the site opens, if you go to the external IP 1.1.1.1, then the Welcom nginx page opens and I know for sure that it is from site1.ru (2.2.2.1).


1.1.1.1 - external
2.2.2.1 - internal (Site1.ru)
2.2.2.2 - internal (site2.ru)
All sites are virtual

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Karabanov, 2017-10-26
@karabanov

default_server option. More details https://nginx.ru/ru/docs/http/request_processing.html

A
Andrey Tov, 2017-10-30
@Don_Andretti

Create 2 virtual hosts in Nginx:
First:

server {
   listen 80 default_server;
   listen [::]:80 default_server ipv6only=on;

   root /var/www/Site1.ru/html;
   index index.html index.htm;

   server_name Site1.ru www.Site1.ru;

   location / {
       try_files $uri $uri/ =404;
   }
}

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

   root /var/www/site2.ru/html;
   index index.html index.htm;

   server_name site2.ru www.site2.ru;

   location / {
       try_files $uri $uri/ =404;
   }
}

Activate virtual hosts:
sudo ln -s /etc/nginx/sites-available/site1.ru/etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/site2.ru/etc/nginx/sites-enabled/

Disable the default host:
There is one more setting that needs to be done in the Nginx configuration file. Open it:
You need to uncomment one of the lines:
Now you can restart the web server for the changes to take effect:
sudo service nginx restart

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question