D
D
dimonka332016-06-10 12:28:32
Nginx
dimonka33, 2016-06-10 12:28:32

How to make a site accessible only via https and without www?

There is a site https://example.com I want everything to be only through https and without www, in other cases there should be a 301 redict to the corresponding https without www ulr, that is:

example.com -> 301 -> https://example.com
https://www.example.com -> 301 -> https://example.com
https://www.example.com/something -> 301 -> https://example.com/something
www.example.com/something -> 301 -> https://example.com/something

etc.
In nginx, there are settings that work partially:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name localhost example.com www.example.com;
#return 301 https://example.com$request_uri; -- why there was an endless redirect
# .......

The site itself is running on a server at localhost:3000

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Romanenko, 2016-06-10
@dimonka33

because you are redirecting to yourself.
You need to do something like this:

server {
listen 80 ;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}

server {
listen 443 ssl ;
listen [::]:443 ssl ;
server_name localhost www.example.com;
return 301 https://example.com$request_uri;
}

server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name  example.com


... тут будет основной конфиг для вашего сервера
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question