G
G
German Zvonchuk2014-05-28 10:53:50
Nginx
German Zvonchuk, 2014-05-28 10:53:50

How to make "http to https" and "www to non-www" on NGINX for a pair of domains at once?

Good day to experts!
I would be very grateful if you tell me the correct sequence of actions:
Due to the fact that the site is multilingual, there are a couple of domains.
I need to do the following:
1) so that both domains always work only via HTTPS
2) always be without WWW
I understand correctly, I first need to create a server on port 80, in server_name register all possible domains with www and without www
1) After why implement a redirect to HTTPS
2) HTTPS to register a check for the presence of www and, if so, throw it on https without www.
Am I thinking right?
With this config I currently have "This webpage has a redirect loop" going on.

## our http server at port 80
server {
      listen      80 default;
      server_name domain.ru www.domain.ru domain.com www.domain.com;
      ## redirect http to https ##
      rewrite        ^ https://$server_name$request_uri? permanent;
}

## Our https server at port 443. You need to provide ssl config here###
server {
listen    443 ssl;
server_name domain.ru www.domain.ru domain.com www.domain.com;

server_name "~^www\.(.*)$";
return 301 https://$host$request_uri;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
kompi, 2014-05-28
@inside22

And if you try like this:

server {
    listen              80;
    server_name "~^(www\.)?(.*)$" ;
    return 301 https://$2$request_uri ;
}
server {
    listen              443 ssl;
    server_name "~^www\.(.*)$" ;
    return 301 https://$1$request_uri ;
}
server {
    listen              443 ssl;
    server_name domain.ru domain.com;
    # основная секция
}

For special perverts:
server {
    listen              80;
    listen              443 ssl;
    server_name server_name "~^(www\.)?(.*)$";
  if($scheme = https) {
    if($1 = www) {
      return 301 http://$2$request_uri;
      }
    }
  if($scheme = http) {
    return 301 http://$2$request_uri;
  }
    # основная секция
}

In a good way, regular seasons should not be. If these are two different sites, then it is better to separate the configs, if this is a mirror and the main one, then write the main one in return and remove the regular expressions.
server {
    listen              80;
    server_name www.domain.com domain.com;
    return 301 https://domain.com$request_uri ;
}
server {
    listen              80;
    server_name www.domain.ru domain.ru;
    return 301 https://domain.ru$request_uri ;
}
server {
    listen              443 ssl;
    server_name www.domain.ru;
    return 301 https://domain.ru$request_uri ;
}
server {
    listen              443 ssl;
    server_name www.domain.com;
    return 301 https://domain.com$request_uri ;
}
server {
    listen              443 ssl;
    server_name domain.ru domain.com;
    # основная секция
}

I
Igor, 2014-05-28
@merryjane

It is not necessary to make the domain go through two rewrites. The domain typed in the address bar should be redirected to the target domain in one rewrite.
For example, all http with www can be wrapped in https without www like this:
server {
listen 80;
server_name "~^www\.(.*)$" ;
return 301 https://$1$request_uri ;
}

G
German Zvonchuk, 2014-05-28
@inside22

@kompi
domain.com - this is the English version of
www.domain.com - this is the ALIAS of the English version
domain.ru - this is the Russian version of
www.domain.ru - this is the ALIAS of the Russian version of
@merryjane .
if the client went to www.domain.com or domain.com,
then he will be redirected to https://domain.com by any means, if he suddenly types https://www.domain.com
in the browser with his inquisitive hands, then the second one will work correctly and the client will fly 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