项目利用react-router,做成single page application,进口地点/home/hard/Project/game/web-client/build/html/index.html,通过域名domain.com就能会见到这个进口。
问题是,利用了路由之后,如果uri为domain.com/games.html,通过可以通过欣赏器api跳转页面,可一旦刷新页面,就会报404。
我但愿的是路由到某个uri之后,纵然刷新页面,或直接在欣赏器中输入这个uri,一样可以直接路由到games.html的页面。
在论坛上提了问,几天了没人响应,也查不到相关的资料,所以说其实查不到的,也问不大白,还得靠本身摸索。
利用node的处事器http-server
我一开始没多想,觉得是nginx的问题,也许node的处事器天然支持这一点。换了,问题没有办理。
server "expression">{ "variable">listen 80; "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">listen 80; "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.html break; "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对应了谁人页面!