vue开发环境、生产环境nginx配置代理转发跨域

后端提供的请求接口完整地址示例:https://www.baidu.com/list

一、开发环境

vue.config.js中

proxy: {
  //配置跨域
  '/api': {
    target: "https://www.baidu.com",
    changOrigin: true,
    pathRewrite: {
      '/api': ''
    }
  }
}
注意:
1. axios请求的完整URL:/api/list 
2. 浏览器network请求地址Request URL一般类似为:http://localhost:8080/api/list

示例:

return request({
  url: 'api/list',//此处api也可写到axios的baseURL中;
  method: 'get',
  params: params
})

二、生产环境

1. 访问路径为根域名:https://abc.com/

本地:

1. router: base: process.env.NODE_ENV=='development' ? '/' : '/',

2. vue.config.js: publicPath: process.env.NODE_ENV=='development' ? '/' : '/',

3. axios baseURL: process.env.NODE_ENV == 'development' ? '/api' : '/api',

nginx:

方法1:

location ^~ /api/ {

   proxy_pass https://www.baidu.com/;  # 转发地址

}

注意:proxy_pass https://www.baidu.com/;    后面要加 /  表示绝对路径;

方法2:

location ^~ /api/ {

    rewrite  ^/api/(.*)$  /$1  break;  # 重写路径将  api 替换为空

    proxy_pass https://www.baidu.com/;后面 / 可加可不加;

}

解释:在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径(/api)代理走;如果没有/,则会把匹配的路径也给代理走。


2. 访问路径不是根域名:https://abc.com/demo/  且不做SLB

本地:

1. router: base: process.env.NODE_ENV=='development' ? '/' : '/demo/',

2. vue.config.js: publicPath: process.env.NODE_ENV=='development' ? '/' : '/',

3. axios baseURL: process.env.NODE_ENV == 'development' ? '/api' : '/api',

nginx:

方法1:

location ^~ /api/ {

   proxy_pass https://www.baidu.com/;  # 转发地址

}

注意:proxy_pass https://www.baidu.com/; 后面要加 /  表示绝对路径;

方法2:

location ^~ /api/ {

    rewrite  ^/api/(.*)$  /$1  break;  # 重写路径将  api 替换为空

    proxy_pass https://www.baidu.com/;后面 / 可加可不加;

}


 3. 访问路径不是根域名:https://abc.com/demo/  要做SLB,SLB匹配路径是:https://abc.com/demo/ 

本地:

1. router: base: process.env.NODE_ENV=='development' ? '/' : '/demo/',

2. vue.config.js: publicPath: process.env.NODE_ENV=='development' ? '/' : '/demo/',

3vue.config.js: outputDir: 'dist/demo',  //否则找不到静态资源

4. axios baseURL: process.env.NODE_ENV == 'development' ? '/api' : '/demo/api',   //否则nginx不能转发请求的接口

nginx:

基础配置:

location /demo {

    root   /mnt/project/h5-cbi360-net/dist;//指定根路径

   index  index.html index.html;

   try_files $uri $uri/ /demo/index.html;//history路由配置,此处的 /demo 必须写在此处,不能写到上面root后面

}

方法1:

location ^~ /demo/api/ {

   proxy_pass https://www.baidu.com/;  # 转发地址

}

注意:proxy_pass https://www.baidu.com/; 后面要加 /  表示绝对路径;

方法2:

location ^~ /demo/api/ {

    rewrite  ^/api/(.*)$  /$1  break;  # 重写路径将  /demo/api 替换为空

    proxy_pass https://www.baidu.com/;后面 / 可加可不加;

}


解释: 具体的配置与本地项目有关系,与nginx配置有关系,与SLB配置有关系,与访问路径是否是根域有关系。


三、 带 / 和不带 / 的区别

URL 参数原则

  1. URL 必须以 http 或 https 开头;
  2. URL 中可以携带变量;
  3. URL 中是否带 URI ,会直接影响发往上游请求的 URL ; 

接下来让我们来看看两种常见的 URL 用法:

  1. proxy_pass http://192.168.100.33:8081
  2. proxy_pass http://192.168.100.33:8081/

这两种用法的区别就是带 / 和不带 / ,在配置代理时它们的区别可大了:

  • 不带 / 意味着 Nginx 不会修改用户 URL ,而是直接透传给上游的应用服务器;可以理解为相对路径;
  • 带 / 意味着 Nginx 会修改用户 URL ,修改方法是将 location 后的 URL 从用户 URL 中删除;可以理解为绝对路径;

不带 / 的用法:

location /bbs/{
  proxy_pass http://127.0.0.1:8080;
}

分析: 

  1. 用户请求 URL : /bbs/abc/test.html
  2. 请求到达 Nginx 的 URL : /bbs/abc/test.html
  3. 请求到达上游应用服务器的 URL : /bbs/abc/test.html

带 / 的用法:

location /bbs/{
  proxy_pass http://127.0.0.1:8080/;
}

分析:

  1. 用户请求 URL : /bbs/abc/test.html
  2. 请求到达 Nginx 的 URL : /bbs/abc/test.html
  3. 请求到达上游应用服务器的 URL : /abc/test.html
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Vue前端与Spring Boot后端的跨域通信,可以使用Nginx作为代理服务器来解决跨域问题。以下是一个简单的Nginx代理配置示例: 首先,确保你已经正确安装和配置Nginx。然后,在Nginx配置文件中添加以下内容: ``` http { server { listen 80; server_name example.com; # 静态文件目录 location / { root /path/to/frontend; try_files $uri $uri/ /index.html; } # API代理 location /api/ { proxy_pass http://backend_server; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } # 后端服务器配置 upstream backend_server { server backend_server_ip:backend_server_port; } } ``` 在这个示例中,假设前端文件存放在`/path/to/frontend`目录下,后端服务器的IP地址为`backend_server_ip`,端口为`backend_server_port`。 配置中的`location /`指定了前端静态文件的目录,并使用`try_files`指令将所有请求都重定向到`index.html`,以支持前端路由。 配置中的`location /api/`指定了后端API的代理,并通过`proxy_pass`将请求转发到后端服务器。`proxy_set_header`指令用于设置请求头信息,以便后端服务器能够获取到正确的客户端信息。 请根据你的实际情况修改示例中的路径、后端服务器地址和端口,并将上述配置添加到Nginx配置文件中。重启Nginx后,Vue前端和Spring Boot后端之间的跨域通信应该可以正常工作了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值