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 and using include directive to include them in all the server and location blocks where additional headers are added.