• webpack中的代理配置


    作用:

    1.解决开发环境跨域问题(不用再去配置nginx和host)

    2.如果你有单独的后端开发服务器API,并希望在同域名下发送API请求,那么代理某些URL会很有用

    下面介绍一下五种经常使用的场景

    使用场景一:

    请求到 /api/xxx 现在会被代理到请求 http://localhost:3000/api/xxx, 例如 /api/user 现在会被代理到请求 http://localhost:3000/api/user

    1. module.exports = {
    2. //...
    3. devServer: {
    4. proxy: {
    5. '/api': 'http://localhost:3000'
    6. }
    7. }
    8. };

    使用场景二

    多个前缀的路径,都用一个路径来代理,使用context属性

    1. module.exports = {
    2. //...
    3. devServer: {
    4. proxy: [{
    5. context: ['/auth', '/api'],
    6. target: 'http://localhost:3000',
    7. }]
    8. }
    9. };

    使用场景三

    将url中的 /api替换为空串:

    1. module.exports = {
    2. //...
    3. devServer: {
    4. proxy: {
    5. '/api': {
    6. target: 'http://localhost:3000',
    7. pathRewrite: {'^/api' : ''}
    8. }
    9. }
    10. }
    11. };

    请求到 /api/xxx 现在会被代理到请求 http://localhost:3000/xxx, 例如 /api/user 现在会被代理到请求 http://localhost:3000/user

    使用场景四:

    如果想要支持https,需要配置。默认情况下,不接受运行在 HTTPS(超文本传输安全协议) 上,且使用了无效证书的后端服务器。

    1. module.exports = {
    2. //...
    3. devServer: {
    4. proxy: {
    5. '/api': {
    6. target: 'https://other-server.example.com',
    7. secure: false//是否验证SSL Certs
    8. }
    9. }
    10. }
    11. };

    使用场景五:

    个人理解:如果想要请求静态html页面,绕过代理;对于api请求则保持代理

    有时你不想代理所有的请求。可以基于一个函数的返回值绕过代理。

    在函数中你可以访问请求体、响应体和代理选项。必须返回 false 或路径,来跳过代理请求。

    例如:对于浏览器请求,你想要提供一个 HTML 页面,但是对于 API 请求则保持代理。你可以这样做:

    1. module.exports = {
    2. //...
    3. devServer: {
    4. proxy: {
    5. '/api': {
    6. target: 'http://localhost:3000',
    7. bypass: function(req, res, proxyOptions) {
    8. if (req.headers.accept.indexOf('html') !== -1) {
    9. console.log('Skipping proxy for browser request.');
    10. return '/index.html';
    11. }
    12. }
    13. }
    14. }
    15. }
    16. };

    解决跨域原理

    上面的参数列表中有一个changeOrigin参数, 是一个布尔值, 设置为true, 本地就会虚拟一个服务器接收你的请求并代你发送该请求,

    1. module.exports = {
    2. //...
    3. devServer: {
    4. proxy: {
    5. '/api': {
    6. target: 'http://localhost:3000',
    7. changeOrigin: true,
    8. }
    9. }
    10. }
    11. };

    vue-cli中proxyTable配置接口地址代理示例

    个人理解:这个配置文件会生成一个代理服务器,用于连接前端请求,向后端api服务器发送请求

    修改 config/index.js

    1. module.exports = {
    2. dev: {
    3. // 静态资源文件夹
    4. assetsSubDirectory: 'static',
    5. // 发布路径
    6. assetsPublicPath: '/',
    7. // 代理配置表,在这里可以配置特定的请求代理到对应的API接口
    8. // 使用方法:https://vuejs-templates.github.io/webpack/proxy.html
    9. proxyTable: {
    10. // 例如将'localhost:8080/api/xxx'代理到'https://wangyaxing.cn/api/xxx'
    11. '/api': {
    12. target: 'https://wangyaxing.cn', // 接口的域名
    13. secure: false, // 如果是https接口,需要配置这个参数
    14. changeOrigin: true, // 如果接口跨域,需要进行这个参数配置
    15. },
    16. // 例如将'localhost:8080/img/xxx'代理到'https://cdn.wangyaxing.cn/xxx'
    17. '/img': {
    18. target: 'https://cdn.wangyaxing.cn', // 接口的域名
    19. secure: false, // 如果是https接口,需要配置这个参数
    20. changeOrigin: true, // 如果接口跨域,需要进行这个参数配置
    21. pathRewrite: {'^/img': ''} // pathRewrite 来重写地址,将前缀 '/api' 转为 '/'。
    22. }
    23. },
    24. // Various Dev Server settings
    25. //host配置访问代理服务器接受访问的ip地址的范围
    26. //localhost只可以在本地访问;如果是0.0.0.0,同一个网段都可以 //访问。
    27. //host: 'localhost', // can be overwritten by process.env.HOST
    28. //可以用process.env.PORT重写,如果接口被占用,会分配一个其他端口
    29. port: 4200, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
    30. }

    更多参数

    dev-server 使用了非常强大的 http-proxy-middleware , http-proxy-middleware 基于 http-proxy 实现的,可以查看 http-proxy 的源码和文档:https://github.com/nodejitsu/node-http-proxy

    1. target:要访问的后端接口的url字符串
    2. forward:要使用url模块解析的url字符串
    3. agent:要传递给http(s).request的对象(请参阅Node的https代理和http代理对象)
    4. ssl:要传递给https.createServer()的对象
    5. ws:true / false,是否代理websockets
    6. xfwd:true / false,添加x-forward标头
    7. secure:true / false,是否验证SSL Certs
    8. toProxy:true / false,传递绝对URL作为路径(对代理代理很有用)
    9. prependPath:true / false,默认值:true - 指定是否要将目标的路径添加到代理路径
    10. ignorePath:true / false,默认值:false - 指定是否要忽略传入请求的代理路径(注意:如果需要,您必须附加/手动)。
    11. localAddress:要为传出连接绑定的本地接口字符串
    12. changeOrigin:true / false,默认值:false - 将主机标头的原点更改为目标URL

  • 相关阅读:
    Python爬取网页源代码(自用)
    解决 Github port 443 : Timed out
    FSC商标门户网站重置密码操作指南
    Dubbo的前世今生
    一款开源、免费、现代化风格的WPF UI控件库 - ModernWpf
    基于HTML+CSS+JavaScript+Bootstarp响应式健身网站(web前端期末大作业)
    IT之路,从迷茫“愤青”到团队项目经理,他是如何一步步走出来的?
    第四章 文件管理 七、文件共享
    设计模式-单例模式
    SpringBootCMS漏洞复现分析
  • 原文地址:https://blog.csdn.net/qiuyushuofeng/article/details/125467203