SoFunction
Updated on 2025-03-02

Summary of the usage of nginx configuration history mode

This article mainly introduces the summary of the use of nginx configuration history mode, as follows:

worker_processes  1;
 
events {
    worker_connections  1024;
}
 
http {
    include       ;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
 
    server {
       listen       80;
       server_name  localhost;
        #Front-end project deployment        location / {
             root html; # Static page in html folder            index   ;
            try_files $uri $uri/ /;
        }
    }
}

try_files $uri $uri/ /;This sentence is an instruction in the Nginx server configuration to set the policy for processing requests.

  • $uri: This is a built-in variable in Nginx, representing the URI of the current request, and does not include the parameter part. For example, if the requested URL is/user?id=1,So$uriThe value is/user

  • $uri/: Try to process the request as a directory. If this directory exists, Nginx will try to return the default file under that directory (usuallyor)。

  • /: If the previous one$uriand$uri/No corresponding file or directory can be found, then return it/document.

therefore,try_files $uri $uri/ /;The meaning of   is: first try to find the corresponding file according to the requested URI. If it cannot be found, try to process the request as a directory. If it still cannot be found, finally return/document.

This is very useful for single page applications because the server returns the same HTML file regardless of the URL the user requests (i.e.), and then the front-end route determines which page to display.

This is the article about the use of nginx configuration history mode. For more related content on nginx configuration history mode, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!