我想看看如何configurationNginx和Varnish。
我在两个IP上运行多个PHP站点和Rack-Sinatra站点作为虚拟主机。 我想阻止Nginx提供静态文件,因为我注意到一些延迟。
编辑 :我已经更改为Nginx,但提供的答案很容易移植到Nginx。
Apache仍然会提供静态文件,但只会提供一次 。 也许最简单的方法是configuration清漆来侦听端口80的所有IP地址,并configurationApache在localhost:8000上侦听。 然后你configuration清漆把所有收到的请求转发到localhost:8000以供Apache处理。
我会去清漆configuration:
# have varnish listen on port 80 (all addresses) varnishd -a 0.0.0.0:80
现在在你的vcl文件中:
backend default { .host = "localhost"; .port = "8000"; } sub vcl_recv { # add a unique header containing the client IP address set req.http.X-Orig-Forwarded-For = client.ip; # we're only handling static content for now so remove any # Cookie that was sent in the request as that makes caching # impossible. if (req.url ~ "\.(jpe?g|png|gif|ico|js|css)(\?.*|)$") { unset req.http.cookie; } } vcl_fetch { # if the backend server adds a cookie to all responses, # remove it from static content so that it can be cached. if (req.url ~ "\.(jpe?g|png|gif|ico|js|css)(\?.*|)$") { unset obj.http.set-cookie; } }
现在在您的Apache httpd.confconfiguration中,您希望Apache监听localhost:8000并在相同的地址上定义您的虚拟主机:端口
Listen 127.0.0.1:8000 NameVirtualHost 127.0.0.1:8000
对于每个网站,创build一个<VirtualHost>节。 在该节中,您需要告诉Apache在所有静态内容上设置Expires和caching控制标题,以便varnish知道要caching它。
<VirtualHost 127.0.0.1:8000> ServerName www.our-example-domain.com # Set expires and cache-control headers on static content and # tell caches that the static content is valid for two years ExpiresActive on <FilesMatch "\.(js|css|ico|gif|png|jpe?g)$"> ExpiresDefault "access plus 2 year" Header append Cache-Control "public" </FilesMatch> # ... the rest of your web site configuration ... </VirtualHost>
我希望这有帮助。
为了未来读者的利益,对于由rjk提供的VCL示例:
否则,发现。 🙂