Ruby-on-rails 提供静态资产(如 .jpgs 等?)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/531189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:58:04  来源:igfitidea点击:

A route to serve static assets (like .jpgs, etc?)

ruby-on-railsjpegroutes

提问by Sniggerfardimungus

I've worked my way through a number of interesting routing problems - turning a request URL into a hash, etc., but just out of curiosity, is there a way to tell the routing system that you want anything that comes under a certain url subpath to be served literally - without going through a controller?

我已经解决了许多有趣的路由问题 - 将请求 URL 转换为哈希等,但出于好奇,有没有办法告诉路由系统您想要某个 URL 下的任何内容从字面上提供子路径 - 不通过控制器?

For instance, if I have /home/me/public_html/rails_proj/images/foo.jpg, and .../rails_proj/images/other/bar.jpg, can I insert a route that says "anything under images should just be served as an object of the default mime type?"

例如,如果我有 /home/me/public_html/rails_proj/images/foo.jpg 和 .../rails_proj/images/other/bar.jpg,我可以插入一条路线,说“图像下的任何内容都应该是作为默认 mime 类型的对象?”

Might be interesting.

可能很有趣。

回答by Nick Messick

If you put the "images" directory into the "public" folder of the Rails app (for example: /public/images/) then you shouldn't have any problems with MIME types unless your web server is configured wrongly.

如果您将“images”目录放入 Rails 应用程序的“public”文件夹中(例如:/public/images/),那么您应该不会遇到 MIME 类型的任何问题,除非您的 Web 服务器配置错误。

According to your examples, you want the images dir in the root of the app. I don't think there is a way though Rails to make those images visible, but if you really wanted to you could use mod_rewrite to make it work. Once again, it would be up to the web server to make sure the images had the correct MIME type.

根据您的示例,您希望图像目录位于应用程序的根目录中。我认为 Rails 没有办法让这些图像可见,但如果你真的想要,你可以使用 mod_rewrite 使其工作。再一次,由网络服务器来确保图像具有正确的 MIME 类型。

回答by Allyn

Things that are served out of the public directory will not go through Rails - they'll just be handled by your server (probably apache). The only reason why you would need to serve images through the rails system is if you wanted some kind of control on who could access them. Just put everything else in public and access ala: siteurl.whatever/images/*.jpg

从公共目录提供的内容不会通过 Rails - 它们只会由您的服务器(可能是 apache)处理。您需要通过 rails 系统提供图像的唯一原因是,如果您想对谁可以访问它们进行某种控制。只需将其他所有内容公开并访问 ala:siteurl.whatever/images/*.jpg

回答by ronaldevers

I typically use nginx as a frontend and Apache/Passenger as a backend. Ngingx proxies all Rails requests to Apache but handles all static content itself. Check out the examples on the English nginx wiki. Here is a small excerpt for nginx config:

我通常使用 nginx 作为前端,使用 Apache/Passenger 作为后端。Ngingx 将所有 Rails 请求代理到 Apache,但自己处理所有静态内容。查看英文 nginx wiki上的示例。这是 nginx 配置的一小段摘录:

server {
    listen 80;
    server_name www.domain.com;
    location ~* \.(jpg|jpeg|gif|png|ico|css|bmp|js)$ {
        root   /path/to/static/assets/dir;
    }
    location / {
        proxy_pass http://127.0.0.1:81;
    }
}

So have apache listen on port 81 to handle Rails requests proxied by nginx and let nginx deliver static content. Not only is nginx supposedly faster than Apache at delivering static content, but this also offloads your Rails application for every image, stylesheet, javascript or whatever other static content.

因此,让 apache 监听端口 81 以处理由 nginx 代理的 Rails 请求,并让 nginx 提供静态内容。据说 nginx 不仅在交付静态内容方面比 Apache 更快,而且还为每个图像、样式表、javascript 或任何其他静态内容卸载了 Rails 应用程序。

回答by Yang Dong

I think the simplest way solve this problem is just using image_path helper method, which provide you the path for the image you want to display in the view. For example, if you want to refer a logo.png under the /assets/images/logo.png, you can just use image_path('logo.png').

我认为解决这个问题的最简单方法就是使用 image_path 辅助方法,它为您提供要在视图中显示的图像的路径。例如,如果你想引用 /assets/images/logo.png 下的 logo.png,你可以使用 image_path('logo.png')。

回答by pantulis

Caveat: if your request URL matches a static resource WEBrick, mongrel or whatever will happily serve it. At any cost, you don't want this in production: if your traffic is high enough your app will be brought to its knees just because its mongrels will be busy serving static content.

警告:如果您的请求 URL 与静态资源 WEBrick、mongrel 或其他任何可以为其提供服务的静态资源匹配。无论如何,您都不希望在生产环境中这样做:如果您的流量足够高,那么您的应用程序就会瘫痪,因为它的杂种会忙于提供静态内容。

So make sure that your web server is properly configured for all kinds of static content as the previous commenters have pointed out.

因此,请确保您的 Web 服务器正确配置为所有类型的静态内容,正如前面的评论者所指出的那样。