简体   繁体   English

nginx后面的node.js应用程序可以使该节点应用程序确定连接是http还是https

[英]node.js app behind nginx can the node app figure out if the connection is http or https

I have a node.js app running on a server behind a nginx reverse proxy. 我有一个在nginx反向代理后面的服务器上运行的node.js应用程序。 Nginx is setup to handle both HTTP and HTTPS and forward both to the same node.js app. Nginx已设置为同时处理HTTP和HTTPS并将它们转发到同一node.js应用程序。 Is it possible for the node.js app to figure out if the request came from http or https? node.js应用程序能否确定请求是否来自http或https?

Also, is it possible for the JS on the client side to figure out if its on http or https? 另外,客户端的JS是否可以找出它是在http还是https上?

EDIT: found one solution for client side: 编辑:为客户端找到一种解决方案:

if (window.location.protocol != "https:")

is there a better way than this? 有没有比这更好的方法了?

Just add custom header from nginx to node Usually it's 只需将自定义标头从nginx添加到节点,通常是

proxy_set_header X-Forwarded-Proto $scheme;

and in node it will appear in request.headers['x-forwarded-proto'] . 在节点中,它将出现在request.headers['x-forwarded-proto']

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM