How to Configure Nginx to Serve Your Website on https://www.domain.com Instead of Redirecting to https://domain.com?

Solution
To configure Nginx to serve your website on https://www.domain.com and redirect https://domain.com to https://www.domain.com, follow these steps:


  1. DNS Configuration:
    • Ensure both domain.com and www.domain.com point to your server's IP address in your DNS settings.
  2. Nginx Configuration:
    • Open your Nginx configuration file, typically located at /etc/nginx/nginx.conf or within the /etc/nginx/sites-available/ directory.
    • Add the following server blocks:

Code:
# Redirect non-www to www over HTTPS
server {
    listen 80;
    listen [::]:80;
    server_name domain.com;
    return 301 https://www.domain.com$request_uri;
}...
To configure Nginx to serve your website on https://www.domain.com and redirect https://domain.com to https://www.domain.com, follow these steps:


  1. DNS Configuration:
    • Ensure both domain.com and www.domain.com point to your server's IP address in your DNS settings.
  2. Nginx Configuration:
    • Open your Nginx configuration file, typically located at /etc/nginx/nginx.conf or within the /etc/nginx/sites-available/ directory.
    • Add the following server blocks:

Code:
# Redirect non-www to www over HTTPS
server {
    listen 80;
    listen [::]:80;
    server_name domain.com;
    return 301 https://www.domain.com$request_uri;
}

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

    ssl_certificate /path/to/ssl_certificate.crt;
    ssl_certificate_key /path/to/ssl_certificate.key;

    return 301 https://www.domain.com$request_uri;
}

# Serve www.domain.com over HTTPS
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.domain.com;

    ssl_certificate /path/to/ssl_certificate.crt;
    ssl_certificate_key /path/to/ssl_certificate.key;

    root /path/to/your/website;
    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ =404;
    }
}

    • Explanation:
      • The first server block listens on port 80 (HTTP) for domain.com and redirects to https://www.domain.com.
      • The second server block listens on port 443 (HTTPS) for domain.com and redirects to https://www.domain.com.
      • The third server block listens on port 443 (HTTPS) for www.domain.com and serves your website content.
  • SSL Certificates:
    • Ensure your SSL certificate covers both domain.com and www.domain.com. You can obtain a free certificate from Let's Encrypt or purchase one from a Certificate Authority.
  • Apply Changes:
    • Test the Nginx configuration for syntax errors

      Code:
      sudo nginx -t
    • If the test is successful, reload Nginx to apply the changes:

      Code:
      sudo systemctl reload nginx
By implementing these configurations, requests to https://domain.com will be redirected to https://www.domain.com, ensuring your website is served on the desired URL.

Note: Always back up your existing Nginx configuration files before making changes. Additionally, ensure that your DNS records and SSL certificates are correctly set up to avoid any downtime or security issues.
 
Solution
Back
Top