nginx的x-sendfile


nginx的x-sendfile

设置

X-sendfile将文件上传的请求由后端转交个前端web服务器处理。用来消除后端程序读文件和处理文件的压力。提高后端服务器的处理效率

client-----》nginx------》fileserver

client访问nginx,nginx请求fileserver。fileserver读取文件返回给nginx,再返回给前端。

使用x-sendfile后。

client----》nginx-------》fileserver
           |
           |----》local disk

client访问nginx,nginx请求fileserver,fileserver不返回文件,只返回一个x-sendfile头给nginx,nginx获取到fileserver头之后,根据nginx的location做匹配,访问到后端或者本地硬盘。后端的fileserver专注处理业务逻辑。

配置

  • fileserver的配置
这里用一个nginx做后端

[root@test-1 vhost]# cat oss_backend.conf 
server
{
    listen       80;
    server_name  oss-backend.test.com;

        location / {
                set $resp_body "";
                header_filter_by_lua '
                        ngx.header["X-Accel-Redirect"]= "/files/aaaa/Centos7.iso";
                        -- ngx.header["X-Accel-Redirect"]= "/img/liran-test.png/100x100";
                        -- ngx.header["X-Accel-Redirect"]= "/img/liran-test.png";
                        ngx.header["Content-Type"]= "application/octet-stream";
                        ngx.header["Accept-Ranges"]= "bytes";

                        -- ngx.header["Content-Type"]= "image/png";
                        -- ngx.header["Content-Disposition"] = "attachment; filename=liran-test.png";
                        -- ngx.header["Content-Length"] = "962592768";
                        -- ngx.header["Content-Length"] = "71907";

                ';
                return 200;
        }
}

说明:
这里的域名只返回一些HEADER头。不做文件传输。

主要的header
- X-Accel-Redirect返回一个文件url
  • nginx配置
[root@test-1 vhost]# cat test_slice.conf
server
{
    listen       80;
    server_name  slice.test.com;


                location /files/ {
                        internal;
                        slice 1m;
                        #alias /webser/www/storage/xc/;
                        proxy_next_upstream http_502 http_504 error timeout invalid_header;
                        proxy_cache cache_one;
                        proxy_cache_valid  200 304 36000s;
                        add_header X-Cache $upstream_cache_status;



                        proxy_cache_key $scheme$host$uri$is_args$args$slice_range;

                        proxy_set_header  Range $slice_range;


                        proxy_set_header Host  $host;
                        proxy_set_header X-Forwarded-For  $remote_addr;
                        proxy_set_header X-Forwarded-Host $server_name;
                        proxy_set_header X-Forwarded-Proto  $scheme;
                        proxy_pass http://10.0.6.20;
                        #proxy_pass http://10.0.6.20;
                }


        #location ~* /img/(.*)\.(png|gif|jpg)/([0-9]+)x([0-9]+)$ {
        location /img/ {
                internal;
                #alias /webser/www/storage/xc/img/$1.$2;
                #set $img_width $3;
                #set $img_height $4;
                #image_filter resize $img_width $img_height;
                #image_filter_jpeg_quality 95;
                #image_filter_buffer 100M;
                proxy_next_upstream http_502 http_504 error timeout invalid_header;
                        proxy_cache cache_one;
                        proxy_cache_valid  200 304 36000s;
                        add_header X-Cache $upstream_cache_status;



                        proxy_cache_key $scheme$host$uri$is_args$args$slice_range;

                        proxy_set_header  Range $slice_range;


                        proxy_set_header Host  $host;
                        proxy_set_header X-Forwarded-For  $remote_addr;
                        proxy_set_header X-Forwarded-Host $server_name;
                        proxy_set_header X-Forwarded-Proto  $scheme;
                        proxy_pass http://10.0.6.20;

        }


                location /download/ 
                {

                    proxy_pass        http://oss-backend.test.com;
                    #proxy_pass        http://10.0.6.20;
                }


}

说明:
- 访问/download会转发到后端的filerserver
- 根据filerserver返回值,匹配/files/,然后根据location相关处理