NGiNX's recipes & tips
Contents
- Introduction
- Userdir functionality like Apache’s mod_userdir
- Note on how NGiNX works with HTTP headers
- Tell a Tor visitor of your clearnet site that you have an onion
- Variables
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;
5. Variables
Just wanna tell about this cool feature you can use to reduce code repetition. Alas, you can use them only in server
, location
and if
blocks. If only they could be declared in a http
block, they’d be perfect. But still, they help a lot.
I heavily use aliases pointing to directories within the same base directory.
Judging from a little testing, variables doesn’t decrease performance. Or I just don’t notice it.
Variables could only store strings. To demonstrate the usage I’ll just post here a part of my config:
set $root /srv/http/dwelling;
...
location =/favicon.ico {
alias $root/shared/static/img/favicon.ico;
}
...
location /assets/ {
alias $root/homepage/static/assets/;
}
The very reason why I had to use variables is that alias
doesn’t work with relative paths. I wish alias
could be relative to root
, but we are forced to look for workarounds.
Update: Oh, I realised that you can declare variables in http
block using map
directive with default
set to value you need. Funny, I’ve been using map
to prevent LAN IPs from being logged all this time, heh. So, here’s example:
map 1 $root {
default /srv/http/dwelling;
}