我有一个小型网络应用程序,可以将文件上传到服务器,以便稍后显示在网络上。目前我的服务器硬盘几乎已满,我想添加第二个。目前该应用程序和所有现有文件都已打开drive1,我将添加drive2. 是否可以使用类似try_files检查多个位置来查看文件是否存在的方法?
例如,如果有人请求,mysite.com/images/2349879.jpgnginx 会首先查找/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;
}
}
Run Code Online (Sandbox Code Playgroud)
这里发生了一些奇怪的事情,所以我将尝试解释一下:文件位于/srv/apps/MYSITE/i并且 URLmysite.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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后http(s)://mysite.com/images/1.png将使用本地文件系统路径提供服务/drive1/images/1.png,或者/drive2/images/1.png如果第一个路径中缺少文件或返回 404。
显然这是一个例子,你不应该在任何网络服务器配置中设置根文件系统路径。
| 归档时间: |
|
| 查看次数: |
2641 次 |
| 最近记录: |