修改Nginx配置文件,添加try_file配置如下,即可实现对 React browser router 的支持。
location / { root /var/www/mysite; try_files $uri /index.html; }
但是该方式也会存在缺点,只要/index.html
存在,服务端就不会响应404,即使客户端请求了实际不存在的JS/CSS/图片文件。
要使非HTML请求实际资源不存在时响应404,方法是:若请求的资源不是HTML,则放弃尝试后备文件。
location / { root /var/www/mysite; set $fallback_file /index.html; if ($http_accept !~ text/html) { set $fallback_file $uri; } try_files $uri $fallback_file; }
要使得try_files不影响index和/与autoindex,方法是:若请求的路径是目录,则放弃尝试后备文件。
location / { root /var/www/mysite; index index.html; autoindex on; set $fallback_file /index.html; if ($http_accept !~ text/html) { set $fallback_file /null; } if ($uri ~ /$) { set $fallback_file $uri; } try_files $uri $fallback_file; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。