title: How to redirect full path from www to non-www in hginx withot checking ssl sertificate
body: <p>Could you please help me to find out what`s going on wrong? I have the following nginx configuration:</p>
  <pre><code>server {
    listen 80;
    listen [::]:80;
    server_name domain.com www.example.com;
  
    location /.well-known/acme-challenge/ {
      root /var/www/certbot/;
    }
    location / {
      if ($host ~ &quot;^www\.(.*)$&quot;) {
        return 301 $https://$1$request_uri;
      }
      return 301 https://$host$request_uri;
      }    
    }
  
  server {
    listen 443 default_server ssl http2;
    listen [::]:443 ssl http2;
  
    server_name example.com www.example.com;
  
    if ($host ~ &quot;^www\.(.*)$&quot;) {
      return 301 https://$1$request_uri;
    }
  
    ssl_certificate /etc/nginx/ssl/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/live/example.com/privkey.pem;
  
    location / {
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
  
      proxy_pass http://nodeserver:3333/;
  
      rewrite ^(/static/media/)(.+)$ https://some$1$2 last;
    }
  }
  </code></pre>
  <p>I expect the following behavior:</p>
  <pre><code>a) http://example.com =&gt; https://example.com
  b) http://example.com?abc =&gt; https://example.com?abc
  c) http://example.com/test =&gt; https://example.com/test
  d) http://www.example.com =&gt; https://example.com
  e) https://www.example.com?abc =&gt; https://example.com?abc
  f) https://www.example.com/test =&gt; https://example.com/test
  </code></pre>
  <p>But the last one, f) doesn't work. It doesn't redirect and stays on <code>https://www.example.com/test</code> with <code>Cannot establish a secure connection</code> error.</p>
  <p>What could be wrong with my setup?</p>