Skip to content

Prometheus Scrape Target Returning 403 Forbidden Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Prometheus Scrape Target Returning 403 Forbidden Fix. We cover key concepts, practical examples, and best practices.

Your Prometheus target shows scrape failed: server returned HTTP status 403 Forbidden — the metrics endpoint requires authentication, the IP is not allowlisted, or the TLS client certificate is missing. Protected endpoints need explicit configuration.

The Problem

scrape_configs:
  - job_name: 'api'
    static_configs:
      - targets: ['api.example.com:443']
    scheme: https
scrape failed: server returned HTTP status 403 Forbidden

The API endpoint requires authentication via bearer token, but Prometheus is not configured to send any credentials.

Step-by-Step Fix

1. Configure bearer token authentication

scrape_configs:
  - job_name: 'api'
    static_configs:
      - targets: ['api.example.com:443']
    scheme: https
    authorization:
      type: Bearer
      credentials: 'your-api-token'
    scrape_interval: 30s
    scrape_timeout: 10s

2. Use basic authentication

scrape_configs:
  - job_name: 'api'
    static_configs:
      - targets: ['api.example.com:443']
    scheme: https
    basic_auth:
      username: 'monitoring'
      password_file: /etc/prometheus/auth/monitoring-passwd

3. Use TLS client certificate

scrape_configs:
  - job_name: 'secure-api'
    static_configs:
      - targets: ['api.example.com:443']
    scheme: https
    tls_config:
      cert_file: /etc/prometheus/tls/client.crt
      key_file: /etc/prometheus/tls/client.key
      ca_file: /etc/prometheus/tls/ca.crt
      insecure_skip_verify: false

4. Use custom headers

scrape_configs:
  - job_name: 'api'
    static_configs:
      - targets: ['api.example.com:443']
    scheme: https
    authorization:
      type: Bearer
      credentials_file: /etc/prometheus/token
    # Add custom headers
    params:
      api_key: ['your-api-key']

5. Debug authentication issues

# Test with curl
curl -v https://api.example.com/metrics \
  -H "Authorization: Bearer your-token"

# Check Prometheus logs
journalctl -u prometheus -n 50

# Verify target status in Prometheus UI
# http://prometheus:9090/targets

Expected output:

Target: api.example.com:443
Status: UP
Auth: Bearer token (configured)
Auth result: 200 OK

Prevention Tips

  • Store credentials in files with credentials_file or password_file
  • Use bearer tokens for API authentication
  • Use TLS client certificates for mutual authentication
  • Test endpoint authentication with curl before configuring Prometheus
  • Rotate credentials regularly and update config files

Common Mistakes with scrape failed

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world PROMETHEUS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Can I use different authentication per target?

Use separate scrape_configs per authentication method. Each scrape config applies the same auth to all targets within it. For target-specific auth, use relabeling with __authorization_* labels or separate job definitions.

How do I handle token rotation?

Store the token in a file and use credentials_file instead of credentials. When the token rotates, Prometheus reads the updated file on the next scrape without needing a restart. Use SIGHUP for Prometheus to reload config if needed.

Can I authenticate to multiple endpoints with different tokens?

Yes, create separate scrape_configs for each set of targets that share the same token. Each config block specifies its own authorization or basic_auth section. Targets with different auth requirements belong in different config blocks.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro