秀米云香港服务器

项目利用react-router,做成single page application,进口地点/home/hard/Project/game/web-client/build/html/index.html,通过域名domain.com就能会见到这个进口。

问题是,利用了路由之后,如果uri为domain.com/games.html,通过可以通过欣赏器api跳转页面,可一旦刷新页面,就会报404。

序列号 CPU RAM HDD 带宽 售价(美元) 免费试用
香港服务器1 E5-2620 32G 1T HDD 50M/无限流量 $196.00 立即申请
香港服务器2 E5-2650 32G 1T HDD 50M/无限流量 $256.00 立即申请
香港服务器3 E5-2680 32G 1T HDD 50M/无限流量 $316.00 立即申请
香港服务器4 E5-2690 32G 1T HDD 50M/无限流量 $336.00 立即申请
香港服务器5 E5-2697 32G 1T HDD 50M/无限流量 $376.00 立即申请
香港服务器6 E5-2620*2 32G 1T HDD 50M/无限流量 $376.00 立即申请
香港服务器7 E5-2650*2 32G 1T HDD 50M/无限流量 $436.00 立即申请
香港服务器8 E5-2680*2 32G 1T HDD 50M/无限流量 $476.00 立即申请
香港服务器9 E5-2690*2 32G 1T HDD 50M/无限流量 $556.00 立即申请
香港服务器10 E5-2697*2 32G 1T HDD 50M/无限流量 $596.00 立即申请
香港服务器11 E5-2680v4*2 32G 1T HDD 50M/无限流量 $696.00 立即申请
香港服务器12 E5-2698v4*2 32G 1T HDD 50M/无限流量 $796.00 立即申请

我但愿的是路由到某个uri之后,纵然刷新页面,或直接在欣赏器中输入这个uri,一样可以直接路由到games.html的页面。

在论坛上提了问,几天了没人响应,也查不到相关的资料,所以说其实查不到的,也问不大白,还得靠本身摸索。

利用node的处事器http-server

我一开始没多想,觉得是nginx的问题,也许node的处事器天然支持这一点。换了,问题没有办理。

开始的nginx设置

server "expression">{ "variable">listen80; "line">server_ "variable">name "variable">www.domain.com "variable">domain.com; "line">location~* "variable">.js${ "line">root "end-block">/home "end-block">/hard "end-block">/Project "end-block">/game "end-block">/web-client "end-block">/build "end-block">/js; "line">} "line">location/ "expression">{ "variable">root "end-block">/home "end-block">/hard "end-block">/Project "end-block">/game "end-block">/web-client "end-block">/build "end-block">/html/; "line">} }

这个导致404是很明明的,假如uri是domain.com/games.html,那么nginx会试图在/home/hard/Project/game/web-client/build/html/目次下找games.html,404是必定的。

nginx rewrite办理

新的设置

server "expression">{ "variable">listen80; "line">server_ "variable">name "variable">www.domain.com "variable">domain.com; "line">location~* "variable">.js${ "line">root "end-block">/home "end-block">/hard "end-block">/Project "end-block">/game "end-block">/web-client "end-block">/build "end-block">/js; "line">} "line">location/ "expression">{ "variable">root "end-block">/home "end-block">/hard "end-block">/Project "end-block">/game "end-block">/web-client "end-block">/build "end-block">/html/; "line">} "line">location~*html "expression">{ "variable">rewrite "variable">.* "end-block">/index.htmlbreak; "line">root "end-block">/home "end-block">/hard "end-block">/Project "end-block">/game "end-block">/web-client "end-block">/build "end-block">/html/; "line">} }

增加了一个 location 用来设置uri里头有html的,我用html来标识是否是客户端页面。

通过rewrite .* /index.html break;把一切path重写为/index.html,break很重要,它使得url的匹配竣事,最终处事返回的文档其实是/home/hard/Project/game/web-client/build/html/index.html。

谁人break抉择了欣赏器里的url是稳定的,而http响应的文档其实就是index.html,,而欣赏器上的path,会自动的被react-router处理惩罚,举办无刷新的跳转,我们看到的功效就是path对应了谁人页面!

标题:修改nginx设置办理react-router欣赏器刷新呈现404页面的问题

地址: https://www.yunhk.xyz/26019.html