html - 使用 Nginx 响应路由器 html5 Pushstae

标签 html nginx react-router

我有一个react应用程序(它使用react-router和html5 Pushstate路由) 我想要http://example.com/v2作为服务器我的新网站的基本路由

我使用这些 nginx 配置:

这是为了捕获/v2 并使用新的 js 文件

location ^~ /v2 {
        alias /usr/local/theapp/dist;
        try_files $uri $uri/ /v2/index.html;
        access_log /var/log/nginx/theapp/acces.web.log;
        error_log /var/log/nginx/theapp/error.web.log debug;
}

location ~ ^/(v2)/.+\..+$ {
        try_files $uri =404;
       alias /usr/local/theapp/dist;
       error_log /var/log/nginx/theapp/error.web.log debug;
}

由于使用第一个位置 block ,即使是 404 也会被重定向到 v2/index.html,因此第二个位置的目的是处理带有扩展名的 uri,因此如果 http://example. com/v2/user/profile/picture.jpg 不存在,那么我仍然会得到正确的 404 资源未找到错误。

上面的代码显然不起作用,因为 ^~ 的优先级高于 ~。尝试了 2 ~ 但我得到了 try_files 重定向循环:(

最佳答案

正如您所注意到的,第二个位置 block 将不会被访问。此外,与正则表达式位置结合使用时,别名 会更加复杂。请参阅this document了解更多。

同时使用 aliastry_files 可能会导致问题(请参阅 this long standing bug)。

尝试:

location ^~ /v2 {
    alias /usr/local/theapp/dist;

    if (!-e $request_filename) {
        rewrite ^[^.]*$ /v2/index.html last;
    }

    access_log /var/log/nginx/theapp/acces.web.log;
    error_log /var/log/nginx/theapp/error.web.log debug;
}

参见this note关于 if 的使用。

关于html - 使用 Nginx 响应路由器 html5 Pushstae,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43356905/

相关文章:

python - nginx uwsgi 和 cgi python 脚本

php - 在同一个 vps 上运行 node、PHP 和 Python

reactjs - 如何在具有 protected 功能的 React Router 中映射路由?

javascript - 如何将 html 解析为 React 组件?

javascript - 一键点击生成所有结果 - Javascript

sockets - Nginx 无法访问 CentOS 7 上的 uWSGI unix 套接字

javascript - 如何使用 React-Router 将页面限制为访客用户?

reactjs - 在组件内使用react router Link

html - 为什么 XSLT 文件中没有填充背景图像

html - 垂直对齐 div 中的内容