Problem description

There is no problem refreshing the page after entering the page, when you jump from/route to /XXX route, refreshing again will prompt 404.

Question why

Our servers find resources by path based on page routing. The web site we packaged has only one HTML page, and there is no HTML in other resource directories. The server could not find the corresponding page, so it reported 404.

The solution

I saw someone on the web solve this problem by configuring multiple location objects and redirecting them back to the root directory’s index.html. However, I think this scheme is too complicated and not suitable for large projects with many pages. So I came up with a solution that only required one line of code.

<! Try_files $uri $uri/ /index.html;Copy the code
<! --> location / {root /usr/share/nginx/ HTML; index index.html index.htm; try_files $uri $uri//index.html; /XXX XXXXXX} <! Once you're done, just save, exit the Vim editor and restart Nginx. -->Copy the code

If you don’t want to bother, you can simply change the hash route to solve the problem.

Finally, if what I said is helpful to you, please give me a thumbs-up and support. I just started to share my experience, and I hope to get everyone’s support and correction.

Thank you!