Skip to content

Commit 69bfdc1

Browse files
committed
feat(nginx-proxy): Implement ssl_reject_handshake for missing certificates
Instead of serving 500 errors with default certificates when a vhost has no valid SSL certificate, nginx now: 1. Uses default certificate (if available) and returns 503 2. Rejects the SSL/TLS handshake entirely (if no default cert) Benefits: - Prevents certificate warning dialogs in browsers - More secure - doesn't expose invalid/default certificates - Cleaner failure mode for unknown hosts - Matches upstream jwilder/nginx-proxy behavior Changes: - Updated fallback HTTPS server block to use ssl_reject_handshake - Added condition to check for default certificate availability - Changed status code from 500 to 503 for better HTTP semantics - Documented SSL certificate handling behavior in README
1 parent 806b081 commit 69bfdc1

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

nginx-proxy/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,28 @@ deny all;
130130

131131
---
132132

133+
## SSL Certificate Handling
134+
135+
### Certificate Lookup
136+
137+
The proxy automatically detects SSL certificates from `/etc/nginx/certs/`:
138+
139+
```bash
140+
/etc/nginx/certs/example.com.crt
141+
/etc/nginx/certs/example.com.key
142+
```
143+
144+
### Fallback Certificate Behavior
145+
146+
When a vhost is accessed via HTTPS but no matching certificate is found:
147+
148+
1. **If default certificate exists:** Uses `/etc/nginx/certs/default.crt` and returns 503
149+
2. **If no default certificate:** Rejects the SSL/TLS handshake
150+
151+
This prevents certificate warning dialogs in browsers and improves security by not exposing invalid certificates.
152+
153+
---
154+
133155
## Environment Variables
134156

135157
| Variable | Description | Default |

nginx-proxy/nginx.tmpl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ server {
554554
{{ end }}
555555
}
556556

557-
{{ if (and (not $is_https) (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
557+
{{ if (and (not $is_https) (ne $https_method "nohttps")) }}
558558
server {
559559
server_name {{ $host }};
560560
listen 443 ssl {{ $default_server }};
@@ -563,10 +563,17 @@ server {
563563
listen [::]:443 ssl {{ $default_server }};
564564
http2 on;
565565
{{ end }}
566-
return 500;
567566

567+
{{ if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
568+
# Use default certificate as fallback
568569
ssl_certificate /etc/nginx/certs/default.crt;
569570
ssl_certificate_key /etc/nginx/certs/default.key;
571+
{{ else }}
572+
# No valid certificate for this vhost nor default certificate found, so reject SSL handshake
573+
ssl_reject_handshake on;
574+
{{ end }}
575+
576+
return 503;
570577
}
571578
{{ end }}
572579

0 commit comments

Comments
 (0)