jeecgboot微服务配置之nginx 之 proxy_pass 后缀配置

1.切换至微服务开发后,jeecgboot后端的访问路径变成了http://10.182.16.104:7001

此时,发布到服务器上若不在nginx中配置跨域则会出现跨域问题。那么nginx中怎么配置呢?

首先,在前端项目中,我们可以看到路径src-utils-request.js中定义了baseurl为jeecg-boot

let apiBaseUrl = window._CONFIG['domianURL'] || "/jeecg-boot";
//console.log("apiBaseUrl= ",apiBaseUrl)
// 创建 axios 实例
const service = axios.create({
  //baseURL: '/jeecg-boot',
  baseURL: '/jeecg-boot', // api apiBaseUrl
  timeout: 9000 // 请求超时时间
})

在开发环境中,我们可以在vue.config.js中进行跨域处理

  devServer: {
    port: 3000,
    proxy: {
     '/jeecg-boot': {
        target: 'http://10.182.21.11:7001',
        ws: false,
        changeOrigin: true
      }
    }
  },

 但是到了生产环境,并没有vue.config.js,这时候我们需要在nginx中conf文件下的nginx.conf中处理。具体代码如下:

server {
        listen       90;
        server_name  localhost;  

        location /jeecgboot/ {
            proxy_pass http://10.182.21.11:7001/;
            add_header 'Access-Control-Allow-Origin' '*'; 
            add_header 'Access-Control-Allow-Credentials' 'true'; 
        }  

        location / {   
             root   html;
            index  index.html index.htm;    
        }  
      
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        } 
    } 

值得注意的是 两个标红点的/,一定要加,不加的话就会访问的是http://10.182.21.11:7001/jeecgboot而不是http://10.182.21.11:7001,那么就会导致报错。

 

posted @ 2023-02-24 14:19  许佳挺  阅读(508)  评论(0编辑  收藏  举报