简体   繁体   English

静态内容来自子域

[英]Static content serve from subdomain

I have domain.localhost and assets.domain.localhost . 我有domain.localhostasset.domain.localhost Virtual host in htppd.conf is set correctly. htppd.conf中的虚拟主机已正确设置。

I have folder in root "public/" and there are other folders like images, scripts, styles . 我在根目录“ public /”中有文件夹,并且还有其他文件夹,例如图像,脚本,样式 I need serve content from domain.localhost and static images, scripts and styles server from assets.domain.localhost. 我需要从domain.localhost提供内容,并从asset.domain.localhost提供静态图像,脚本和样式服务器。

I have this code in htaccess: 我在htaccess中有以下代码:

RewriteEngine On
RewriteCond %{HTTP_HOST} !assets\.domain\.localhost
RewriteCond %{REQUEST_URI} \.(png|gif|ico|css|js)$ [NC]
RewriteRule ^(.*) http://assets.domain.localhost/$1 [L]

First question: Is it correct way to load assets separatly and decrease count of request from domain? 第一个问题:分别加载资产并减少来自域的请求数是正确的方法吗?

Second question: I checked it in firebug 第二个问题:我在萤火虫中检查了它

网络形象在这里

and everything was loaded from assets.domain.localhost, but css and js was loaded twice. 并且所有内容都从assets.domain.localhost加载,但是css和js加载了两次。 First time from domain.localhost with 302 code (just some byte but is it request) and than from assets.domain.localhost with 304 code. 第一次从具有302代码的domain.localhost(只是一个字节,但它是请求的),然后是从具有304代码的asset.domain.localhost。 What I doing wrong? 我做错了什么?

Update 更新资料

This workaround increase result in PageSpeed Insights, but decrease in Webpage performance test. 此解决方法增加了PageSpeed Insights的结果,但降低了网页性能测试。 In second one is longer response from server and assets are not cache anymore. 第二个是服务器的响应时间更长,资产不再缓存。 I have this one in htaccess for that: 我在htaccess中有这个功能:

Header set Cache-Control "max-age=604800, public, must-revalidate"
<IfModule deflate_module>
  <IfModule filter_module>
    AddOutputFilterByType DEFLATE application/rdf+xml application/rss+xml application/atom+xml image/svg+xml
    AddOutputFilterByType DEFLATE text/css text/javascript application/javascript application/x-javascript
  </IfModule>
</IfModule>

Before changes, it worked, not caching does't work. 在更改之前,它起作用了,而不是缓存不起作用。

Update2 更新2

Ok problem is with first request to domain.localhost , that is not cached, secon request to assets.domain.localhost is cached. 好的问题是第一次请求domain.localhost,assets.domain.localhost未缓存,塞康请求被高速缓存。 How to rid first unecessary request? 如何摆脱不必要的要求?

You have to make sure you change your HTML to contain the Asset-URLs with your assets.domain.localhost Domain. 您必须确保将HTML更改为包含带有Assets.domain.localhost域的Asset-URL。

If you leave your HTML as is, the Browser will request the files from domain.localhost, receive a "302 Found" from Apache (as defined in your .htaccess) and then request the files from assets.domain.localhost again. 如果您保留HTML原样,浏览器将向domain.localhost请求文件,从Apache(在您的.htaccess中定义)接收到“ 302 Found”,然后再次从asset.domain.localhost请求文件。

If it is not possible to change the Asset-Links in your HTML you can try something like mod_substitute to replace the links on-thy-fly. 如果无法在HTML中更改资产链接,则可以尝试使用mod_substitute之类的方法即时替换链接。 But i've never tried this in a real-world-scenario. 但是我从未在现实世界中尝试过这种方法。

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

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