Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

vue-cli3中使用nginx解决跨域遇到的问题,求解!

我的vue-cli3本地项目启动配置

    devServer: {
         open: process.platform === 'darwin',
         host: '127.0.0.1', // 允许外部ip访问
         port: 80, // 端口
         https: false, // 启用https
         overlay: {
                warnings: true,
                errors: true
         },
         disableHostCheck: true
    },

发送请求的统一配置是

    const HOSTNAME = window.location.hostname;

发送请求接口为(这里搞复杂了)

    checkUser(params){
      return axios.get(`http://${HOSTNAME}/veliky/user/checkUser`,  {params})
    },

windows10 的host文件配置了

   127.0.0.1 test.demo.xyz

在页面地址栏输入 test.demo.xyz就是触发 checkUser接口发送请求
http://test.demo.xyz/veliky/user/checkUser
但是其实际请求的地址应该是另外一个新地址。

在nginx中配置

server {
        listen 81;
        server_name test.demo.xyz;
        location /veliky {
            proxy_pass http://新地址/veliky;
            proxy_set_header Host $host;
            proxy_cookie_path /volzhski /;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Cookie $http_cookie;
        }
    }

但是这样报访问是报404, 接口的远程请求地址是Remote Address: 127.0.0.1:80是,nginx日志中没有收到请求。
请问是哪里有问题???谢谢了


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

image.png
nginx监听了81接口,81接口是什么,你自己定义的吗。http默认是80端口,81需要手动设置。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...