How do I adapt my NGINX config to use subdomains instead of subdirectories?

submitted by rand_alpha19

I have a NGINX configuration that works perfectly, but it uses subdirectories (e.g., example.site/service) because I originally had a free DDNS hostname instead of a real domain. Now that I have a domain, I would prefer to use subdomains instead (e.g., service.example.site).

I tried to change things on my own by editing each location into a separate server block but that didn't work, so I have to be missing something. Below is my current config - I'd really appreciate any help.

server {    
    set $server     "X.X.X.2";
    set $domain     "example.site";
    server_name     example.site;

    # kavita book server
    location /kavita {
        # host and X headers
        proxy_set_header        Host $host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for; aio threads;
        proxy_set_header        X-Forwarded-Proto $scheme;

        # headers to proxy websocket connections
        proxy_http_version      1.1;
        proxy_set_header        Upgrade $http_upgrade;
        proxy_set_header        Connection "Upgrade"; 

        # proxy to Kavita running locally on port using ssl
        proxy_pass              http://$server:5000;
    }

    # dashboard
    location / {
        proxy_pass              http://$server:6000;

        auth_basic              "Log in to the dashboard";
        auth_basic_user_file    /etc/apache2/.htpasswd;
    }

    # weekly menu
    location /menu {
        root                    /etc/nginx/html;
        index                   weekly-menu.html;
    }

    listen      443 ssl;

    ssl_certificate             /etc/nginx/certs/example.site.pem;
    ssl_certificate_key         /etc/nginx/certs/example.site.key;
}

server {    
    if ($host = $domain) {
        return  302 https://$host$request_uri;
    }

    listen 80;
}

Log in to comment

3 Comments

rand_alpha19 [OP]

Right, I did that and it didn't work. Obviously there's something else I need to do or I wouldn't have made this post.

mozz

Ask GPT to rewrite your configuration, check over it with diff to make sure it didn’t do something dumb, bingo bango