nginx配置按路径年份切换访问路径

由 夕空 撰写于  2025年10月27日

用途:当系统运行久了,不可避免的文件空间不足,但想不改系统代码,将久远的文件迁移到他出,正好文件存储路径都带有年份,这就好办了。

(代码用AI生成,尚未测试,这种简单逻辑应该问题不大)


server {
listen 80;
listen 443 ssl;
server_name www.abc.com;

# ... SSL 配置 ...

location /file/download/ {
if ($args ~ ^url=upfiles/(files|img)/(\d{4})/) {
set $captured_year $2;
}
if ($captured_year <= 2020) {
return 301 $scheme://www.abcd.com$request_uri;
}
# 正常处理...
}

location ~ ^/upimg/(\d{4})/ {
if ($1 <= 2020) {
return 301 $scheme://www.abcd.com$uri$is_args$args;
}
# 正常处理...
}
}


负载均衡:保持原有链接切换服务器

# 定义内网存储服务器
upstream internal_storage {
server 192.168.1.100:8080; # 内网存储服务器地址
server 192.168.1.101:8080; # 可以配置多个
}

server {
listen 80;
listen 443 ssl;
server_name www.abc.com;

# ... SSL 配置 ...

location /file/download/ {
if ($args ~ ^url=upfiles/(files|img)/(\d{4})/) {
set $captured_year $2;
}

if ($captured_year <= 2020) {
# 使用内网upstream
proxy_pass http://internal_storage;
proxy_set_header Host internal-storage.local;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

# 添加内网访问标识(可选)
add_header X-Internal-Access "true";

# 禁止响应头中的跳转指令
proxy_redirect off;
break;
}

# 正常处理2020年之后的文件...
}

location ~ ^/upimg/(\d{4})/ {
set $img_year $1;

if ($img_year <= 2020) {
# 使用内网upstream
proxy_pass http://internal_storage;
proxy_set_header Host internal-storage.local;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

# 添加内网访问标识(可选)
add_header X-Internal-Access "true";

# 禁止响应头中的跳转指令
proxy_redirect off;
break;
}

# 正常处理2020年之后的图片...
}
}


判断指定状态码进行轮询其它内网IP,配置超时

upstream internal_storage {
server 192.168.1.100:8080 max_fails=3 fail_timeout=30s;
server 192.168.1.101:8080 max_fails=3 fail_timeout=30s;

# 大文件下载使用最少连接算法
# least_conn;
keepalive 32;
}

server {
listen 80;
listen 443 ssl;
server_name www.abc.com;

# ==================== 处理 /file/download/ 路径 ====================
location /file/download/ {

proxy_pass http://internal_storage;
proxy_set_header Host internal-storage.local;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

# 禁止响应头中的跳转指令
proxy_redirect off;

# ========== 大文件下载专用超时配置 ==========

proxy_connect_timeout 10s; # 连接超时
# 读取超时(关键)- 大文件下载需要很长时间
proxy_read_timeout 3600s; # 1小时 读取响应数据的超时
proxy_send_timeout 10s; # 发送超时

# ========== 缓冲配置 ==========
# 禁用缓冲,让数据直接传输,避免超时
proxy_buffering off;

# 禁用临时文件
proxy_max_temp_file_size 0;

# ========== 重试配置 ==========
proxy_intercept_errors on;
proxy_next_upstream error timeout invalid_header
http_500 http_502 http_503 http_504
http_401 http_403;
proxy_next_upstream_tries 2; #最多尝试2台服务器
proxy_next_upstream_timeout 60s; # 重试总超时时间

# 添加调试头
add_header X-Internal-Access "true";
add_header X-Upstream-Addr $upstream_addr;
add_header X-Upstream-Status $upstream_status;

}

# ==================== 处理 /upimg/ 路径 ====================
location /upimg/ {

proxy_pass http://internal_storage;
proxy_set_header Host internal-storage.local;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

# 禁止响应头中的跳转指令
proxy_redirect off;

# ========== 图片等小文件超时配置 ==========
proxy_connect_timeout 10s; # 连接超时
# 读取超时(关键)- 大文件下载需要很长时间
proxy_read_timeout 30s; # 30秒 读取响应数据的超时
proxy_send_timeout 10s; # 发送超时

# ========== 缓冲配置 ==========
# 小文件开启缓冲,提高性能
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 32 4k;
proxy_busy_buffers_size 64k;

# ========== 重试配置 ==========
proxy_intercept_errors on;
proxy_next_upstream error timeout invalid_header
http_500 http_502 http_503 http_504
http_302 http_404;
proxy_next_upstream_tries 2; #最多尝试2台服务器
proxy_next_upstream_timeout 10s;

add_header X-Internal-Access "true";
add_header X-Upstream-Addr $upstream_addr;
add_header X-Upstream-Status $upstream_status;

}
}


声明:星耀夕空|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - nginx配置按路径年份切换访问路径


欢迎光顾我的小站!