Apache整合Tomcat实现静态资源与动态资源分离

来源:互联网 发布:windows 卡在欢迎界面 编辑:程序博客网 时间:2024/05/16 03:00

第一步:

修改本地host文件(C:\Windows\System32\drivers\etc\host),增加一个FQDN,用来访问静态资源。如:

127.0.0.1 static.topsuapp.com


第二步:

把项目的静态资源复制到Apache的某个目录下(如:${SRVROOT}/htdocs/topsuapp。${SRVROOT}是apache的安装目录)


第三步:

自己写一个conf文件,名字就叫"topsu-app.conf",内容如下。然后在httpd.conf里引用这个文件。 (Include conf/test/topsu-app.conf)

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. LoadModule headers_module modules/mod_headers.so  
  2.   
  3. <VirtualHost *:80>  
  4.     Header set Access-Control-Allow-Origin http://topsuapp.com:9001  
  5.     ServerName static.topsuapp.com  
  6.     DocumentRoot "${SRVROOT}/htdocs/xwanapp"  
  7. </VirtualHost>  
说明:

1)LoadModule, 要先加载apache的headers_module

2)Header 这行很重要,声明哪个url可以跨域访问这些静态资源。(也可以把后面的url改成 “*” ,即 “Header set Access-Control-Allow-Origin *”,表示所有人都能访问这些静态资源,但这样好像很不安全。)

3)ServerName 指出用哪个url来访问这些静态资源。这样用了第一步创建的url "static.topsuapp.com"

4)DocumentRoot 指出放静态资源的位置


第四步:

在项目文件中,比如index.jsp,用下面的方法引用静态资源

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <head>  
  2. <meta charset="UTF-8">  
  3. <link href="http://static.topsuapp.com/dojo_lib/gridx/resources/claro/Gridx.css" rel="stylesheet" type="text/css"/>  
  4. <link href="http://static.topsuapp.com/dojo_lib/dijit/themes/claro/claro.css" rel="stylesheet" type="text/css"/>  
  5. <link href="http://static.topsuapp.com/css/app.css" rel="stylesheet" type="text/css"/>  

最后,把项目部署到tomcat。启动tomcat和apache,访问项目url http://topsuapp.com:9001,发现所有静态资源都可以访问。


补充:

关于Access-Control-Allow-Origin,后面要么匹配一条url (如:http://topsuapp.com:9001),要么匹配所有(如:*)。暂时没发现怎样才能匹配额定几条url。

0 0
原创粉丝点击