Skip to main content

NGINX Redirect based on User Agent

Tsim Sha Tsui, Hongkong

Continuation from NGINX Docker Ingress

Github Repository

NGINX

I need to offer a URL that redirects user based on the user agent their browser reports. This can be done using a simple IF Statement:

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

    server_name  192.168.2.111;

    # force all traffic to use https
    return 301 https://$server_name$request_uri;
}


server {

    listen 443 ssl;
    listen [::]:443 ssl;

    include /etc/nginx/conf.d/self-signed.conf;
    include /etc/nginx/conf.d/ssl-params.conf;

    server_name 192.168.2.111;

    if ( $http_user_agent ~* 'android|blackberry' ) {
       return 301 https://192.168.2.117$request_uri;
     }

    if ( $http_user_agent ~* 'ipad|iphone|ipod' ) {
       return 301 https://192.168.2.77$request_uri;
     }

    if ( $http_user_agent ~* 'windows' ) {
       return 301 https://192.168.2.249$request_uri;
     }

}

Now start the ingress with this new configuration file:

docker run -d -p 443:443 -p 80:80 -v /opt/redirection:/etc/nginx/conf.d --name ingress nginx:1.21.1-alpine

Accessing the ingress on IP 192.168.2.111 will now upgrade you to HTTPS and then redirect you to another local IP address based on your browser reported user agent:

  • Android Phones -> https://192.168.2.117
  • iPhones Phones -> https://192.168.2.77
  • Windows Phones -> https://192.168.2.249

Apache2

Getting Started

Just to see how it works on the other side - here is how to do the same thing in Apache:

mkdir -p /var/www/html/my.page.com/
chown -R $USER:$USER /var/www/html/my.page.com/
sudo chmod -R 755 /var/www/html/my.page.com
nano /var/www/html/my.page.com/index.html

And add some page content here:

<html>
    <head>
        <title>My App</title>
    </head>
    <body>
        <h1>Hi there!</h1>
    </body>
</html>

Now, create a virtual host file for the domain using the command shown below.

nano /etc/apache2/sites-available/my.page.com.conf

Now copy and paste the content below and replace the domain my.page.com with your own domain:

<VirtualHost *:80>
    ServerAdmin admin@my.page.com
    ServerName my.page.com
    ServerAlias www.my.page.com
    DocumentRoot /var/www/html/my.page.com/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

At this point, first test then enable the virtual host file as shown:

apachectl configtest
a2ensite my.page.com.conf
systemctl restart apache2

Adding the Redirects

Now add the following lines to your virtual host config:

<VirtualHost *:80>
    ServerAdmin admin@my.page.com
    ServerName my.page.com
    ServerAlias www.my.page.com
    RewriteEngine On
    RewriteCond %{HTTP_HOST} my.page.com$ [NC]
    RewriteCond %{HTTP_USER_AGENT} iPhone [OR]
    RewriteCond %{HTTP_USER_AGENT} iPod
    RewriteRule ^(.*)$ https://apps.apple.com/us/app/instarvision/id413109553 [L,R=301]
    RewriteCond %{HTTP_USER_AGENT} iPad [OR]
    RewriteCond %{HTTP_USER_AGENT} Mac\ OS\ X
    RewriteRule ^(.*)$ https://apps.apple.com/us/app/instarvision-for-ipad/id767539403 [L,R=301]
    RewriteCond %{HTTP_USER_AGENT} Android
    RewriteRule ^(.*)$ https://play.google.com/store/apps/details?id=de.instar.vision [L,R=301]
    RewriteCond %{HTTP_USER_AGENT} Windows
    RewriteRule ^(.*)$ https://www.microsoft.com/de-de/store/p/instarvision/9nblggh10wtp [L,R=301]
    RewriteCond %{HTTP_USER_AGENT} ^(.*)
    RewriteRule ^(.*)$ https://www.instar.com/support/downloads [L,R=301]
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

And test and reload Apache:

apachectl configtest
systemctl reload apache2

Verify that the redirects are working and run Certbot to create the SSL certificate and add the Apache configuration for you

certbot -d my.page.com