Select Page

nginx dynamic settings – part 2

In my previous post re. nginx dynamic settings, I’ve put an example of using variables in the index directive for serving a dynamic main file. This time I want to talk about try_files directive.

In the official examples, linked above, there is a one showing how to provide default place holder image, which is nice, and useful for hard set configurations. Most of the other examples are around internal rewrites to language interpreters.

Now say you host a Drupal multi-site, or WordPress multi-site and want to provide different favicon.ico files or robots.txt per domain, this can come handy. Here is an example:

location /favicon.ico {
    try_files $http_host.favicon.ico favicon.ico =404;
    log_not_found off;
    access_log off;

This way you can provide a default file for all, and specify a unique one for some.

Notice that for favicon.ico this doesn’t really cover it, since themes provide “shortcut icon” tags that override the default favicon. But for robots.txt this is very useful.

How to set dynamic nginx settings using variables

Looking through solutions on the internet, I found that for nginx there are plenty solution for dynamic root directories, headers and environment variables out there.

Today I was asked about using the same application directory with various cached index files, in this case, the determination is based on the domain accessed.

The previous solution used was to create spearate root directories with copies of the same system, which is wrong, just a waste of deployment time and configuration.

A more elegant solution, is to use the $http_host variable, and define a dynamic index file, like this:

index index.$http_host.php index.php index.html;

Now, be aware, this might not always be the best solution. also, most of the times, this will not be the specific setting or variable to use, but the idea is there.

Short variable swap in PHP > 5.4.x

Following this great old post from David Walsh’s post Tweet for Code #2, here is a PHP adaptation for this JavaScript Var Swap tweet:

$b = [$a, $a = $b][0];

Works on PHP 5.4 and up.

I know this is not very practical, for daily work, but it can come handy in a job interview.

♦ ♦ ♦

[Update:June 16, 2016]

In PHP 7.1.x it will finally be possible to use a cleaner swap short-code:

[$a, $b] = [$b, $a];


Gitlab / Github set custom branch as default

When using Gitlab / Github for development with large development groups, with or without branch per feature, you probably would want to use a development branch, and setting it as a default is a good idea. so when making a new clone you will automatically be in the development branch.

You need to keep in mind that deploying will now require the usage of -b master in the clone command (unless you are using tags, which is really a better idea, but just to be fair, in old installations you can’t clone into tags, so you can… no, just upgrade)

I attached screenshots from both Gitlab and Github’s settings page, just change the “Default branch”.



Gitlab settings page - default branch field


Github settings page - default branch field


Bad vs. Narrow-Minded

For the bad guy, contempt feeds his actions, penalty fits them.
But the narrow-minded guy, punishment feeds his narrowness, shame fits him.