Nginx not redirecting but is working

Solution 1:

You can use rewrite in your http server block

server {
    listen 80;
    server_name test.domain.com;
    rewrite ^ https://$server_name$request_uri? permanent;
}

Solution 2:

Missing ";" at the end of line. (or just a broken copy/paste?)

    server_name test.domain.com;

I think you have another server_block operating at default server that overrides this one without valid server name

Tips:

  1. If you want to catch all http requests with one global server block, use default_server:

    listen 80 default_server;
    
  2. Your can use more variables ;)

    return 301 https://test.domain.com$request_uri;
    

    to

    return 301 https://$host$request_uri;
    

    Do not use rewrite, return is fastest.