apache 配置动静分离,允许跨域, 并在反向代理的情况下维持默认主页

修改httpd.conf

<VirtualHost *:80>
        ServerName app.taotianyue.com
        DocumentRoot /data/web/guanwang_app/static_src
        ErrorLog         /data/web/guanwang_app/apache_error.log
        ServerAdmin 346126185@.qq.com
	Header set Access-Control-Allow-Origin *
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyPassMatch /*.jpg !
        ProxyPassMatch /*.mp4 !
        ProxyPassMatch /*.css !
        ProxyPassMatch /*.gif !
        ProxyPassMatch /*.png !
        ProxyPassMatch /*.js !
        ProxyPassMatch /*.html$ !
        ProxyPass /test.html !
        ProxyPass /index.html !
        ProxyPassMatch  ^/$ !
        ProxyPass       /   http://127.0.0.1:8123/
        ProxyPassReverse /  http://127.0.0.1:8123/
        <Directory "/data/web/guanwang_app/static_src">
                Options -Indexes
                AllowOverride None
                Order allow,deny
                Allow from all
                DirectoryIndex index.html
        </Directory>
</VirtualHost>


说明

<VirtualHost *:80>
        ServerName app.gaotianyue.com  #域名
        DocumentRoot /data/web/guanwang_app/static_src #静态资源目录

        ErrorLog         /data/web/guanwang_app/apache_error.log
        ServerAdmin 346126185@qq.com

        Header set Access-Control-Allow-Origin * #允许跨域

        ProxyRequests Off
        ProxyPreserveHost On
        ProxyPassMatch /*.jpg !
        ProxyPassMatch /*.mp4 !
        ProxyPassMatch /*.css !
        ProxyPassMatch /*.gif !
        ProxyPassMatch /*.png !
        ProxyPassMatch /*.js !
        ProxyPassMatch /*.html$ !   #ProxyPassMatch 允许通配符
        ProxyPass /test.html !            #ProxyPass不允许通配符  

        ProxyPassMatch  ^/$ !  #   仅仅匹配 / ,而不匹配任何字符,这样使得访问域名绕过反向代理而使默认主页的设置不失效
        ProxyPass       /   http://127.0.0.1:8123/
        ProxyPassReverse /  http://127.0.0.1:8123/
        <Directory "/data/web/guanwang_app/static_src">
                Options -Indexes
                AllowOverride None
                Order allow,deny
                Allow from all
                DirectoryIndex index.html  默认主页
        </Directory>
</VirtualHost>

为了更直观些,我截了个图



现在有种情况要对 /mg/文件夹下的html,css等资源也同样代理,但是其他的html,css等不代理,则么做?

添加

ProxyPassMatch  /mg/  http://127.0.0.1:8123/  

即可

<VirtualHost *:80>
        ServerName app.sincetimes.com
        DocumentRoot /data/web/guanwang_app/static_src
        ErrorLog         /data/web/guanwang_app/apache_error.log
        Header set Access-Control-Allow-Origin *
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyPassMatch  /mg/  http://127.0.0.1:8123/
        ProxyPassMatch /*.jpg !
        ProxyPassMatch /*.mp4 !
        ProxyPassMatch /*.css !
        ProxyPassMatch /*.gif !
        ProxyPassMatch /*.png !
        ProxyPassMatch /*.js !
        ProxyPassMatch /*.html$ !
        ProxyPass /test.html !
        ProxyPass /index.html !
        ProxyPassMatch  ^/$ !
        ProxyPass       /   http://127.0.0.1:8123/
        ProxyPassReverse /  http://127.0.0.1:8123/
        <Directory "/">
                Options -Indexes
                AllowOverride None
                Order allow,deny
                Allow from all
                Header set Access-Control-Allow-Origin *
                DirectoryIndex index.html
        </Directory>
</VirtualHost>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值