Redirect only Desktop traffic to HTTPS with Nginx

This is a very peculiar problem I was facing when I decided to start redirecting my website Naadan Chords traffic to HTTPS. Here is the catch: I couldn't redirect 301 all traffic to HTTPS because that would mean I would break some older versions of the Android app or the iOS app (where I cannot push updates anymore). Since all API URLs refer to the HTTP path.

So, I can only force redirect Desktop traffic to HTTPS, while continuing to serve all HTTP requests from mobile.

User Agent check in Nginx

Thanks to this Github gist, I got the following snippet:

### Testing if the client is a mobile or a desktop.
### The selection is based on the usual UA strings for desktop browsers.

## Testing a user agent using a method that reverts the logic of the
## UA detection. Inspired by notnotmobile.appspot.com.
map $http_user_agent $is_desktop {  
    default 0;
    ~*linux.*android|windows\s+(?:ce|phone) 0; # exceptions to the rule
    ~*spider|crawl|slurp|bot 1; # bots
    ~*windows|linux|os\s+x\s*[\d\._]+|solaris|bsd 1; # OSes
}

Pop this at the top of your domain Nginx configuration file inside (usually) /etc/nginx/sites-available)

The actual redirection

Here is how I ended up doing the redirection:
Please note I have removed some parts of the code with comments (so don't copy paste without reading!)

server {  
    server_name naadanchords.com www.naadanchords.com;

    location / {
        #Reverse proxy logic to Apache
    }

    listen 443 ssl;
    # HTTPS certbot configuration goes here
}

server {  
    if ($is_desktop) {
        set $redirection A;
    }

    if ($host = www.naadanchords.com) {
        set $redirection "${redirection}B";
    }


    if ($host = naadanchords.com) {
        set $redirection "${redirection}B";
    }


    if ($redirection = AB) {
        return 301 https://$host$request_uri;
    }

    server_name naadanchords.com www.naadanchords.com;

    listen 80;
    location / {
        #Reverse proxy logic to Apache
    }
}

It took me a long time of head scratching to get this done. So here it is, if it could help someone.