简体   繁体   English

如何使用Nginx提供静态文件

[英]How to serve static file with nginx

I'm switch from Apache to Nginx, and with apache all static files (images, css, javascript) in skins folders were served by Apache directly. 我从Apache切换到Nginx,并且使用apache,皮肤文件夹中的所有静态文件(图像,css,javascript)都直接由Apache提供。

Example : 范例:

DocumentRoot    /zope/z_france_velo_tourisme/france-velo-tourisme/src
RewriteRule  ^/images-fvt/(.*) /fvt.commun/fvt/commun/skins/fvt_commun_images/images-fvt/$1 [L]

I read some docs about Nginx and Plone, and I didn't saw that. 我读了一些有关Nginx和Plone的文档,但我没有看到。 Here the example from Plone.org conf : https://github.com/plone/plone-org-nginx/blob/master/nginx.conf 这是来自Plone.org conf的示例: https : //github.com/plone/plone-org-nginx/blob/master/nginx.conf

So, my question is : is it still a good practice to serve static files with Apache/Nginx, and if yes, how to do it with Nginx ? 所以,我的问题是:用Apache / Nginx提供静态文件仍然是一种好习惯,如果是的话,如何用Nginx完成呢?

I've tried a configuration like this, but it didn't work, I get a 404 error : 我已经尝试过这样的配置,但是没有用,我收到404错误:

root /zope/z_france_velo_tourisme/france-velo-tourisme/src;
rewrite ^/images-fvt/(.*)$ /fvt.commun/fvt/commun/skins/fvt_commun_images/images-fvt/$1;

I've also tried this (to avoid the root directive) but got a 404 too : 我也尝试过这样做(避免使用root指令),但也得到了404:

rewrite ^/images-fvt/(.*)$ /zope/z_france_velo_tourisme/france-velo-tourisme/src/fvt.commun/fvt/commun/skins/fvt_commun_images/images-fvt/$1;

Thanks. 谢谢。

Let me focus on what I think is the core of your question: "is it still a good practice to serve static files with Apache/Nginx ... ?" 让我集中讨论我认为是您的问题核心的内容:“使用Apache / Nginx ...提供静态文件仍然是一种好习惯吗?”

Assuming that you're using a proxy caching mechanism, this is usually going to be a wasted optimization that just adds complexity to your configuration. 假设您使用的是代理缓存机制,这通常是浪费的优化,只会增加配置的复杂性。

Instead, first visit your site setup and turn on appropriate http caching. 相反,请首先访问您的站点设置并打开适当的http缓存。 Even the minimum settings will cause static resources to be served with headers calling for long expiration times. 即使是最小设置,也将导致静态资源与要求较长过期时间的标头一起使用。 Under normal circumstances, this will mean that your zope/plone instance only serves a static resource very infrequently. 在正常情况下,这将意味着您的zope / plone实例很少提供静态资源。

Either set up proxy caching inside Nginx or -- if you need serious performance -- set up Varnish. 在Nginx内设置代理缓存,或者-如果需要出色的性能-设置Varnish。 Or, use a caching CDN like Cloudflare. 或者,使用缓存CDN(例如Cloudflare)。

The benefits you'll get out of spending time implementing caching will likely vastly outweigh any benefits you'd see from setting up an alternative static file mechanism. 从花时间实现缓存中获得的好处可能会大大超过通过设置备用静态文件机制所看到的任何好处。

server {
    listen 433 ssl;
    server_name plone.org

    root /path/to/virtual/server/folder;
    location / {
        proxy_pass http://whatever;
    }
    location ~^ (images|css|js|anything)/ {

    }
}

something like this should work, if you have the assets in the right place. 如果您将资产放在正确的位置,则类似的操作应该会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM