Access digital ocean volume using nginx alias

October 20, 2016 118 views
Nginx Ubuntu 16.04

I would like to alias /home/mrneilypops/databank to /mnt/volume-fra1-01/databank
I followed the guide here for nginx;

https://www.digitalocean.com/community/questions/how-do-i-access-the-space-on-an-attached-volume-from-my-webroot

I added the alias to /etc/nginx/sites-available/default as per;

location /home/mrneilypops/databank/ {
                alias /mnt/volume-fra1-01/databank/;
        }

This does not alias the volume databank file.

Any tips would be welcome...

3 comments
  • As per?

    location /databank/ {
                    alias /mnt/volume-fra1-01/databank/;
            }
    
  • Wait a second. Do you want to when you or someone else type http://droplet-ip/home/mrneilypops/databank/ in browser it serves content from volume?
    Or you want when you go to /home/mrneilypops/databank/ in your console, SSH, it shows you content of it?
    If you want second, this tutorial will not work, as it works only if you want to serve it on Web, via nginx.

  • I would like to access /home/mrneilypops/databank/ and show content of /mnt/volume-fra1-01/databank/
    I am running Ghost blog on my site so I don't know how I would access something like;
    http://droplet-ip/home/mrneilypops/databank/ in a browser...this would be the ideal solution.

    Any suggestions welcome.

2 Answers

Read the guide again, note how location is a relative path, not an absolute one.

You are right with your configuration if you want to use http://droplet-ip/databank/.
In your nginx server block you already added:

location /databank/ {
                alias /mnt/volume-fra1-01/databank/;
}

So that's all. Make sure you restarted nginx after that. You can also test nginx configuration with:

  • sudo nginx -t

It should ouput something in line of this:

Sample output of sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If this is OK, restart nginx:

  • sudo systemctl restart nginx

Now there some important point:
Accessing http://droplet-ip/databank will return 404 Not Found!
Accessing via http://droplet-ip/databank/ (note the red slash) will work.
To fix that remove last / in location /databank/. You will have now:

location /databank {
                alias /mnt/volume-fra1-01/databank/;
}

Restart nginx again and when you go to http://droplet-ip/databank it will return now 403 Forbidden.
This is because you didn't specified file to open. Go to /mnt/volume-fra-01/databank/ and create some html file (e.g. file called abc.html).

  • nano abc.html

Write some html code in it:

nano abc.html
<html>
    <head>
        <title>Hello World!</title>
    </head>
    <body>
        <p>Hello World!</p>
    </body>
</html>

Save it and exit.
Now go to http://droplet-ip/databank/abc.html from your browser and you should see Hello World! paragraph.
If for some reason when you go to http://droplet-ip/databank you want to list all files, you can add autoindex on under nginx location block. This will show you all files you have in databank when you access http://droplet-ip/databank.
For that your location block should look like this:

location /databank/ {
                alias /mnt/volume-fra1-01/databank/;
                autoindex on;
}

Restart your nginx once again, go to http://droplet-ip/databank/abc.html and you should see all files from databank directory on disk.
Note that this is not something so recommended. Keep in mind that anybody will be able to see all your files in /mnt/volume-fra1-01/databank/.

If this is what you want, good luck, I hope it works for you :)

Have another answer? Share your knowledge.