Switch between branches using Nginx

For some of my projects, most of the time I use two branches: master and dev and I work mostly in dev. If I need to send the url to some clients with some dev/beta features I should create another (sub)domain like dev.myproject.com and sometimes, if I forget to change some configurations about the domain name (specially with some “very intelligent” CMSes that keeps the configuration in the database) I will get an email back saying “the new feature is not working” 🙂

In the end, I wanted to have an easy solution to switch between branches, keeping the same domain and to not confuse the clients or the QA with my beta/dev/test/acceptance subdomains. Everything should be on: http://myproject.com.

So, how I did it?
My project has the root folder in /var/www/project_dev/web for the dev branch and /var/www/project_master/web for the master branch.

In this case, I have the following nginx config file (/etc/nginx/sites-enabled/project)


In this config file I specify, if I have a cookie called “branch” to read it’s value. If the value is “dev” it will send the request further to the nginx running on port 8080, called dev. If the cookie doesn’t exists or it is “master” it will send the request to the nginx running on port 8888.

And I have also another two nginx config files: /etc/nginx/sites-enabled/dev and /etc/nginx/sites-enabled/master

In dev file I have:

And in master file I just changed:


After I setup this nginx configuration, in my project I created a file, set.php through which I create the specific cookie. The PHP code is very simple. I just read the GET branch parameter and I setup a cookie.

The file set.php should be in /var/www/project_dev/web as well in /var/www/project_master/web.


Now, I can just do: http://myproject.com/set.php?branch=dev and I will see the dev branch and if I want to switch back on master, I just have to go to http://myproject.com/set.php?branch=master

In the future I hope to write another blog post in which to present a more elegant solution, without 3 nginx config files and a PHP script to set up cookies.

All the code can be found on Github.

If you found useful this blog post, please click some of the share buttons below. Thanks! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.