By on3al


2014-01-14 06:01:09 8 Comments

I'm setting up an nginx server with an SSL.

The domain with the ssl is www.mydomain.com

I want to redirect all requests from:

http://mydomain.com, http://www.mydomain.com, & https://mydomain.com to

https://www.mydomain.com

I have the following server blocks setup currently:

server{
  listen 443 ssl;
  root /www/mydomain.com/;

  ssl_certificate /ssl/domain.crt;
  ssl_certificate /ssl/domain.key;
  .
  . 
  .
}

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

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

server{
  listen ssl 443;
  server_name mydomain.com;
  return 301 https://www.mydomain.com$request_uri;
}

This currently does not work, but I don't understand why not. I can get a combination of either http -> https working or no-www to -> www working, but mixing them as above does not work.

3 comments

@RanchMobile 2019-04-04 17:35:37

#If you like to redirect all "http" to "https" then add the following:
server {
        listen 80;

        server_name yourdomain.com;
        server_name www.yourdomain.com;

 if ($scheme = "http")
        {
                rewrite ^(.*)$ https://yourdomain.com$1 permanent;
        }
}

@Mohammad AbuShady 2014-01-14 12:25:39

The ssl redirect won't work if your ssl certificate doesn't support the non-www domain. The config is correct but can be reduced to just 1 redirect server

Also don't forget to reload nginx sudo service nginx reload

server {
  listen 80;
  listen 443 ssl;
  server_name example.com;
  # add ssl settings
  return 301 https://www.example.com$request_uri;
}

@Danijel-James W 2014-01-30 02:31:45

This is exactly what I wanted and am using it the other way around. Thank you!

@montrealmike 2014-08-19 14:17:25

what about http://www..., it doesn't redirect to https...?

@Mohammad AbuShady 2014-08-19 14:32:18

You'll need a separate server with server name www.domain.com and liatens to port 80 to do that aditional redirect if you need it.

@sarat 2014-10-18 11:27:01

This gives me - nginx: [emerg] invalid parameter "443" in /etc/nginx/nginx.conf:96

@Mohammad AbuShady 2014-10-18 11:44:47

could u paste that line '96', also your nginx version

@Berto 2014-10-21 18:27:54

This is also giving me nginx: [emerg] invalid parameter "443" - Something must have changed. Using nginx 1.6.2

@Mohammad AbuShady 2014-10-21 21:35:33

I just tested it on that version, it gave me the same too, you can split it into two listen lines listen 80; and listen 443;

@haemse 2017-10-31 22:22:13

could someone explain why you need a working certificate for the none-www domain, since, this is a plaine redirection that leads to a new request with the www.domain ?

@Mohammad AbuShady 2017-11-01 10:08:19

@haemse because what the browser does ( roughly ) is it creates a connection to the non www domain ( the url you requested ), gets the certificate and validates it, then if things are ok it would ask for the response ( the redirect ), so you get the certificate failure before getting the redirect response.

@haemse 2017-11-01 15:44:40

@MohammadAbuShady: Ok, so in short, without the cert, it does not even build up a connection ...

@Mohammad AbuShady 2017-11-01 19:38:23

@haemse yea, you'll get the invalid certificate error from the browser

@Suneel Kumar 2015-01-21 13:18:07

I am late, But you can do like this

server{
  listen 443 ssl;
  server_name www.mydomain.com;
  root /www/mydomain.com/;

  ssl    on;
  ssl_certificate /ssl/domain.crt;
  ssl_certificate /ssl/domain.key;
  .
  . 
  .
}

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

server{
  listen 443;
  server_name mydomain.com;
  return 301 https://www.$server_name$request_uri;
}

Or you can replace return 301 https://www.$server_name$request_uri; with rewrite ^ http://www.$server_name$request_uri? permanent;, both will work.

You also need to set this in google webmaster for better SEO.

@Feras 2016-02-22 05:19:27

Seems wrong, www.mydomain.com will get replaced to www.www.mydomain.com in the listen 80 block.

@Suneel Kumar 2016-02-22 05:29:57

@Feras I have updated the answer, please check. Please let me know if you have any problem.

@Feras 2016-02-22 05:55:01

yep thats the correct one, i use the same setup

@Suneel Kumar 2016-02-22 06:35:17

@Feras Great, I am glad this helps.

@dan 2016-06-11 17:45:27

This doesn't seem to work just for https//mydomain.com

@Suneel Kumar 2016-06-13 04:47:39

It will redirect https//mydomain.com to https//www.mydomain.com, if did not want to redirect then remove third block.

Related Questions

Sponsored Content

58 Answered Questions

[SOLVED] How do I redirect to another webpage?

13 Answered Questions

[SOLVED] Are HTTPS URLs encrypted?

17 Answered Questions

[SOLVED] Nginx no-www to www and www to no-www

7 Answered Questions

[SOLVED] Enabling HTTPS on express.js

5 Answered Questions

[SOLVED] Nginx: redirect non-www to www on https

6 Answered Questions

[SOLVED] apache redirect http to https and www to non www

0 Answered Questions

Nginx Redirect https://non-www to http://www not working

  • 2017-09-11 07:37:55
  • Radosh
  • 103 View
  • 0 Score
  • 0 Answer
  • Tags:   ssl redirect nginx

1 Answered Questions

[SOLVED] nginx https redirect adds www

0 Answered Questions

Redirecting www to non-www (just domain) over HTTPS in Azure

Sponsored Content