react使用BrowserRouter打包后,刷新页面出现404

今天在部署React打包后的项目, 把项目代码放到nginx目录后。 发现导航页面访问404, 增加以下内容即可解决

 

server {
    listen 80 default_server;
    server_name example.com;

    root /var/www/example.com;
    index index.html index.htm;      

    location ~* \.(?:manifest|appcache|html?|xml|json)$ {
      expires -1;
      # access_log logs/static.log; # I don't usually include a static log
    }

    location ~* \.(?:css|js)$ {
      try_files $uri =404;
      expires 1y;
      access_log off;
      add_header Cache-Control "public";
    }

    # Any route containing a file extension (e.g. /devicesfile.js)
    location ~ ^.+\..+$ {
      try_files $uri =404;
    }

    # Any route that doesn't have a file extension (e.g. /devices)
    location / {
        try_files $uri $uri/ /index.html;
    }
}

其他WEB服务器参考以下

https://gkedge.gitbooks.io/react-router-in-the-real/content/nginx.html

发表评论