0

I have the following config under my /etc/nginx/sites-available/example.com. What I need is HTTP traffic to be redirected to HTTPS, and WWW to be redirected to non-www. But for some reason WWW to non-WWW redirection is not working. Cleared browser cache to test as well...

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

server { listen 443 ssl http2; server_name example.com;

root /var/www/html/example.com;
index index.html index.htm index.php index.nginx-debian.html;

location / {
    proxy_pass http://localhost:8000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

location /wordpress/ {
    rewrite ^/wordpress/wp-json/(.*?)$ /wordpress/index.php?rest_route=/$1 last;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

location /wordpress/wp-content/uploads {
    add_header X-Robots_tag "index, follow";
    if ($request_uri ~* ".(eot|woff2|woff|ttf|ico|css|js|gif|jpe?g|png|svg|mp4|avi|flv|wmv|mov|ogv|m4p|m4v|3gp|pdf)$") {
        expires 365d;
        access_log off;
        add_header Pragma public;
        add_header Cache-Control "public";
        break;
    }
    location ~ \.php$ {
        deny all;
    }
}

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
}

location ~ /\.ht {
    deny all;
}

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

geochanto
  • 159
  • 1
  • 9

1 Answers1

1

What you have is a redirect from http://www.example.com to https://example.com.

In addition you will need a redirect from https://www.example.com to example.com, with certificates to match.

vidarlo
  • 11,723