SoFunction
Updated on 2025-04-11

Solve the problem of Nginx configuration static resource file 404 Not Found

When using nginx as a static resource server, after the configuration is completed, the 404 Not Found error is reported through the browser access. My nginx configuration information is as follows:

location /images/ {  
     root /mnt/upload/files;  
}

All files are stored in/mnt/upload/files

analyze:

It was found that it was a configuration problem. There are two ways to configure static paths. The previous configuration was to write the root directory directly in the URL, but now the configuration is a prefixed URL, so it reported a 404 Not Found error.

The root configuration will follow the configured directory and form the corresponding file path, that is, the address you want to access is:

/images/

The file path that nginx takes according to configuration is

/mnt/upload/files/images/

And what I need is

/mnt/upload/files/

Nginx provides another static path configuration: alias configuration

Official root configuration

Sets the root directory for requests. For example, with the following configuration
location /i/ {
    root /data/w3;
}
The /data/w3/i/ file will be sent in response to the “/i/” request

Official alias configuration

Defines a replacement for the specified location. For example, with the following configuration
location /i/ {
    alias /data/w3/images/;
}
on request of “/i/”, the file /data/w3/images/ will be sent.

When accessing /i/, root is to go to /data/w3/i/ to request the file, alias is to go to /data/w3/images/ to request it, that is,

The path to root response: the configured path + the full access path (full location configuration path + the static file)

The path to alias response: configuration path + static file (remove the path configured in location)

Solution:

location /images/ {  
     alias /mnt/upload/files/;  
}

Note: When using alias, you must add "/" after the directory name; generally, configure root in location/ and alias in location/*.

This is the article about the solution to the Nginx configuration static resource file 404 Not Found problem. For more related content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!