Question

Can't access Volume from NGINX

Posted April 16, 2021 358 views
NginxDigitalOcean Volumes

I have a Droplet running named XXX.

Inside /etc/nginx/sites-enabled/XXX.xx

I have the following configuration.

server {

   server_name XXX.xx  www.XXX.xx;

   location / {
     proxy_pass http://localhost:5000;
     proxy_http_version 1.1;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection keep-alive;
     proxy_set_header Host $host;
     proxy_cache_bypass $http_upgrade;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /images/ {
        alias /mnt/XXXblob/images/;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/XXX.xx/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/XXX.xx/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}

Before that I also have runned

sudo chown -R www-data:www-data /mnt/XXXBlob/images/

When I look up /etc/fstab it has the following content.

LABEL=cloudimg-rootfs / ext4 defaults 0 0
LABEL=UEFI /boot/efi vfat defaults 0 0
/dev/disk/by-id/scsi-0DOVolumeXXXBlob /home/portal/XXXBlob ext4 defaults,nofail,discard 0 2

Any help are appreciated!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi there,

According to the fstab, your volume should be mounted at /home/portal/XXXBlob. What is the output of the following command:

df -h

What I could suggest here is to mount the volume not on /mnt but inside your document root, so for example if your website files are hosted at /var/www/html/ you could mount the disk to images folder at /var/www/html/images.

Regards,
Bobby

  • Hi Bobby!

    Thanks for the answer!

    My output from the command are as following:

    Filesystem      Size  Used Avail Use% Mounted on
    udev            474M     0  474M   0% /dev
    tmpfs            99M 1000K   98M   1% /run
    /dev/vda1        25G  8.8G   16G  37% /
    tmpfs           491M     0  491M   0% /dev/shm
    tmpfs           5.0M     0  5.0M   0% /run/lock
    tmpfs           491M     0  491M   0% /sys/fs/cgroup
    /dev/vda15      105M  3.9M  101M   4% /boot/efi
    tmpfs            99M     0   99M   0% /run/user/1000
    /dev/loop6       70M   70M     0 100% /snap/lxd/19188
    /dev/loop2       56M   56M     0 100% /snap/core18/1988
    /dev/loop7       71M   71M     0 100% /snap/lxd/19647
    /dev/loop3       33M   33M     0 100% /snap/snapd/11402
    /dev/loop0       56M   56M     0 100% /snap/core18/1997
    /dev/loop1       33M   33M     0 100% /snap/snapd/11588
    /dev/sda        9.8G   37M  9.3G   1% /mnt/XXXblob
    tmpfs            99M     0   99M   0% /run/user/0
    

    My website files are hosted and running from /home/portal/XXXBlob/imagges

    Do you have any suggestions, about what I’m missing?

    • Hi there,

      Indeed this looks correctly mounted at /mnt/XXXblob.

      If you run the ls /mnt/XXXblob command do you see the images folder there? And when running ls /mnt/XXXblob/images do you see your files?

      Also when accessing your domain.com/images/some_image.png does it load? If not are there any errors in your web console along with your Nginx error logs?

      Regards,
      Bobby

      • Hi again.

        Output for ls /mnt/XXXblob

        images lost+found

        And for running the last command, yes it shows me the files in the directory. But sadly, no - I can’t see it when I visit the browser.

        Can you tell me the command you want me to trigger, to get the Nginx error logs?

        • Hi there,

          When trying to visit an image, do you get a 403 error or 404?

          Regards,
          Bobby

          • Hi Bobby.

            I’m getting the following error when I visit the path:

            404 Not Found

            nginx/1.18.0 (Ubuntu)

          • Hi there,

            I’ve tested this at my end and I managed to get it working by removing the slash at the end of the alias path:

                location /images/ {
                    alias /mnt/XXXblob/images;
                }
            

            Let me know how it goes on your side!
            Regards,
            Bobby

          • Hi!

            I just tried to update the sites-available as suggested to the following.

            location /images/ {
                    alias /mnt/XXXblob/images;
            }
            

            I afterwards restarted and reloaded (Tried both)

            sudo systemctl reload nginx
            

            But I’m still getting 404 Not Found.

            Can you provide me with the steps you’re doing, since I must be missing out something?

          • Hi there,

            I was able to replicate the problem after adding the proxy pass rules.

            To fix this you could update your configuration to the following:

            upstream backend {
            server localhost:5000;
            }
            
            server {
                listen 443;
               server_name XXX.xx  www.XXX.xx;
            
            
                root /var/www/html;
            
                location / {
                   try_files $uri @backend;
                }
            
                location @backend {
                     proxy_pass http://backend;
                     proxy_http_version 1.1;
                     proxy_set_header Upgrade $http_upgrade;
                     proxy_set_header Connection keep-alive;
                     proxy_set_header Host $host;
                     proxy_cache_bypass $http_upgrade;
                     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                     proxy_set_header X-Forwarded-Proto $scheme;
                 }
            
                listen 443 ssl; # managed by Certbot
                ssl_certificate /etc/letsencrypt/live/XXX.xx/fullchain.pem; # managed by Certbot
                ssl_certificate_key /etc/letsencrypt/live/XXX.xx/privkey.pem; # managed by Certbot
                include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
                ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
            }
            
            

            Then once you have this in place, you would need to mount your volume to your document root defined in the Nginx configuration.

            For example, if you leave the /var/www/html as the document root, then you can mount your images volume to:

            /var/www/html/images
            

            Let me know how it goes!
            Regards,
            Bobby