nginx部署umi前端

less than 1 minute read

Step 1:

编译项目, 生成生产环境使用的前端文件

umi build

Step 2:

把编译好的文件放到nginx的服务路径下。 如果静态文件不在nginx的根路径下, 需要配置publicPath

配置方法为修改.umirc.js

export default {
    publicPath: "http://yourcdn/path/to/static/"
}

Step 3:

配置nginx, 并把所有url定向到index.html

location / {
    try_files $uri @fallback;
    autoindex on;
}
location @fallback {
    rewrite .* /index.html break;
}

apache config

<Directory /var/www/test>
    Allowoverride all
    Order allow,deny
    Allow from all
    Require all granted
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(.*) /index.html [NC,L]
</Directory>