Apache虛擬目錄配置及vue-cli反向代理的設(shè)置方法
配置需求來(lái)自于前后端分離。后臺(tái)由于使用PHP或者Java,但是前端使用vue,React這些框架時(shí)怎么和后端有效的數(shù)據(jù)通信。反向代理是個(gè)很好的選擇,雖然jsonp也可以,單并不好玩。
Apache配置虛擬目錄
-實(shí)際上線(xiàn)項(xiàng)目需要通過(guò)域名來(lái)訪(fǎng)問(wèn),比如http://www.xxx.com,但在本機(jī)上如何配置虛擬域名來(lái)訪(fǎng)問(wèn)本機(jī)的項(xiàng)目呢?
1.找到C:\Windows\System32\drivers\etc\hosts這個(gè)文件添加以下格式內(nèi)容
127.0.0.1 www.mytest.com //你的虛擬域名
2.配置Apache項(xiàng)目目錄
1.找到 \apache\conf\httpd.conf 這個(gè)文件,修改內(nèi)容
# Virtual hosts Include conf/extra/httpd-vhosts.conf (這行的注釋#去掉)
2.找到\apache\conf\extra\httpd-vhosts.conf這個(gè)文件配置項(xiàng)目目錄
<VirtualHost *:80> ##ServerAdmin webmaster@dummy-host.example.com DocumentRoot "C:/xampp/htdocs/mobileApp" ##你的后端項(xiàng)目目錄 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>
3.proxyTable代理配置,以vue-cli為例
proxyTable: { '/api': { target: 'http://www.mytest.com/api', changeOrigin: true, pathRewrite: { '^/api': '' } } },
這樣就可以實(shí)現(xiàn)跨域訪(fǎng)問(wèn)了。
示例:
$.ajax({ url: '/api/indexList.php', type: 'GET', success: function (data) { that.list = data.data; console.log(data); } })
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。