我已经使用 nginx 和 Elastic Beanstalk 建立了一个 Express 和 React 网站。浏览网站时它工作正常,但如果我刷新主路线以外的路线,或尝试输入主路线以外的特定路线,则会导致 404。
我找到了许多不同的解决方案来更改 nginx.config
,并且认为我已经走了一步。这是我目前的nginx.config
:
server {
listen 80;
server_name leaftherapyeb-env.eba-xyc63u32.eu-west-2.elasticbeanstalk.com www.leaftherapyeb-env.eba-xyc63u32.eu-west-2.elasticbeanstalk.com;
root /var/www/leaftherapyeb-env.eba-xyc63u32.eu-west-2.elasticbeanstalk.com;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
我已经看到很多建议使用 root /usr/share/nginx/html;
的解决方案,但是当我使用它而不是每个页面上方的根链接到 nginx index.html 时。所以我改为尝试将 root 作为域的路径,我认为我错了,但不知道如何计算路径,目前在包括主路由在内的每条路由上都会导致 500 个内部错误。
非常感谢任何帮助,谢谢。
答案 0 :(得分:1)
我认为你的 nginx 配置有错误的语法。在下面的 nginx.conf 中,/ 位置指向文件夹 /var/www/html
中的 index.htmlserver {
listen 80;
server_name leaftherapyeb-env.eba-xyc63u32.eu-west-2.elasticbeanstalk.com www.leaftherapyeb-env.eba-xyc63u32.eu-west-2.elasticbeanstalk.com;
location / {
root /var/www/html;
index index.html
try_files $uri $uri/ /index.html;
}
}
您需要手动将 client/build 文件夹的所有内容复制到 /var/www/html - 或者您可以只创建一个符号链接(IMO 不是最好的解决方案,但它应该可以工作)
ln -s client/build /var/www/html
如果要使用符号链接,请确保在创建符号链接之前删除文件夹,否则会出现错误