How to use block storage for existing website without delete/moving it to the block storage?

March 8, 2018 1.7k views
Nginx Ubuntu 16.04

How to use block storage for existing website without delete/moving it to the block storage?
my /dev/sda1 is now full, I attach a new volume like 50GB, and it already mouted in /mnt/volume....
I’m using easyengine nginx to host my website. My website is located at /var/www/example.com/htdocs, and I want to use my block storage to save my wp-content that located in /var/www/example.com/htdocs/wp-content. so It will saved in block storage… what should I do? I’ve try this
Link

I try this

mkdir /mnt/volume.../wp-content
sudo chown -R www-data:www-data /mnt/volume.../wp-content

and edit /etc/nginx/sites-enabled/default
and add this to the last line

        location /wp-content/ {
                alias /mnt/volume.../wp-content/;
        }

after that, I save it, and restart nginx service, and it got error serverctl1....
can you help me how to implement this?

4 comments
  • Check your nginx config file for errors using this command:

    sudo nginx -t
    

    What does it output?

  • it said like this

    nginx: [emerg] "location" directive is not allowed here in /etc/nginx/sites-enabled/default:84
    nginx: configuration file /etc/nginx/nginx.conf test failed
    root@tes:~# [emerg] "location" directive is not allowed here in /etc/nginx/sites-enabled/default:84
    [emerg]: command not found
    
    

    here is /etc/nginx/nginx.conf file

    
    user www-data;
    worker_processes auto;
    worker_rlimit_nofile 100000;
    pid /run/nginx.pid;
    
    events {
            worker_connections 4096;
            multi_accept on;
    }
    
    http {
            ##
            # EasyEngine Settings
            ##
    
            sendfile on;
            tcp_nopush on;
            tcp_nodelay on;
            keepalive_timeout 30;
            types_hash_max_size 2048;
    
            server_tokens off;
            reset_timedout_connection on;
            add_header X-Powered-By "EasyEngine 3.7.5";
            add_header rt-Fastcgi-Cache $upstream_cache_status;
    
            # Limit Request
            limit_req_status 403;
            limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
    
            # Proxy Settings
            # set_real_ip_from      proxy-server-ip;
            # real_ip_header        X-Forwarded-For;
    
            fastcgi_read_timeout 300;
            client_max_body_size 100m;
    
            ##
            # SSL Settings
            ##
    
            ssl_session_cache shared:SSL:20m;
            ssl_session_timeout 10m;
            ssl_prefer_server_ciphers on;
            ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    
            ##
            # Basic Settings
            ##
            # server_names_hash_bucket_size 64;
            # server_name_in_redirect off;
    
            include /etc/nginx/mime.types;
            default_type application/octet-stream;
    
            ##
            # Logging Settings
            ##
    
            access_log /var/log/nginx/access.log;
            error_log /var/log/nginx/error.log;
    
            # Log format Settings
            log_format rt_cache '$remote_addr $upstream_response_time $upstream_cache_status [$time_local] '
            '$http_host "$request" $status $body_bytes_sent '
            '"$http_referer" "$http_user_agent"';
    
            ##
            # Gzip Settings
            ##
    
            gzip on;
            gzip_disable "msie6";
    
            gzip_vary on;
            gzip_proxied any;
            gzip_comp_level 6;
            gzip_buffers 16 8k;
            gzip_http_version 1.1;
            gzip_types
                application/atom+xml
                application/javascript
                application/json
                application/rss+xml
                application/vnd.ms-fontobject
                application/x-font-ttf
                application/x-web-app-manifest+json
                application/xhtml+xml
                application/xml
                font/opentype
                image/svg+xml
                image/x-icon
                text/css
                text/plain
                text/x-component
                text/xml
                text/javascript;
    
            ##
            # Virtual Host Configs
            ##
    
            include /etc/nginx/conf.d/*.conf;
            include /etc/nginx/sites-enabled/*;
    }
    
    
    #mail {
    #       # See sample authentication script at:
    #       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
    #
    #       # auth_http localhost/auth.php;
    #       # pop3_capabilities "TOP" "USER";
    #       # imap_capabilities "IMAP4rev1" "UIDPLUS";
    #
    #       server {
    #               listen     localhost:110;
    #               listen     localhost:110;
    #               protocol   pop3;
    #               proxy      on;
    #       }
    #
    #       server {
    #               listen     localhost:143;
    #               protocol   imap;
    #               proxy      on;
    #       }
    #}
    
    
    

    and here is /etc/nginx/sites-enabled/default

    ##
    # You should look at the following URL's in order to grasp a solid understanding
    # of Nginx configuration files in order to fully unleash the power of Nginx.
    # http://wiki.nginx.org/Pitfalls
    # http://wiki.nginx.org/QuickStart
    # http://wiki.nginx.org/Configuration
    #
    # Generally, you will want to move this file somewhere, and start with a clean
    # file but keep this around for reference. Or just disable in sites-enabled.
    #
    # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
    ##
    
    # Default server configuration
    #
    server {
            listen 80 default_server;
            listen [::]:80 default_server;
    
            # SSL configuration
            #
            # listen 443 ssl default_server;
            # listen [::]:443 ssl default_server;
            #
            # Self signed certs generated by the ssl-cert package
            # Don't use them in a production server!
            # include snippets/snakeoil.conf;
            #
            # ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # don’t use SSLv3 ref: POODLE
            # ssl_ciphers HIGH:!aNULL:!MD5;
            # ssl_prefer_server_ciphers on;
    
            root /var/www/html;
    
            # Add index.php to the list if you are using PHP
            index index.html index.htm index.nginx-debian.html;
    
            server_name _;
    
            location / {
                    # First attempt to serve request as file, then
                    # as directory, then fall back to displaying a 404.
                    try_files $uri $uri/ =404;
            }
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #location ~ \.php$ {
            #       include snippets/fastcgi-php.conf;
            #
            #       # With php5-cgi alone:
            #       fastcgi_pass 127.0.0.1:9000;
            #       # With php5-fpm:
            #       fastcgi_pass unix:/var/run/php5-fpm.sock;
            #}
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            # concurs with nginx's one
            #
            #location ~ /\.ht {
            #       deny all;
            #}
    }
    
    
    # Virtual Host configuration for example.com
    #
    # You can move that to a different file under sites-available/ and symlink that
    # to sites-enabled/ to enable it.
    #
    #server {
    #       listen 80;
    #       listen [::]:80;
    #
    #       server_name example.com;
    #
    #       root /var/www/example.com;
    #       index index.html;
    #
    #       location / {
    #               try_files $uri $uri/ =404;
    #       }
    #}
            location /wp-content/ {
                    alias /mnt/volume-sfo2-01/wp-content;
            }
    
    
    
  • You have defined the wp-content block outside of the server block, in the /etc/nginx/sites-enabled/default file. That causes nginx to fail.
    Move it to the first server block, for example after location / block (you can put it anywhere as long as it’s in the first server block):

    ...
    server {
    ...
           server_name _;
    
            location / {
                    # First attempt to serve request as file, then
                    # as directory, then fall back to displaying a 404.
                    try_files $uri $uri/ =404;
            }
    
           location /wp-content/ {
                    alias /mnt/volume-sfo2-01/wp-content;
            }
    ...
    }
    

    Then, try to run nginx -t again, and if everything is okay, restart Nginx to put changes in the effect: sudo systemctl restart nginx.

  • thanks, it’s worked..
    but when I upload an image to my site, it’s still saved in example.com/htdocs/wp-content/....
    it’s not saved in /mnt/vol…/wp-content
    is there something wrong? or I was missunderstood about aliases?

1 Answer

@novaneviyantia Wordpress accesses the filesystem directly in order to save files—it doesn’t go through Nginx. So while Nginx will still ‘redirect’ requests to the Volume, Wordpress will save files in /var/www/html/wp-content.

You can use a symlink in order to have Wordpress save files on the Volume as well (make sure the paths are correct—I tried to guess based on your config):

# move any existing files to the Volume
sudo mv /var/www/html/wp-content/* /mnt/volume-sfo2-01/wp-content/*
# delete the old (now empty) directory
sudo rm -r /var/www/html/wp-content
# create a symlink to points to wp-content on the Volume
sudo ln -s /mnt/volume-sfo2-01/wp-content /var/www/html/wp-content

Because this is set up on the filesystem, you can also remove the Nginx alias and it would still work.

Have another answer? Share your knowledge.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!