Ruby on rails 使用Nginx+;时缺少内容长度标题;Gzip+;独角兽

Ruby on rails 使用Nginx+;时缺少内容长度标题;Gzip+;独角兽,ruby-on-rails,nginx,gzip,unicorn,Ruby On Rails,Nginx,Gzip,Unicorn,我不知道为什么我在nginx中使用gzip时http响应遗漏了“内容长度头”,我真的被卡住了,请有人帮帮我,非常感谢! 这是我的配置文件 nginx.conf 用户无人无人; 工人8人 events { worker_connections 1024; accept_mutex on; # "on" if nginx worker_processes > 1 use epoll; # enable for Linux 2.6+ } http { incl

我不知道为什么我在nginx中使用gzip时http响应遗漏了“内容长度头”,我真的被卡住了,请有人帮帮我,非常感谢! 这是我的配置文件

nginx.conf

用户无人无人; 工人8人

events {
    worker_connections  1024;
    accept_mutex on; # "on" if nginx worker_processes > 1
    use epoll; # enable for Linux 2.6+
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    upstream backend-unicorn {
        server unix:/tmp/unicorn_app.sock fail_timeout=0;
        #server localhost:5000;
    }
    #access_log  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  100;
    gzip on;
    gzip_static on;
    gzip_proxied any;
    gzip_http_version 1.1;
    gzip_comp_level 6;
    gzip_min_length  1100;
    gzip_buffers 16 8k;
    gzip_types text/plain application/zip text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript image$

    server {
        listen       8080;
        server_name  misago;
        access_log  /var/log/nginx/unicorn.access.log  main;
        client_max_body_size 64M;
        location /uploads/ {
            root /usr/local/rails_apps/me_management_tool/current/public/uploads;
            break;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

}
我不知道为什么在nginx中使用gzip时,http响应会丢失“内容长度头”


因为在这种情况下,在发送标头时,内容的长度是未知的。Nginx无法知道内容的压缩程度。

下面是一个性能良好的Nginx解决方案,可将
x-file-size
头添加到Gzip响应中。在此进行全面讨论:

位置/{
##必须安装Nginx Lua模块https://docs.nginx.com/nginx/admin-guide/dynamic-modules/lua/
## https://github.com/openresty/lua-nginx-module#header_filter_by_lua
标题\u过滤器\u按\u lua\u块{
功能文件(文件名)
本地文件=io.open(文件名“r”)
如果(file==nil),则返回-1 end
本地大小=文件:查找(“结束”)
文件:close()
返回大小
结束
ngx.header[“X-File-Size”]=文件长度(ngx.var.request\u文件名);
}
}

但是在发送gzipped响应之前,Nginx是否有办法重新计算新的
内容长度
?因为Nginx必须在知道gzipped内容的总长度之前将gzipped内容的第一块流式传输到客户端。请参阅下面的
x-file-size
解决方案