简体   繁体   English

使用现有的反向代理(node.js应用)将所有http转发到nginx上的https

[英]Forward all http to https on nginx with existing reverse proxy (node.js app)

Preface: I have searched dozens of tutorials for this over the past few days and I can't get any of them to work, which is why I'm asking here. 前言:在过去的几天中,我已经搜索了数十本教程,而其中的任何一本都无法使用,这就是为什么我在这里问这个问题。 I hope it's something simple that I'm missing. 我希望这很简单,我很想念。 I appreciate any advice I can get! 感谢您能得到的任何建议!

Ok -- so I'm running a centos 6 vps with nginx installed. 好的-我正在安装nginx运行centos 6 vps。 I have multiple domains on this vps. 我在此vps上有多个域。 On the domain in question, I'm running a node.js app on port 9000. I currently have proxy_pass setup to point www.example.net to www.example.net:9000 -- and that works fine. 在有问题的域上,我正在端口9000上运行一个node.js应用程序。我目前具有proxy_pass设置,将www.example.net指向www.example.net:9000 -效果很好。 What I need to do now is take all traffic coming in to http://www.example.net and redirect it to https://www.example.net while preserving the proxy pass for the app on port 9000. 我现在需要做的是将所有流量传入http://www.example.net ,并将其重定向到https://www.example.net,同时保留端口9000上应用程序的代理权限。

Again, I've tried dozens of different tutorials and stackoverflow answers, but none of them work for me. 同样,我尝试了数十种不同的教程和stackoverflow答案,但是它们都不适合我。 I've had to revert back to the nginx config that I started with just so I can view my app in a browser. 我必须恢复到刚开始使用的nginx配置,以便可以在浏览器中查看我的应用程序。 I will post here what I have, and if anyone could point me in the right direction, it would be greatly appreciated! 我将在这里发布我所拥有的东西,如果有人能指出正确的方向,将不胜感激!

 server {
    listen      123.45.678.90:80;
    server_name example.net www.example.net;
    error_log  /var/log/httpd/domains/example.net.error.log error;

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 https://123.45.678.90:9000/;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";


    }
}

We do something like this for our setup. 我们为设置执行类似的操作。 Our nginx config for a "redirect all http requests to https " looks something like this: 我们用于“将所有http请求重定向到https ”的nginx配置看起来像这样:

server {
    listen      123.45.678.90:80;
    server_name example.net www.example.net;
    error_log  /var/log/httpd/domains/example.net.error.log error;

    # Always redirect to HTTPS
    redirect 301 https://www.example.net$request_uri;
}

server {
    listen      123.45.678.90:443 ssl;
    server_name example.net www.example.net;
    error_log  /var/log/httpd/domains/example.net.error.log error;
    ssl_certificate /path/to/server.crt
    ssl_certificate_key /path/to/server.key

    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 https://123.45.678.90:9000/;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

Thus the HTTP server listening on port 80 always issues an HTTP 301 redirect to the HTTPS server listening on port 443. 因此,侦听端口80的HTTP服务器始终向侦听端口443的HTTPS服务器发出HTTP 301重定向。

Hope this helps! 希望这可以帮助!

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

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