2012-12-13 29 views
0

我目前在rails中构建多域cms。由于这个内容是相同的,直到下一次更改,我想通过静态文件进行缓存。nginx - 有条件地提供文件而无需重定向

公共目录中foo.com和baz.com的(/和/自卫队在两种情况下)一些缓存的网页:

public/ 
    assets/ 
     cms.css 
    sites/ 
     foo.com/ 
     assets/ 
      screen-some-hash.min.css 
     index.html 
     asdf/ 
      index.html 
     baz.com/ 
     assets/ 
      screen-some-hash.min.css 
     index.html 
     asdf/ 
      index.html 

我想要做的是以下几点:

重定向www到非www(作品)

如果请求包含子域(cms,admin,whatever): 如果路径包含/资产在公共/资产中提供文件并将过期内容设置为30d左右。这里没有问题,因为/ assets = public/assets和public /是乘客根。 其他一切:通过导轨处理,无需特殊缓存或任何需要的东西。

对于所有其他请求(意思是没有子域): 如果路径包含/ assets在public/sites/$ host $ request_uri中提供文件并将过期内容设置为30d左右。其他一切:检查公共/网站/ $主机$ request_uri或回落到rails应用程序。

我从来没有与除www /非www重定向之外的nginx条件一起工作,也不知道我必须为上述条件做些什么。如果可能的话,我不想使用重定向缓存的内容(即重定向到/sites/foo.com/asdf),而是希望让nginx在去http://foo.com/asdf时直接提供此文件。

此外:我不想硬编码主机名,因为我想处理未知数量的域。我也不想为此使用多个单独的rails应用程序。

回答

0

子域,这应该做的伎俩:

server { 
    server_name ~^(?<subdomain>.+)\.example\.com$; 
    access_log /var/log/nginx/$subdomain/access.log; 
    location /assets { 
     expires max; 
    } 
    location/{ 
     proxy_pass http://your_rails_app; 
    } 
} 

不是真的肯定是我用Ruby只有经验Apps是Gitlab,这我跑这样的proxy_pass设置。我希望这至少有一点帮助。

server { 
    server_name example.com; 

    location /assets { 
     root /public/sites/$hostname/$request_uri; 
     expires max; 
    } 
} 

您必须添加自己的设置并稍微玩一下,因为我现在没有机会真正测试它。但它应该告诉你方式。

1

得到了一些有效的东西,不是100%,而是现在足够好。

server { 
    listen 80; 
    server_name *IP*; 

    if ($host ~* www\.(.*)) { 
    set $host_without_www $1; 
    rewrite ^(.*)$ http://$host_without_www$1 permanent; 
    } 

    location ~ ^/(assets)/ { 
    try_files /sites/$host$uri $uri @passenger; 

    root /home/cms/app/current/public; 
    gzip_static on; 
    expires max; 
    add_header Cache-Control public; 
    } 

    location/{ 
    try_files /sites/$host$uri/index.html /sites/$host$uri $uri @passenger; 
    root /home/cms/app/current/public; 
    } 

    location @passenger { 
    access_log /home/cms/app/shared/log/access.log; 
    error_log /home/cms/app/shared/log/error.log; 
    root /home/cms/app/current/public; 
    passenger_enabled on; 
    } 
}