Hello all,
I am running up against the following scenario:
Using a load balancer with SSL termination, and Proxy Protocol enabled, directing traffic to a droplet running nginx;
nginx has been configured to accept the proxy protocol
server {
listen 80 proxy_protocol;
[...]
location ~ / {
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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;
This is breaking my attempts to set a secure session cookie from within my app.
Does anyone have any suggestions? Is this perhaps a bug in the Digital Ocean load balancer?
Thanks to all who have ideas!
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Heya,
The X-Forwarded-Proto header represents the protocol used to connect to the load balancer. Since you have SSL termination enabled on your DigitalOcean Load Balancer, clients connect to the Load Balancer using HTTPS. After that, the Load Balancer connects to your backend (nginx) using HTTP, which is why X-Forwarded-Proto is being set to “http”.
The issue you’re seeing is a common one when using SSL termination on load balancers. Here’s how to solve it:
Trust the Header from the Load Balancer:
Since the traffic between your Load Balancer and the nginx server is internal (and thus can be considered safe from tampering), you can set X-Forwarded-Proto to the value provided by the Load Balancer instead of letting nginx reset it. Here’s how:
Replace this:
proxy_set_header X-Forwarded-Proto $scheme;
With this:
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
By making this change, you’re essentially passing along the X-Forwarded-Proto value as sent by the Load Balancer, instead of setting it to the $scheme used to connect to nginx.
Ensure Your Application Trusts the Header:
Ensure that your Express (or any other) app is set up to trust proxy headers. In Express, this can be achieved with:
app.set('trust proxy', true);
With this setting, Express will trust the X-Forwarded-Proto header and realize that the original request was made using HTTPS, even though it itself received the request over HTTP.
Check SSL Termination on Load Balancer:
Make sure SSL termination is correctly set up on the DigitalOcean Load Balancer. The SSL certificate should be valid and applied. The incoming traffic on port 443 should be forwarded to port 80 on the backend droplets.
By implementing these changes, the X-Forwarded-Proto header passed to your application should correctly reflect the protocol used to connect to the Load Balancer (i.e., HTTPS), and you should be able to set secure session cookies from within your app.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.