1. 程式人生 > WINDOWS開發 >historyApiFallback: true 對應的 nginx 配置

historyApiFallback: true 對應的 nginx 配置

這個設定用於自動將404響應跳轉到首頁入口檔案 index.html。

webpack 配置如下:

module.exports = {
  //...
  devServer: {
    historyApiFallback: true
  }
};

部署到伺服器上時 nginx 的配置如下:

server {
    listen  80;
    server_name  172.20.47.16;

    location / {
        root /usr/share/nginx/html/console;
        try_files $uri $uri/ /index.html;  # 加上這一行即可
    }

    access_log /var/log/nginx/console_access.log main;
    error_log /var/log/nginx/console_error.log;
}

參考:
https://webpack.js.org/configuration/dev-server/#devserverhistoryapifallback
https://www.cnblogs.com/zph666/p/11468883.html