Apache虚拟目录配置及vue-cli反向代理

配置需求来自于前后端分离。后台由于使用php或者java,但是前端使用vue,react这些框架时怎么和后端有效的数据通信。反向代理是个很好的选择,虽然jsonp也可以,单并不好玩。

Apache配置虚拟目录 
-实际上线项目需要通过域名来访问,比如http://www.xxx.com,但在本机上如何配置虚拟域名来访问本机的项目呢?

1.找到C:\Windows\System32\drivers\etc\hosts这个文件添加以下格式内容

127.0.0.1       www.mytest.com //你的虚拟域名
 
 
  • 1

2.配置Apache项目目录 
1.找到 \apache\conf\httpd.conf 这个文件,修改内容

# Virtual hosts
Include conf/extra/httpd-vhosts.conf (这行的注释#去掉)
 
 
  • 1
  • 2

2.找到\apache\conf\extra\httpd-vhosts.conf这个文件配置项目目录

<VirtualHost *:80>
    ##ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "C:/xampp/htdocs/mobileApp" ##你的后端项目目录
    ServerName www.mytest.com ##虚拟域名
    ##ServerAlias www.dummy-host.example.com
    ##ErrorLog "logs/dummy-host.example.com-error.log"
    ##CustomLog "logs/dummy-host.example.com-access.log" common
    <Directory "C:/xampp/htdocs/mobileApp"> 
        Options Indexes FollowSymLinks
        DirectoryIndex index.html index.php
        AllowOverride all
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

3.proxyTable代理配置,以vue-cli为例

   proxyTable: {
      '/api': {
        target: 'http://www.mytest.com/api',
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    },
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

这样就可以实现跨域访问了。 
示例:

   $.ajax({
                url: '/api/indexList.php',
                type: 'GET',
                success: function (data) {
                    that.list = data.data;
                    console.log(data);
                }
            })
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值