我如何使用 try_files 重写 nginx 来让 React 路由器工作?
How can I use nginx rewrite with try_files to get react router working?
我在 nginx 中使用 try_files 指令并做出反应以强制所有路径转到 /index.html 并由反应路由器处理,这样路径就不需要放在前面与/#/.
这适用于:
location / {
try_files $uri /index.html;
}
所以像 /invitation
这样的 url 会去正确的地方。
但是,仍然有一些来源正在生成 /#/
urls,我想将它们重写为等效的 urls 而没有 /#/
(所以 /#/invitation
会转到 /invitation
)。
我已经尝试了所有我能想到的变体,包括更改顺序、转义特殊字符以及使用和不使用永久重定向,但没有任何效果:
location / {
rewrite ^/#/(.*) permanent;
try_files $uri /index.html;
}
正确的做法是什么?
Richard Smith 的评论是正确的 - 锚点部分永远不会发送到服务器。更多信息在这里:Is the anchor part of a URL being sent to a web server?
我在 nginx 中使用 try_files 指令并做出反应以强制所有路径转到 /index.html 并由反应路由器处理,这样路径就不需要放在前面与/#/.
这适用于:
location / {
try_files $uri /index.html;
}
所以像 /invitation
这样的 url 会去正确的地方。
但是,仍然有一些来源正在生成 /#/
urls,我想将它们重写为等效的 urls 而没有 /#/
(所以 /#/invitation
会转到 /invitation
)。
我已经尝试了所有我能想到的变体,包括更改顺序、转义特殊字符以及使用和不使用永久重定向,但没有任何效果:
location / {
rewrite ^/#/(.*) permanent;
try_files $uri /index.html;
}
正确的做法是什么?
Richard Smith 的评论是正确的 - 锚点部分永远不会发送到服务器。更多信息在这里:Is the anchor part of a URL being sent to a web server?