0

The desired functionality of my nginx configuration for my website is as follows.

  1. All main site traffic to example.com should redirect to https://www.example.com. All non-www traffic to the base domain should be re-written to be www.
  2. All existing subdomains need to be kept alive for my enterprise clients and routed to https (e.g. nike.example.com to https://nike.example.com)

In my current configuration below the only case that does not work is when I type https://example.com into my browser, the browser shows me the Your connection is not private page indicating the site is not safe. I understand that the configuration below does not cover this case but I don't know how to solve the problem without killing off all the subdomains.

How can I alter this configuration file to cover all the cases above?

server {
  listen 80 default_server; 
  listen [::]:80 default_server; 
  root /var/www/website;
  index index.html;
  server_name www.example.com;
  server_tokens off;

  if ($http_x_forward_proto = 'http') {
    return 301 https://$host$request_uri; 
  } 

  location / {
    try_files $uri$args $uri$args/ /index.html =404; 
  } 

}

0

you can setup additional virtual host, serving just example.com, ie:

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

also, if you want to have redirection using tls (https), then you will have to setup virtual host and have certificate that covers example.com and www.example.com domains.

  • Thank you for the quick response - I have a wildcard SSL certificate for all *.example.com subdomains that are applied to a load balancer upstream. I will try adding this virtual host block above the existing configuration and test. – Joe Aug 6 at 15:02
  • Afaik *.example.com certificate does not cover example.com – Martynas Saint Aug 6 at 15:04
  • With your advice, I added another server block as seen above. Also, with AWS ACM, I was able to associate example.com's and *example.com's certificates to the same load balancer. Thank you. – Joe Aug 18 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.