others-how to solve '404 page not found' when using nginx as reverse proxy for prometheus ?

1. Purpose

In this post, I would demo how to solve the below exception when using nginx as reverse proxy for prometheus.

404 page not found

2. Environment

  • Linux

3. The configurations

3.1 Nginx configurations

Here is the reverse proxy configuration for prometheus:

http {
    upstream prometheus {
        server 127.0.0.1:9090;
    }
    server {
            location /prometheus/ {
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header Host $http_host;
                    proxy_pass http://prometheus;
            }
    }
}

3.2 Start the prometheus service

Let’s start the prometheus service :

./prometheus --web.external-url=prometheus

3.3 The problem

When we visit the url:

http://mydomain/prometheus/graph

The error occurred:

mydomain.com/prometheus/static/css/2.89610b22.chunk.css 404 not found

4. Solution

We should use virtual host or server blocks to proxy prometheus as follows:

    server {
        listen       81;
        server_name  localhost;
        location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_pass http://prometheus;
         }
    }

What is a nginx virtual host?

Server Blocks, often referred to as Nginx virtual host are a feature of the Nginx web server that allows you to host multiple websites on one server. As opposed to setting up and configuring a server for each domain, hosting a number of websites on a single machine saves both time and money.

5. Summary

In this post, I demonstrated how to solve ‘404 page not found’ problem when using nginx to proxy some service, the key process is to try the virtual host.