I am trying to redirect HTTP requests to HTTPS, but keep on hitting an infinite redirect with nginx.
I've tried:
- Appending/removing the trailing slash from the nginx
proxy_pass
value - Removing/keeping
proxy_set_header Host $host;
- Changing docker to serve on the host's port 8080 (instead of
80
), and updating the nginx conf accordingly. (This returns connection refused errors.)
It looks like the redirect process goes like:
- Client hits port
80
- Client is redirected to port
443
- The
proxy_pass
location in the443
location block hits port80
- This new request is redirected to port
443
- Repeat ad infinitum
How do I stop this from looping?
Here's my nginx.conf
file:
server {
listen 80;
server_name sub.mydomain.com;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name sub.mydomain.com;
server_tokens off;
ssl_certificate /etc/letsencrypt/live/sub.mydomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/sub.mydomain.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location / {
proxy_pass http://sub.mydomain.com/;
proxy_redirect off;
# 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;
proxy_set_header X-Url-Scheme $scheme;
}
}
(The include
d /etc/letsencrypt/options-ssl-nginx.conf
file is auto-generated by Certbot.)
Here's my docker-compose.yml
:
version: '3'
services:
nginx:
build: .
ports:
- "80:80"
- "443:443"
restart: unless-stopped
volumes:
- ./data/nginx:/etc/nginx/conf.d
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx'"
depends_on:
- certbot
certbot:
image: certbot/certbot
restart: unless-stopped
volumes:
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
The docker-compose
and Let's Encrypt setup is based on this repo