2

My configuration:

server {
  listen 80;
  server_name mydomain.com;
  rewrite ^(.*)$ $scheme://www.mydomain.com$1;
}

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

  if ($host !~* ^(www.mydomain.com)$ ) {
    return 444;
  }
}

server {
  listen 443;
  server_name www.mydomain.com;
  ssl on;
  ssl_certificate     /path/to/www.mydomain.com_chain.pem;
  ssl_certificate_key /path/to/www.mydomain.com.key;

  ...
}

What does work:

  • redirection from mydomain.com to www.mydomain.com
  • redirection from www.mydomain.com to https://www.mydomain.com

What does NOT work correctly:

  • when I enter https://mydomain.com directly into the browser the redirect to www.mydomain.com simply does not occur, but the request is passed onto the application server as https://mydomain.com
    • it appears as if the port 80 "entrance" is being skipped and the server is being accessed directly at port 443, but I'm not an Nginx expert so I'm unable to tell

What is the solution for the https://mydomain.com problem above?

3 Answers 3

2

Use an if statement in your SSL server block to check the host:

server {
  listen 443;
  server_name www.mydomain.com mydomain.com;
  ssl on;
  ssl_certificate     /path/to/www.mydomain.com_chain.pem;
  ssl_certificate_key /path/to/www.mydomain.com.key;



  if ($host = 'mydomain.com' ) {
     rewrite  ^/(.*)$  https://www.mydomain.com/$1  permanent;
  }

  ...
}
2
  • Thanks for your answer, but I'm reluctant to use if in this context; read here. Jan 22, 2014 at 16:20
  • "Directive if has problems when used in location context". This is not a location context Jan 22, 2014 at 16:30
0

As I suspected the reason was because port 80 was being skipped and port 443 accessed directly. Adding the following server block enabled the redirect:

server {
  listen 443;
  server_name mydomain.com;
  ssl on;
  ssl_certificate     /path/to/www.mydomain.com_chain.pem;
  ssl_certificate_key /path/to/www.mydomain.com.key;
  rewrite ^(.*)$ $scheme://www.mydomain.com$1;
}

If anyone knows of a better or more elegant solution please add your answer.

1
  • You probably want to add permanent at the end of your rewrite line to do a 301 redirect.
    – Mxx
    Jan 22, 2014 at 17:03
0

I prefer to avoid if because if is evil, also I prefer using return over rewrite because it doesn't need to run a regex engine.

server {
  #default server that catches all undefined host names
  listen 80 default_server;
  return 444;
}
server {
  #redirecting server for non-www ssl and non ssl domains
  listen 80;
  liste 443 ssl;
  server_name mydomain.com;
  ssl_certificate     /path/to/www.mydomain.com_chain.pem;
  ssl_certificate_key /path/to/www.mydomain.com.key;
  return 301 https://www.mydomain.com$request_uri$is_args$query_string;
}
server {
  # redirecting server for www non ssl server
  listen 80; 
  server_name www.mydomain.com;
  return 301 https://mydomain.com$request_uri$is_args$query_string;
}
server {
  # main server config
  listen 443 ssl;
  server_name www.mydomain.com;
  ssl on;
  ssl_certificate     /path/to/www.mydomain.com_chain.pem;
  ssl_certificate_key /path/to/www.mydomain.com.key;
  # remaining config...
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .