Change your Nginx docroot
Some applications may require you to use a different docroot than the typical directory of
~/public_html. Other times, your own application deploy flow may require you to change Nginx's
docroot, for example to point it to ~/current. In this guide, we'll quickly go over some common
situations and ways to set this up.
Note
This is mostly applicable for general applications. Many CMSes and frameworks have a custom docroot path, that we configure by default in Nginx if you set up your application with the correct application type. Be sure to check your current settings before making any changes!
Using symlinks
The easiest way to make sure Nginx reads application files in the correct directory, is simply by turning the default directory into a symlink, pointing to the desired directory. Consider the following default directory setup:
sander@web1:~$ ls -l
<...>
drwxr-xr-x 2 sander sander 4096 Apr 13 00:00 logs
drwxr-xr-x 2 sander sander 4096 Jan 13 09:24 nginx
lrwxrwxrwx 1 sander sander 4096 Apr 14 11:53 public_html
<...>
Let's say your app typically deploys with a directory releases/, which contains the actual
different releases and a symlink current, linking to the most recent release. This is the default
Deployer setup. In that case, you can simply remove the existing, empty public_html directory and
replace it with a symlink:
sander@web1:~$ rmdir public_html
sander@web1:~$ ln -s current public_html
sander@web1:~$ ls -l
<...>
lrwxrwxrwx 1 sander sander 11 Apr 14 11:53 current -> releases/56
drwxr-xr-x 2 sander sander 4096 Jan 13 09:24 nginx
lrwxrwxrwx 1 sander sander 7 Apr 14 11:53 public_html -> current
<...>
Using the Nginx docroot
Alternatively, you could change the actual path Nginx looks for. In your home directory you'll find
the ~/nginx config directory, containing the 50main.conf file. Here, you'll find your main
application Nginx config, including the docroot. Depending on your configured app type, the exact
contents of this file will change. However, you can edit this file as desired. An example of how the
docroot is defined in this file is below:
root /var/www/sander/public_html;
Here you could change the public_html part to any path as required by your application. After
editing, apply the change by reloading Nginx with the TurboStack CLI:
tscli nginx reload
Warning
Editing 50main.conf needs Nginx knowledge - a mistake can take the site offline.
tscli nginx reload validates the configuration and reports the error if there is one.
Using the documentroot setting
TurboStack also has a platform-native override: the documentroot setting on the website. It changes
which folder the web server serves files from, and the platform keeps the generated configuration in
sync - so it survives platform updates without editing 50main.conf by hand. This is the cleanest
option when a framework serves from a fixed subfolder such as public or web.
system_users:
- username: prod
vhosts:
- server_name: example.com www.example.com
documentroot: "/var/www/prod/public_html/public" # absolute path to the framework's public/ subfolder
See the documentroot reference for the full field description, then publish the host to apply it - see Publish.
Related
- Configure Nginx
- What is Nginx?
- documentroot reference
- TurboStack CLI - reload Nginx with
tscli nginx reload