


How does Nginx solve the 404 problem of page refresh in history mode?
Pre-knowledge
Single page application (SPA - single page application)
Only load the page for the first time When, the only HTML page and its public static resources are returned, and subsequent page jumps will not fetch the HTML file from the server. (Hash and history routing implement browser url changes without refreshing the page)hash routing
Example: www.baidu.com/#/home, originally hash is used to combine The anchor point realizes the control of the page view. When the value after # changes, the page will not be re-requested. This is mainly achieved through the onhashchange method of the window.history routing
Compared with hash routing, the most intuitive change is that there is no # in the routing. By calling a series of methods on the window.history object, the page is not refreshed. Jump(pushState, replaceState).
In history mode, because the url has changed, if you refresh the page manually at this time, the browser thinks it is requesting a new page (initiating a new Http request), and the new page is Does not exist (if the backend is not configured), resulting in 404.
Let’s briefly describe what happens after entering the IP or domain name on the browser (it feels a bit like an interview question????). After pressing Enter, the http sent by the browser will request html. After a series of forwarding and addressing parsing, the file is received by port 80 (default) on the server where the target IP is located. At this time, the problem arises. After the server's 80 interface receives the HTTP request, it does not know what to do. To return something, this time you need Nginx to perform static resource proxy and tell the server what static files to return
Nginx
For general project deployment, we need to deal with nginx. conf configuration file
What you need to know about this file is as follows
.... # http 是指令块,针对http网络传输的一些指令配置 http { #文件扩展名与文件类型映射表 include mime.types; #设置客户端与服务端请求的超时时间 keepalive_timeout 65; # 开启压缩功能,目的:提高传输效率,节省带宽 gzip on server { #监听端口 listen 80; #服务命名,最好就是用这个服务器的域名命名 server_name localhost; #指令块,配置外部访问资源和实际资源的对应关系 location /{ root /usr/blog; #表示静态资源所在的目录 index index.html index.htm; #访问这个路径对应的默认静态资源文件或者网页 } } }
location
Syntax
location [=|~|~*|^~|@] uri { ... } location @name { ... }
=: indicates exact match
~: Indicates case-sensitive regular matching
~*: Indicates case-insensitive regular matching
^~: Indicates that the URI starts with a regular string
!~: Indicates case-sensitive regular expression mismatch
!~*: Indicates case-insensitive regular non-matching
/: Universal matching, any request will be matched to
Common matching rules
# 将所有请求直接转发给服务器的9090端口 location = / { proxy_pass http://127.0.0.1:9090/; } # 目录匹配 location ^~ /static/ { root /webroot/static/; } # 后缀匹配 location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { root /webroot/res/; } # 将/account/开始的请求转发给Account服务器 location /account/ { proxy_pass http://127.0.0.1:8080/ } # 将/order/开始的请求转发给Order服务器 location /order/ { proxy_pass http://127.0.0.1:9090/ }
root and alias
The difference between the two is how nginx interprets the url after location
[root]
Syntax: root path
Default value: root html
Configuration section: http, server, location, if
Processing result: root path + location path
[alias]
Syntax: alias path
Configuration section :location
Processing results: Use alias path to replace location path
# 返回/www/root/html/t/a.html的文件 location ^~ /t/ { root /www/root/html/; } # 返回/www/root/html/new_t/a.html的文件 # 把location后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录。 location ^~ /t/ { alias /www/root/html/new_t/; }
Solve the problem of 404 after refreshing
It can be known from the above knowledge that after refreshing, the browser will The current url requests the html file, but SPA only has one html file, so you need to configure a line of code in the corresponding location of nginx.conf try_files $uri $uri/ /index.html;
Tell nginx if in order Check whether the file exists, if not, redirect to the index.html file
The above is the detailed content of How does Nginx solve the 404 problem of page refresh in history mode?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to fix Nginx 403 Forbidden error? Check file or directory permissions; 2. Check .htaccess file; 3. Check Nginx configuration file; 4. Restart Nginx. Other possible causes include firewall rules, SELinux settings, or application issues.

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.

Answer to the question: 304 Not Modified error indicates that the browser has cached the latest resource version of the client request. Solution: 1. Clear the browser cache; 2. Disable the browser cache; 3. Configure Nginx to allow client cache; 4. Check file permissions; 5. Check file hash; 6. Disable CDN or reverse proxy cache; 7. Restart Nginx.
