How to redirect Nginx non-www to www domain over SSL

ht‮ww//:spt‬w.lautturi.com
How to redirect Nginx non-www to www domain over SSL

To redirect non-www domain requests to the www version of a domain over SSL on an Nginx web server, you can use the server_name and return directives in the server block configuration.

Here is an example configuration that redirects all HTTP requests for example.com to https://www.example.com:

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

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

    # SSL configuration and other server block settings go here
}

In this configuration, the first server block listens for HTTP requests on port 80 and redirects them to the www version of the domain using a 301 permanent redirect. The second server block listens for HTTPS requests on port 443 and handles the requests for the www version of the domain.

Make sure to replace example.com with your own domain name, and configure the SSL settings as needed for your server. Consult the Nginx documentation and online resources for more information on configuring SSL for Nginx.

Created Time:2017-10-28 21:39:06  Author:lautturi