V
V
Vladimir2017-09-08 14:28:24
Nginx
Vladimir, 2017-09-08 14:28:24

How to leave default IP address free when configuring virtual host redirect in NGINX?

The bottom line is this:
There is a server with virtual hosts and one IP (xxx.xxx.xxx.xxx)
There is the following virtual host setting:

server {

listen 80;
server_name www.domain.com domain.com;
return 301 https://domain.com$request_uri;

}

server {

    listen       443 ssl; 
    server_name  domain.com;

....
  
 }

Before setting up https, when entering xxx.xxx.xxx.xxx in the address bar of the browser, it redirected to the standard page "Welcome to Nginx"
Now it redirects to
domain.com host? those. in the address bar see xxx.xxx.xxx.xxx and not domain.com and see "Welcome to Nginx"?
UPD
Reset the browser cache - everything is as it should be. I checked in other browsers, everything is fine too.

The initial settings were correct
in the default config
server {
   listen 80 default_server;
   server_name xxx.xxx.xxx.xxx;
}

in domain.com config
server {

listen 80;
server_name www.domain.com domain.com;
return 301 https://domain.com$request_uri;

}

server {

    listen       443 ssl; 
    server_name  domain.com;

....
  
 }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
ky0, 2017-09-08
@ipdesign

The default_server directive of the listen parameter.

V
Vladimir, 2017-09-08
@ipdesign

What strange things are happening)
in the /etc/nginx/sites-available/default file I have:

server {
   listen 80 default_server;
   server_name xxx.xxx.xxx.xxx;
}

change to:
listen xxx.xxx.xxx.xxx:80 default_server;
   server_name "_";

and on :
listen 80 default_server;
   server_name "_";

Anyway, when I enter xxx.xxx.xxx.xxx in the browser address, I get:
https://domain.com
the domain.com config is the same:
server {

listen 80;
server_name www.domain.com domain.com;
return 301 https://domain.com$request_uri;

}

server {

    listen       443 ssl; 
    server_name  domain.com;

....
  
 }

and if you register the IP address in the domain.com config
server {

listen xxx.xxx.xxx.xxx:80;
server_name www.domain.com domain.com;
return 301 https://domain.com$request_uri;

}

then generally some unknown garbage:
www.domain.com - gets to Welcom to Nginx
domain.com and xxx.xxx.xxx.xxx redirects to https://domain.com

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question