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

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

nginx nginx.conf

server {
  listen 80 default_server;
  server_name /var/www/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;
  }
}

apache 修改httpd.conf

Invalid command ‘RewriteEngine’, perhaps misspelled or defined by a module not included in the server configuration
如果出现上面的错误,找到 LoadModule rewrite_module 这行, 取消掉前面的注释
https://stackoverflow.com/questions/10144634/htaccess-invalid-command-rewriteengine-perhaps-misspelled-or-defined-by-a-m

<VirtualHost *:80>
  ServerName localhost
  DocumentRoot D:\\apache\\Apache24\\htdocs\\build

  <Directory "D:\\apache\\Apache24\\htdocs\\build">
    RewriteEngine on
    # Don't rewrite files or directories
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    # Rewrite everything else to index.html to allow html5 state links
    RewriteRule ^ index.html [L]
  </Directory>
</VirtualHost>

原文链接

https://www.cnblogs.com/ajanuw/p/9827402.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
react 打包后刷新404的问题是因为在react应用中使用了前端路由,并且在部署后刷新页面时,服务器并未正确处理前端路由的路径。 通常,在开发过程中,我们可以使用React Router等前端路由库来管理应用程序的导航和页面切换。这些库会根据URL的路径来渲染相应的页面组件。而在开发环境中,由于使用了开发服务器,这些路由会自动处理并响应正确的页面。 但是,在生产环境中,我们需要将React应用打包成静态资源文件,并将其部署到服务器上。当用户在浏览器中访问应用时,他们可能会直接在浏览器地址栏中输入URL路径来访问特定的页面。但是,服务器默认情况下只会返回根路径的资源文件,对于其他路径会返回404页面。 为了解决这个问题,我们需要配置服务器来处理这些前端路由的路径。具体来说,我们需要确保服务器在收到请求后,返回React应用的主HTML文件,无论请求的路径是什么。这样,React应用就能在正确的URL路径下响应相应的页面。 对于使用Node.js的服务器,我们可以使用`express`或`koa`等框架来进行配置。对于Nginx服务器,我们可以使用`rewrite`指令来进行配置。 需要注意的是,具体的配置方法和步骤可能会根据使用的服务器和项目的不同而有所差异。因此,在遇到这个问题时,我们需要参考相关服务器和项目的文档或社区中的解决方案,并根据实际情况进行配置。 综上所述,处理React打包刷新404的问题需要配置服务器以正确处理前端路由的路径,确保服务器返回主HTML文件,并让React应用能够根据URL路径正确渲染相应的页面

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值