I have a small web app that uploads files to the server to later be displayed on the web. Currently my server HD is almost full and I'd like to add a second one. Currently the app and all existing files are on drive1
and I will be adding drive2
. Is it possible using something like try_files
to check multiple locations to see if the file exists?
For example, if someone requests mysite.com/images/2349879.jpg
nginx would look for /drive2/images/2349879.jpg
first, if it doesn't exist it would check check drive1/images/2349879.jpg
and if it isn't there serve a 404?
This is my current nginx.conf
server {
listen 80 default_server;
listen 443 ssl;
server_name www.MYSITE.com _;
ssl_certificate /srv/apps/MYSITE-ssl/certs/MYSITE.com.pem;
ssl_certificate_key /srv/apps/MYSITE-ssl/private/MYSITE.com.key;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers RC4:HIGH:!aNULL:!MD5:!kEDH;
ssl_prefer_server_ciphers on;
set $https off;
if ( $scheme = 'https' ) { set $https on; }
add_header Strict-Transport-Security "max-age=315360000; includeSubdomains";
keepalive_timeout 70;
return 301 $scheme://MYSITE.com$request_uri;
}
server {
listen 80;
listen 443 ssl;
server_name MYSITE.com;
ssl_certificate /srv/apps/MYSITE-ssl/certs/MYSITE.com.pem;
ssl_certificate_key /srv/apps/MYSITE-ssl/private/MYSITE.com.key;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers RC4:HIGH:!aNULL:!MD5:!kEDH;
ssl_prefer_server_ciphers on;
set $https off;
if ( $scheme = 'https' ) { set $https on; }
add_header Strict-Transport-Security "max-age=315360000; includeSubdomains";
keepalive_timeout 70;
root /var/www/MYSITE.com;
charset utf-8;
access_log /srv/apps/logs/MYSITE.access.log main;
location /images {
internal;
}
location /images/ {
internal;
}
location ~ ^/images/(.*)$ {
alias /srv/apps/MYSITE/i/$1;
include /srv/apps/MYSITE.hosts;
expires max;
add_header Pragma "public";
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
log_not_found off;
}
location ~* \.(?:png|jpe?g|gif|ico|bmp|xml)$ {
include /srv/apps/MYSITE.hosts;
expires max;
add_header Pragma "public";
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
log_not_found off;
}
location /i/ {
internal;
try_files $uri /srv/imagestorage$uri /srv/apps/MYSITE/i$uri;
}
location /i {
internal;
try_files $uri /srv/imagestorage$uri /srv/apps/MYSITE/i$uri;
}
location / {
index index.php
try_files $uri $uri/ /index.php?$args;
}
error_page 404 /info.php?act=404;
error_page 500 /info.php?act=500;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~* \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS $https;
fastcgi_pass php;
}
location ~* \.(?:js|css)$ {
expires max;
log_not_found off;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
location ~ /\. {
deny all;
}
}
There are a few weird things happening here so I'll try to explain: the files are located in /srv/apps/MYSITE/i
and the URL mysite.com/i/file.jpg
will return a file from that location. A request to mysite.com/images/file.jpg
will return the same file.
The code I tried is try_files $uri /srv/imagestorage$uri /srv/apps/MYSITE/i$uri;
in two locations above however it doesn't seem to be working at all (I can still see files in /i
but it does not check the new drive (/srv/imagestorage
) at all. Maybe I am adding try_files
in the wrong location?
Best Answer
You can simply do this :
Then
http(s)://mysite.com/images/1.png
will be served using local filesystem path/drive1/images/1.png
or/drive2/images/1.png
if the file is missing from the first path or return a 404.Obviously that's an exemple, you should never setup your root filesystem path in any webserver configuration.