VUE2.0生产环境解决跨域问题
发表时间: 2021-03-12 16:04:04 | 浏览次数:
前面发过一遍VUE2.0开发环境解决跨域问题的文章,今天给大家分享一下生产环境如何解决跨域问题。主要是通过Nginx的反向代理,解决跨域的问题。
1.我的VUE项目是使用axios请求数据的,我的域名为:vue.lrfun.com;请求后端数据url为:vue.lrfun.com/api(使用nginx做反向代理)。
2.配置前端VUE项目:
server {
listen 80;
server_name vue.lrfun.com;
index index.html;
root /www/vue;
if ($http_host !~ "^vue.lrfun.com$") {
rewrite ^(.*) http://vue.lrfun.com$1 permanent;
}
location / {
index index.html;
try_files $uri $uri/ /index.html?$uri&$args;
}
#反向代理,/api这个目录指向8080端口的后端项目
location /api/ {
proxy_pass http://127.0.0.1:8080/;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 1h;
}
}
3.配置后端项目:
server {
listen 8080;
server_name 127.0.0.1;
index index.php;
root /www/php;
location / {
index index.php;
try_files $uri $uri/ /index.php?$uri&$args;
}
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
上一篇:VUE2.0开发环境解决跨域问题
下一篇:我的VUE2.0学习笔记