简体   繁体   English

HTTP到HTTPS重定向-Nginx与Node Express

[英]HTTP to HTTPS redirect - Nginx vs Node Express

I want to redirect the HTTP traffic to the secure HTTPS version of my website. 我想将HTTP流量重定向到我的网站的安全HTTPS版本。 I am running NodeJS Express on an nginx server. 我在Nginx服务器上运行NodeJS Express。 What would be the best way to do the redirect: using nginx or Express? 进行重定向的最佳方法是什么:使用nginx或Express? Is there any significant difference between the two options, like performance for example? 两种选择之间是否有重大区别,例如性能?

It all depends on how you do it but the performance difference will likely be insignificant. 这完全取决于您的操作方式,但性能差异可能微不足道。 What I usually do is when nginx handles the SSL keys and certificates then I also let it take care of the redirects. 我通常要做的是,当nginx处理SSL密钥和证书时,我还要让它负责重定向。 That way the Node app doesn't even need to know about the HTTP - all it cares is serving the requests coming from the reverse proxy. 这样,Node应用程序甚至不需要了解HTTP,它关心的只是服务于来自反向代理的请求。

Example nginx config: 示例nginx配置:

    server {
            listen 80;
            server_name example.com;
            add_header Strict-Transport-Security "max-age=3600";
            root /www/example.com/html;
            index index.html index.htm;

            location / {
                    return 302 https://example.com$request_uri;
            }
    }

Keep in mind that you will need to temporarily turn off the redirect to HTTPS if you're using Let's Encrypt but only for the time of certification renewal - something worth noting because it can be hard to diagnose when your certification renewal fails. 请记住,如果您使用的是“让我们加密”,但仅在证书更新时需要临时关闭到HTTPS的重定向-值得注意的是,由于证书更新失败时很难诊断。

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

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