React-router typed-url 并在 tomcat、elastic beanstalk 上刷新



关于这个主题有很多问题,即在生产中使用 react 时键入的 URL 和刷新不起作用,但我现在尝试了很长时间,由于某种原因它不起作用。

我正在使用带有 Spring boot 和 tomcat 的 React 。我正在使用浏览器路由器进行路由。我在 aws 弹性豆茎上部署应用程序。部署时,我执行以下操作: 运行npm run build然后将构建文件夹中生成的文件复制到 webContent 文件夹中,然后将项目导出为 war 文件并将其部署到 AWS。

据我了解,主要的解决方案是为所有请求服务器索引.html文件,我尝试了以下方法: - 将以下内容添加到/etc/httpd/conf/httpd.conf

<VirtualHost *:80>
DocumentRoot /var/lib/tomcat8/webapps/ROOT/
ServerName domain.elasticbeanstalk.com
AliasMatch ^/(.*)$ /var/lib/tomcat8/webapps/ROOT/index.html
<Directory "/var/lib/tomcat8/webapps/ROOT">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine On  
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^/(.*)$ - [L]
RewriteRule ^/(.*)$ /index.html [L]
</Directory>
</VirtualHost>

从我读到的问题中,建议使用/var/www/html,但我不明白为什么。我试图通过将ROOT中的所有内容复制到它来使用它,但也没有工作。

我尝试了对配置文件中的代码进行许多变体。

使用配置,我收到错误SyntaxError: expected expression, got '<'. 在火狐上,我得到了The script from “http://awsdomain.com/static/js/2.7322c0fb.chunk.js” was loaded even though its MIME type (“”) is not a valid JavaScript MIME type.

在 chrome 上,我得到同样的错误,但使用 css 文件而不是 js。

在这一点上,我假设 index.html 的服务正在工作,但还有另一个问题,即服务器试图解析一些 js 或 css 文件,如 html 或文本,这些文件不起作用。无论如何,我不知道如何告诉浏览器以正确的方式解析它们,为什么它以正常方式解析它们而没有错误(不解决路由问题)? 我尝试将类型属性添加到索引中的链接和脚本标签.html但没有奏效。

在 aws 实例上的配置文件中,我特别厌倦了很多东西,但很难将它们全部放在这里。我希望从评论中找出问题的根源,然后我可以提供任何缺失的信息。

我的问题终于解决了。 以下是我对httpd.conf的最后补充

<VirtualHost *:80>
DocumentRoot /var/lib/tomcat8/webapps/ROOT/
ServerName domain.elasticbeanstalk.com
<Directory /var/lib/tomcat8/webapps/ROOT>
Options FollowSymLinks MultiViews
AllowOverride All
Require all granted
DirectoryIndex index.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
</Directory>
</VirtualHost>

此外,很烦人的是,默认情况下不包含 DirectoryIndex 需要的dir_module。要包含它,请添加LoadModule dir_module modules/mod_dir.so

最新更新