Rails 忽略 config.action_dispatch.x_sendfile_header?使用薄 + nginx

发布于 2024-12-01 18:27:15 字数 1822 浏览 0 评论 0原文

我已经设置了一个运行 Rails 3.1.0rc6、Thin 和 Nginx 的生产环境。

由于某种原因,在 config/environments/Production.rb 中设置了 config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" 后,Rails 似乎完全忽略了它;资源没有被提供,并且一个文件的响应标头如下:

Server: nginx/1.0.5
Date: Sun, 28 Aug 2011 00:26:08 GMT
Content-Type: image/png
Content-Length: 0
Cache-Control: no-cache
Last-Modified: Sat, 27 Aug 2011 23:47:35 GMT
Etag: "af4810c52cb323d9ed061d1db5b4f296"
X-UA-Compatible: IE=Edge,chrome=1
X-Sendfile: /var/www/***/app/assets/images/bg-linen-light.png
X-Runtime: 0.004595
X-Content-Digest: da39a3ee5e6b4b0d3255bfef95601890afd80709
Age: 0
X-Rack-Cache: stale, valid, store

200 OK

因此看来 Rails 仍在设置 X-Sendfile 标头。我尝试将 sendfile_header 行添加到 config/application.rb 中,但我猜它也被覆盖或忽略。

我的 Thin yml 文件:

--- 
chdir: /var/www/***
environment: production
address: 0.0.0.0
port: 3000
timeout: 30
log: log/thin.log
pid: tmp/pids/thin.pid
max_conns: 1024
max_persistent_conns: 512
require: []

wait: 30
servers: 1
daemonize: true

Nginx vhost:

upstream *** {
    server 127.0.0.1:3000;
}

server {
    listen 80;
    server_name ***;
    root /var/www/***/public;
    index index.html;

    location / {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (-f $request_filename/index.html) {
            rewrite (.*) $1/index.html break;
        }

        if (-f $request_filename.html) {
            rewrite (.*) $1.html break;
        }

        if (!-f $request_filename) {
            proxy_pass http://***;
            break;
        }
    }
}

我已经尝试过 /etc/init.d/thin stop 然后再次启动几次,但无济于事。

I've set up a production environment running Rails 3.1.0rc6, Thin and Nginx.

For some reason, having set config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" in config/environments/production.rb, Rails seems to have completely ignored it; assets aren't being served, and the response headers for one file is as follows:

Server: nginx/1.0.5
Date: Sun, 28 Aug 2011 00:26:08 GMT
Content-Type: image/png
Content-Length: 0
Cache-Control: no-cache
Last-Modified: Sat, 27 Aug 2011 23:47:35 GMT
Etag: "af4810c52cb323d9ed061d1db5b4f296"
X-UA-Compatible: IE=Edge,chrome=1
X-Sendfile: /var/www/***/app/assets/images/bg-linen-light.png
X-Runtime: 0.004595
X-Content-Digest: da39a3ee5e6b4b0d3255bfef95601890afd80709
Age: 0
X-Rack-Cache: stale, valid, store

200 OK

So it seems Rails is still setting the X-Sendfile header. I've tried adding the sendfile_header line to config/application.rb, but I'm guessing it's being overriden or ignored as well.

My yml file for Thin:

--- 
chdir: /var/www/***
environment: production
address: 0.0.0.0
port: 3000
timeout: 30
log: log/thin.log
pid: tmp/pids/thin.pid
max_conns: 1024
max_persistent_conns: 512
require: []

wait: 30
servers: 1
daemonize: true

Nginx vhost:

upstream *** {
    server 127.0.0.1:3000;
}

server {
    listen 80;
    server_name ***;
    root /var/www/***/public;
    index index.html;

    location / {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (-f $request_filename/index.html) {
            rewrite (.*) $1/index.html break;
        }

        if (-f $request_filename.html) {
            rewrite (.*) $1.html break;
        }

        if (!-f $request_filename) {
            proxy_pass http://***;
            break;
        }
    }
}

I've already tried /etc/init.d/thin stop then starting it again a few times, to no avail.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蝶舞 2024-12-08 18:27:15

我在 Production.log 中遇到了这个问题:

Started GET "/assets/bg-linen-light.png" for ***** at 2011-08-28 11:04:42 +0400
Served asset /bg-linen-light.png - 304 Not Modified (102ms)

问题是浏览器在 x_sendfile_header 更改为应有的内容之前请求了该文件,所以看来Rails(和/或浏览器)在更改变量后绝对不会执行任何操作。

通过转到 rails 控制台 并输入 Rails.cache.clear 解决了该问题。之后硬刷新就解决了问题!

Started GET "/assets/bg-linen-light.png" for ***** at 2011-08-28 11:06:06 +0400
Served asset /bg-linen-light.png - 200 OK (4ms)

I came across this in production.log:

Started GET "/assets/bg-linen-light.png" for ***** at 2011-08-28 11:04:42 +0400
Served asset /bg-linen-light.png - 304 Not Modified (102ms)

The problem is that the browser had requested that file before x_sendfile_header was changed to what it should be, so it seems that Rails (and/or the browser) does absolutely nothing after changing the variable.

The issue was resolved by going to rails console, and typing Rails.cache.clear. A hard refresh after that solves the problem!

Started GET "/assets/bg-linen-light.png" for ***** at 2011-08-28 11:06:06 +0400
Served asset /bg-linen-light.png - 200 OK (4ms)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文