About Conditional Authentication with Nginx

For the staging website you can conditionally require authentication based on the user agent. Here is an example of nginx settings:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
map $http_user_agent $authentication {
    default "Access Restricted";
    "~^Facebot" "off";
    "~^Twitterbot" "off";
    "~^Google \(\+https://developers.google.com/\+/web/snippet/\)" "off";
    "~^Python-urllib" "off";
    "~.+?googleimageproxy" "off";
    "~^WeasyPrint" "off";
}

server {
    # ...
    location / {
        # ...
        auth_basic $authentication;
        auth_basic_user_file /etc/nginx/.htpasswd;
    }
}

This allows the website to be accessed by Facebook, Twitter, and Google logins, also by Python requests, and WeasyPrint library, but requires authentication for all other requests.

Tips and Tricks Programming Dev Ops Architecture nginx WeasyPrint