我有一个带有常规浏览器路由器(没有哈希)的单页应用程序。每当有人浏览页面并点击刷新按钮时,nginx 都会尝试在此路径上查找文件。所以如果有人在mypage.com/about
nginx 寻找about
文件并以 404 Not Found 响应。如何解决这个问题?
我正在考虑用通配符指定一个位置 -mypage.com/*
except /api
不过,因为这个应用程序中的每个后端端点都以api
。如何匹配除一个之外的所有路径?这就是我的配置的样子:
upstream frontend {
server frontend:3000;
}
upstream backend {
server backend:8000;
}
server {
listen 80;
location /api {
proxy_pass http://backend;
proxy_set_header Host \$http_host;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
}
location / {
proxy_pass http://frontend;
proxy_redirect default;
}
}
为什么您的代理请求前端应用程序?我假设您正在使用某种开发服务器来为您的前端应用程序提供服务。最好将前端应用程序构建为静态文件,并将它们作为常规静态文件提供服务,除了 nginx 之外不需要任何服务器。
至于你的问题,如果你将前端应用程序构建到静态文件中,你可以在 nginx 中配置位置,如下所示:
root /var/www/your_site;
location / {
try_files $uri /index.html;
}
其中index.html是应用程序的入口点,根路径应配置为存储它的位置。
如果您仍然想通过 nginx 从开发服务器提供前端应用程序,您可以配置 nginx 来处理来自上游的错误并将错误页面指向开发服务器的根目录。
在这种情况下,以下指令应该可以帮助您:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_intercept_errors http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_intercept_errors
http://nginx.org/en/docs/http/ngx_http_core_module.html#error_page http://nginx.org/en/docs/http/ngx_http_core_module.html#error_page
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)