修改nginx返回路径

修改nginx返回路径

我正在尝试让本地 Telegram Bot API 中的文件下载工作。我在 github 上找到了一个如何使用 nginx 执行此操作的示例:https://github.com/aiogram/telegram-bot-api/tree/master/example。它与相对路径(例如documents/file_0.txt)配合得很好。但我需要通过完整路径请求文件(例如/var/lib/telegram-bot-api/bot-token/documents/file_0.txt)。当我尝试这样做时,nginx 正在寻找/var/lib/telegram-bot-api/完整路径并抛出 404。我需要在这里更改什么来修复它?我是 nginx 的完全菜鸟。这是配置:

log_format token_filter '$remote_addr - $remote_user [$time_local] '
                        '"$sanitized_request" $status $body_bytes_sent '
                        '"$http_referer" "$http_user_agent"';

upstream telegram-bot-api {
    server api:8081;
}

server {
    listen 80;
    server_name _;

    chunked_transfer_encoding on;
    proxy_connect_timeout 600;
    proxy_send_timeout 600;
    proxy_read_timeout 600;
    send_timeout 600;
    client_max_body_size 2G;
    client_body_buffer_size 30M;
    keepalive_timeout 0;
    
    set $sanitized_request $request;
    if ( $sanitized_request ~ (\w+)\s(\/bot\d+):[-\w]+\/(\S+)\s(.*) ) {
        set $sanitized_request "$1 $2:<hidden-token>/$3 $4";
    }
    access_log /var/log/nginx/access.log token_filter;

    location ~* \/file\/bot\d+:(.*) {
        rewrite ^/file\/bot(.*) /$1 break;
        try_files $uri @files;
    }

    location / {
        try_files $uri @api;
    }

    location @files {
        root /var/lib/telegram-bot-api;
        gzip on;
        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 6;
        gzip_buffers 64 8k;
        gzip_http_version 1.1;
        gzip_min_length 1100;
    }

    location @api {
        proxy_pass  http://telegram-bot-api;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name;
    }
}```

相关内容