vue-cli里的proxyTable怎么配置才能访问nginx上运行的web服务?

大致说下我遇到的问题:
(1)http://test.yang.com,运行在本地的网站,nginx服务器,后端用php实现,监听的是本地80端口,hosts文件中将域名关联到127.0.0.1。域名下http://test.yang.com/api/radi... 是可以访问到的接口,打开网页是可以访问到的。

https://img1.mukewang.com/5c8f4c280001682908000206.jpg

(2)http://localhost:8080,通过vue-cli搭建的一个node web服务
(3)我在/config/index.js中配置了下proxyTable:

  dev: {
    env: require('./dev.env'),
    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
        '/api': {
            target: 'http://test.yang.com',
            changeOrigion: true,
            pathRewrite: {
                '^/api': '/api' // 这里不需要重写,这里写错了
            }
        }
    },

我尝试了下面的几种配置nginx都给返回了404:

    proxyTable: {
        '/api': {
            target: 'http://test.yang.com',
            changeOrigion: true,
        }
    },
    proxyTable: {
        '/api': {
            target: 'http://test.yang.com',
            changeOrigion: true,
            pathRewrite: {
                '^/api': ''
            }
        }
    },
    proxyTable: {
        '/': {
            target: 'http://test.yang.com',
            changeOrigion: true,
            pathRewrite: {
                '^/api': '/api'
            }
        }
    },
    proxyTable: {
        '/': {
            target: 'http://test.yang.com/api',
            changeOrigion: true,
        }
    },

然后重启node服务,在页面中用vue-resource发出请求:

    this.$http.get('/api/radio.php')
      .then(function (res) {
        console.log(res);
      }, function(res) {
        console.log(res)
      })

结果发现出现nginx返回了404错误:

https://img1.mukewang.com/5c8f4c2a0001576508000278.jpg
(4)我的nginx的test.yang.com配置如下:

https://img3.mukewang.com/5c8f4c2c0001592d07780614.jpg

我看了segmentfault上的一些关于proxyTable的文章,但是仍然不知道该怎么解决这个问题,有哪位遇到过和我类似的问题,请指教一下呗,谢谢。

PS:我还想问下,我如何才能知道我发起请求时这个proxyTable到底代理目标服务器的哪个接口地址?比如有没有个工具或者日志能够打印下。

精慕HU
浏览 1141回答 4
4回答

红颜莎娜

你可以看看你nginx里面的日志记录,看看有没有请求,请求到哪儿去了
打开App,查看更多内容
随时随地看视频慕课网APP