const router=new Router({
mode: 'hash', //把history修改为hash
base: process.env.BASE_URL,
routes
})
// 跨域配置
module.exports = {
publicPath:'/', 这里加上这个
devServer: { //记住,别写错了devServer//设置本地默认端口 选填
port: 80,
proxy: { //设置代理,必须填
'/api': { //设置拦截器 拦截器格式 斜杠+拦截器名字,名字可以自己定
target: 'http://自己服务器的ip地址:8080', //代理的目标地址
changeOrigin: true, //是否设置同源,输入是的
pathRewrite: { //路径重写
'^/api': ' ' //选择忽略拦截器里面的内容
}
}
}
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
try_files $uri $uri/ /index.html last;
index index.html index.htm;
}
#如果你前项目中加了代理,那么就得加上这个
location ^~ /api/ { #注意这里得写对
proxy_pass http://自己服务器的地址:8080/; #后端服务的地址和端口号
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
我的问题:我一开始 写的是 这种,发现没,少了个’/’,然后这个符号是我噩梦的开始,搞了好几天才发现到
location ^~ /api {
proxy_pass http://自己服务器的地址:8080/; #后端服务的地址和端口号
}
//vue.config.js
module.exports = {
publicPath:'/',
devServer: {
port: 80,
proxy: {
'/api': { //
target: 'http://服务器地址:8080', //代理的目标地址 localhost
changeOrigin: true, //是否设置同源,输入是的
pathRewrite: { //路径重写
'^/api': ' ' //选择忽略拦截器里面的内容 (这里得主要下有的是 '^/api': '/' )
}
}
}
//utils/request.js
const service = axios.create({
timeout: 5000,
baseURL: '/api', //这里也得注意,看看有没有写成'/api/'
withCredentials: true
});
原文地址:https://blog.csdn.net/DonteLin/article/details/127602256
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_37050.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!