NGiNX's recipes & tips

1. Introduction

Here I'll place recipes for implementing different functionalities and notes on webserver's behaviour.

2. Userdir functionality like Apache's mod_userdir

Once I wanted to have a “tilde user directories” like /~user/ which is more known as Apache's mod_userdir feature. I work with NGiNX so regular expressions is the way to do that.

Nothing special in my case. I only need to keep files there, so others may get them. Thing is simple, but I took some time to realise what regexp to use here back then.

The implementation is quite simple:

location ~ ^/~(.+?)(/.*)?$ {
    alias /home/$1/pub$2;
    autoindex on;
}

Of course, you can choose whatever place for user's public directories. In my case you need to give a read and execute permissions to user's home directory and to public directories inside them to others (chmod o=rX /home/user), which may be a security concern.

autoindex on will make an index of files that lies by URL. And, of course, you can put there an index.html file.

3. Note on how NGiNX works with HTTP headers

Let's say in http block you specified common headers like X-Frame-Options, X-XSS-Protection, and so on for all server directives to use. But, if you add some other header for a specific server or location block then all those headers would be dropped.

For now the only cure for it is to place all that headers in a separate file like common-headers.inc and using include directive to include them in all the server and location blocks where additional headers are added.

4. Tell a Tor visitor of your clearnet site that you have an onion

There is a custom HTTP header Onion-Location that is being recognised by Tor Browser and it will show a ".onion available" button in an address bar.

The code for NGiNX is this simple:

add_header Onion-Location http://yoursite.onion$request_uri;