从nginx中的两个位置之一拉取静态文件

我有一个小型的networking应用程序,上传文件到服务器,以便以后在网上显示。 目前我的服务器HD已满,我想添加第二个。 目前该应用程序和所有现有的文件都在drive1 ,我将添加drive2 。 是否有可能使用类似try_files检查多个位置来查看文件是否存在?

例如,如果有人要求mysite.com/images/2349879.jpg nginx会首先查找/drive2/images/2349879.jpg ,如果它不存在,它将检查检查drive1/images/2349879.jpg ,如果它不是那里服务404?


这是我目前的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; } } 

在这里发生了一些奇怪的事情,所以我会试着解释:文件位于/srv/apps/MYSITE/i和URL mysite.com/i/file.jpg将从该位置返回一个文件。 对mysite.com/images/file.jpg的请求将返回相同的文件。

我试过的代码是try_files $uri /srv/imagestorage$uri /srv/apps/MYSITE/i$uri; 在上面的两个位置,但它似乎并没有工作(我仍然可以在/i看到的文件,但它根本不检查新的驱动器( /srv/imagestorage )。也许我是在错误的位置添加try_files

你可以简单地做到这一点:

 server { [...] root /; location /images { try_files /drive1$uri /drive2$uri; } } 

然后http(s)://mysite.com/images/1.png将使用本地文件系统path/drive1/images/1.png/drive2/images/1.png如果该文件从第一个path丢失或返回一个404。

显然,这是一个例子,你不应该在任何web服务器configuration中设置你的根文件系统path。