Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

req.protocol never gives https behind nginx proxy

I'm trying to recognise whether my express app is serving over an https protocol.

Using nginx to handle the certification and encryption (on the same machine), and forward requests, req.protocol evaluates to http even when https is being used and working fine.

I've tried both of the following (individually):

app.set('trust proxy', 'loopback');

and

app.enable('trust proxy');

Yet req.protocol still reports http.

What gives?

Here's req.header:

{ 'x-real-ip': '196.38.239.10',
  'x-forwarded-for': '196.38.239.10',
  host: 'idwork.co',
  'x-nginx-proxy': 'true',
  connection: 'close',
  'content-length': '0',
  'cache-control': 'no-cache',
  origin: 'file://',
  'content-type': 'application/json',
  'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Postman/4.3.2 Chrome/47.0.2526.73 Electron/0.36.2 Safari/537.36',
  'postman-token': 'redacted',
  accept: '*/*',
  'accept-encoding': 'gzip, deflate',
  'accept-language': 'en-US' }

Here are my relevant(?) nginx forwarding rules:

location / {
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_set_header X-NginX-Proxy true;
  proxy_pass 127.0.0.1:1234;
  proxy_redirect off;
}
like image 421
eye_mew Avatar asked Feb 07 '23 16:02

eye_mew


1 Answers

I needed to add this to my nginx block:

proxy_set_header X-Forwarded-Proto https; 

🙈

like image 79
eye_mew Avatar answered Feb 15 '23 22:02

eye_mew