W
W
Win32Sector2017-04-03 14:13:50
Nginx
Win32Sector, 2017-04-03 14:13:50

How to redirect from site.ru to www.site.ru and from https://site.ru to https://www.site.ru in nginx?

Comrades, tell me, please, on a subject. There is an nginx config:

server {
  listen 80;
  server_name site.ru;
  location / {
        return       301 http://www.site.ru$request_uri;
  }
}


server {
    listen 80;
    listen 443 ssl;
    server_name www.site.ru;

    ssl_certificate     cert.crt;
    ssl_certificate_key key.key;

How to add a redirect for https://site.ru -> https://www.site.ru ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Fixid, 2017-04-03
@Win32Sector

stackoverflow.com/questions/7947030/nginx-no-www-t...

W
Win32Sector, 2017-04-03
@Win32Sector

Solution to my question:

server {
    listen               80;
    listen               443 ssl;
    server_name          example.com;
    ssl_certificate      path/to/cert;
    ssl_certificate_key  path/to/key;

    return 301 $scheme://www.example.com$request_uri;
}

server {
    listen               80;
    listen               443 ssl;
    server_name          www.example.com;
    ssl_certificate      path/to/cert;
    ssl_certificate_key  path/to/key;

    # rest goes here...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question