SoFunction
Updated on 2025-03-04

nginx configuration https://localhost/index report 404

When your Nginx configuration causes a 404 error on page refresh time, it is usually caused by the following reasons:

  • Static file path configuration error: Nginx does not point to the directory of the static file correctly.
  • Front-end routing issues: If it is a SPA (single-page application), Nginx does not correctly redirect the request to the entry file when refreshing the page (e.g.)。
  • Reverse proxy configuration error: If Nginx is a reverse proxy, the backend service may not handle the request correctly.

Check and resolve steps

1. Check the static file path configuration

Ensure that the Nginx configuration file is inrootoraliasThe directive correctly specifies the path to the static file.

server {
    listen 80;
    server_name ;

    root /path/to/your/static/files;  # Make sure the correct static file path is specified here
    location / {
        try_files $uri $uri/ /;  # Try to match the file, if it does not exist, return    }

    #Other location configuration...}

2. Handle front-end routing

For SPA applications, make sure Nginx redirects all unmatched requests to, so that front-end routing can handle these requests.

server {
    listen 80;
    server_name ;

    root /path/to/your/static/files;

    location / {
        try_files $uri $uri/ /;  # Try to match the file, if it does not exist, return    }

    #Other location configuration...}

3. Check the reverse proxy configuration

If your Nginx acts as a reverse proxy, make sure the backend service can handle the request correctly.

server {
    listen 80;
    server_name ;

    location / {
        proxy_pass http://backend_server;  # Replace with your backend service address        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    #Other location configuration...}

4. Check the log

Check out Nginx's error log and access log for more details about 404 errors.

tail -f /var/log/nginx/
tail -f /var/log/nginx/

Sample configuration

Here is a complete example configuration for static file and SPA applications:

server {
    listen 80;
    server_name ;

    root /path/to/your/static/files;

    location / {
        try_files $uri $uri/ /;  # Try to match the file, if it does not exist, return    }

    location /api/ {
        proxy_pass http://backend_server;  # Replace with your backend service address        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    #Other location configuration...}

Test configuration

After modifying the configuration file, test whether the Nginx configuration is correct:

sudo nginx -t

If the test passes, reload Nginx to apply the new configuration:

sudo systemctl reload nginx

This is the article about nginx configuration https://localhost/index 404. For more related nginx configuration index 404 content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!