'nginx request_uri not working redirects to root

I am using the nginxinc/nginx-unprivileged:1.21-alpine Docker Image.

My config file looks like this:

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

server {
    listen ${LISTEN_PORT_HTTPS};
    server_name www.example.de;
    ssl_certificate *****;
    ssl_certificate_key *****;
    return 301 https://example.de$request_uri;
}

server {
    server_name example.de;
    listen ${LISTEN_PORT_HTTPS} ssl http2;
    ssl_certificate *****;
    ssl_certificate_key *****;

    gzip on;
    gzip_vary on;
    gzip_min_length 1024;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/css text/xml text/javascript application/javascript application/x-javascript application/xml;
    gzip_disable "MSIE [1-6]\.";

    location /static {
        alias /vol/static;
        add_header Cache-Control "public,max-age=31536000,immutable";
    }

    location / {
        uwsgi_pass              ${APP_HOST}:${APP_PORT};
        include                 /etc/nginx/uwsgi_params;
        client_max_body_size    10M;
    }
}

The problem I have is that http and www redirect to https non-www but no matter what url it always redirects to the home page. it looks like $request_uri is empty

EDIT: Got it working with: rewrite (.*)$ https://example.de$1 permanent; but as far as im concerned it's not best practice



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source