有很多关于这个话题的问题,当在生产环境中使用react时,键入的URL和刷新不起作用,但我现在已经尝试了很长时间,由于某些原因,它不起作用。
我将React与Spring boot和tomcat一起使用。我正在使用BrowserRouter进行路由。我将应用程序部署在aws elastic beanstalk上。在部署时,我执行以下操作:运行npm run build
,然后将build文件夹中的结果文件复制到webContent文件夹中,然后将项目导出为war文件并将其部署到aws。
据我所知,解决此问题的主要解决方案是为所有请求提供index.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或文本,这将不起作用。在任何情况下,我都不知道如何告诉浏览器以正确的方式解析它们,为什么它以正常的方式解析它们而没有错误(而没有解决路由问题)?我尝试将类型属性添加到index.html中的链接和脚本标记中,但没有成功。
在aws实例的配置文件中有很多东西我特别累,但是很难把它们都放在这里。我希望从评论中找到问题的根源,然后我可以给出任何遗漏的信息。
发布于 2019-04-19 04:43:44
我的问题终于解决了。下面是我最后添加到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
。
https://stackoverflow.com/questions/55714498
复制相似问题