Prometheus Scrape Target Returning 403 Forbidden Fix
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_fileorpassword_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
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
- 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro