[React]React实战(1)——Ant Design Pro项目history模式下相关配置

相关链接

AntDesign文档

Nginx 配置文件修改

完整配置

server {
    listen       80;
    server_name  localhost;
    location / { 
    proxy_pass http://localhost:8000;
    }
  location /active {
      try_files $uri $uri/ /healthy/index.html;
  }

  location /healthy/api {
       proxy_pass   http://localhost:8888/;
  }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

8000端口下nginx配置

server {
    listen  8000;
    server_name   localhost;
    location / {
        root   /usr/project/;
        try_files $uri $uri/ /index.html;  #配合react项目的history模式去除#时用到
        index index.html;
        client_max_body_size    8m;
    }
}

配置文件说明

  • 使用try_files方式的前提是服务器安装nginx的时候需要安装相应的模块,我是通过yarn源的方式安装的,包含了所有模块,所以可以直接使用
  • 80端口下根目录会指向8000端口(习惯把项目都放在8000端口下指向的project目录下),同时添加/active访问拦截指向healthy目录下index.html,同时添加/healthy/api是为了拦截接口访问
  • 8000端口下root指向的是/usr/project/,此目录放置所有前端项目文件夹,一些node服务端项目也可以放进去

Ant Design Pro项目中相关配置

修改config文件

项目目录下config/config.js

export default {
  plugins,
  proxy,
  block: {
    defaultGitUrl: 'https://github.com/ant-design/pro-blocks',
  },
  hash: false, // browser 模式下为false, hash 模式下为true
  history: 'browser', // 默认是 browser , hash
  base: '/healthy/', // browser 模式下为 /healthy/ , hash模式下为 /
  publicPath: '/healthy/', // 模式下为 /healthy/ , hash模式下为 ./
  targets: {
    ie: 11,
  },
  devtool: isAntDesignProPreview ? 'source-map' : false,
}

引用外部文件

pages/document.ejs中引用的外部文件

<link rel="shortcut icon" href="./favicon.ico">

接口调用配置

// config/config.js配置文件
global.api = '/healthy/api'
// 调用接口页面
let response = await axios.get(`${global.api}/dtea-service/user/result?examId=${this.questionId}`);

为了本地项目和发布至服务器打包文件保持一致,可以把项目中本地代理修改下

const proxy = {
  '/healthy/api': {
    target: 'http://192.136.12.211:8888',
    changeOrigin: true,
    pathRewrite: {
      '^/healthy/api': '',
    },
  },
};
}

作者:wayne1125
链接:https://www.jianshu.com/p/16de172d0d45
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。