Score:0

Nginx redirect https to https

us flag

I'm trying to redirect a https domain like https://example.com to https://example.com/something with nginx. it works properly when I use http://example.com in my browser but not working with https://example.com. my configuration is like below.

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

Thanks!

in flag
You haven't configured any SSL certificates for your SSL server block.
Richard Smith avatar
jp flag
Where is `https://example.com/something` handled? At the moment you are creating a redirection loop.
Score:0
in flag
  1. You are missing TLS certificates so even if you configure the redirect the browser will only respond with certificate error. You must configure the ssl_certificate and ssl_certificate_key directives

And you must specify ssl and http2 on the listening directive for HTTP/2

For HTTP/3 http3 implies mandatory ssl, so no ssl directive should be specified in that case.

  1. Your configuration has an infinite redirection loop which makes the server unusable. Especially you are infinitely redirecting to HTTPS.

  2. Your server is lacking IPv6 support.

You should do something like this:

server {
    listen 0.0.0.0:80 default_server;
    listen [::]:80 default_server;

    location / {
      return 308 https://$host$request_uri/something;
    }
}
server {
    listen              0.0.0.0:443 http3 reuseport;
    listen              0.0.0.0:443 http2 ssl;
    listen              [::]:443 http3 reuseport;
    listen              [::]:443 http2 ssl;
    server_name         $YOURDOMAIN;
    ssl_certificate    $CERT_PATH;
    ssl_certificate_key $CERT_KEY;
[...]
}

See also the Mozilla TLS configuration tool to help you: https://ssl-config.mozilla.org/

And adjust it to your needs.

Note: The above config lines regarding to http3 only is useful if you compile NGINX with HTTP/3 support, which is only available on nginx-quic branch.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.