nginx的proxy_pass代理转发以及vue的proxy转发

近期在调整一个项目,将原有项目调整为两个项目

遇到的问题就是,在不动前段的情况下如何实现

比如,前端的接口是

http://www.xxx.xxx/equip/system/dddd

http://www.xxx.xxx/equip/aaaa

上面两个接口调整之前属于一个项目,调整之后要代理成这里

http://www.xxx.xxx/user/system/dddd

http://www.xxx.xxx/equip/aaaa

1,于是,可以通过如下配置即可实现,vue的配置如下:

  devServer: {

    progress: true,

    port: 80,

    proxy: {

      '/equip/AI/': {

        target: 'http://www.xxx.cn:8077/',

        changeOrigin: true,

        pathRewrite: { // 需要rewrite重写的, 如果在服务器端做了处理则可以不要这段

          '^/equip/AI/': ''

        }

      },

      '/equip/user': {

        target: 'http://192.168.0.200:9001/user',

        ws: false,

        changeOrigin: true,

pathRewrite:{

            '^/equip/user': '/'

        }

      },

      '/equip/system': {

        target: 'http://192.168.0.200:9001/user/system',

        ws: false,

        changeOrigin: true,

pathRewrite:{

            '^/equip/system': '/'

        }

      },   

      '/equip/': {

        target: 'http://192.168.0.200:9002/',

        ws: false,

        changeOrigin: true

      },

      '/static/': {

        target: 'http://192.168.0.200:9002/equip/',

        ws: false,

        changeOrigin: true

      }

    }

  },

2,nginx的配置就需要使用到proxy_pass,他有四种配置方式

我们以允许接口来作为例子 https://www.bugkong.com/equip/system/dddd

第一种:location /equip/ {

    proxy_pass https://www.bugkong.com/;

}

代理到URL:https://www.bugkong.com/system/dddd

第二种(相对于第一种,最后少一个 / ,也是我们常用的)

location /equip/ {

    proxy_pass https://www.bugkong.com;

}

代理到URL:https://www.bugkong.com/equip/system/dddd

第三种:location /equip/ {

    proxy_pass https://www.bugkong.com/aaa/;

}

代理到URL:https://www.bugkong.com/aaa/system/dddd

第四种(相对于第三种,最后少一个 / )

location /equip/ {

    proxy_pass https://127.0.0.1/aaa;

}

代理到URL:https://127.0.0.1/aaasystem/dddd

在我的项目中,nginx我做如下配置:

server {

        listen       80;

        server_name  192.168.0.200;

client_max_body_size 20M;

set $root_path 'D:/Program Files/nginx/html/equipment'; #你的路径

root $root_path;

location / {

            try_files $uri $uri/ @router;#需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404

            index  index.html index.htm;

        }

location @router {

            rewrite ^.*$ /index.html last;

        }

location /equip/user/ {

add_header Cache-Control no-cache;

# 把 /system 路径下的请求转发给真正的后端服务器

proxy_pass http://192.168.0.200:9001/user/;

# 把host头传过去,后端服务程序将收到your.domain.name, 否则收到的是192.168.0.222:8080

proxy_set_header real-ip $remote_addr;

proxy_set_header Host $http_host;

# 把cookie中的path部分从/api替换成/service

}

location /equip/system/ {

add_header Cache-Control no-cache;

# 把 /system 路径下的请求转发给真正的后端服务器

proxy_pass http://192.168.0.200:9001/user/system/;

# 把host头传过去,后端服务程序将收到your.domain.name, 否则收到的是192.168.0.222:8080

proxy_set_header real-ip $remote_addr;

proxy_set_header Host $http_host;

# 把cookie中的path部分从/api替换成/service

}

location /equip/ {

add_header Cache-Control no-cache;

# 把 /system 路径下的请求转发给真正的后端服务器

proxy_pass http://192.168.0.200:9002;

# 把host头传过去,后端服务程序将收到your.domain.name, 否则收到的是192.168.0.222:8080

proxy_set_header real-ip $remote_addr;

proxy_set_header Host $http_host;

# 把cookie中的path部分从/api替换成/service

}

}


爆款云服务器s6 2核4G 低至0.46/天,具体规则查看活动详情Blog Img