使用现有的反向代理将所有http转发到nginx上的https(node.js应用) [英] Forward all http to https on nginx with existing reverse proxy (node.js app)

查看:143
本文介绍了使用现有的反向代理将所有http转发到nginx上的https(node.js应用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前言:在过去的几天里,我已经搜索了数十本教程,而其中的任何一本都无法使用,这就是我在这里问的原因.我希望这是我想念的简单事情.我感谢我能得到的任何建议!

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!

好-我正在安装nginx的centos 6 vps.我在此vps上有多个域.在有问题的域上,我正在端口9000上运行一个node.js应用程序.我目前具有proxy_pass设置,将www.example.net指向www.example.net:9000 -效果很好.我现在需要做的是将所有流量传入 http://www.example.net 并将其重定向到 https://www.example.net ,同时在端口9000上保留应用程序的代理密码.

Ok -- so I'm running a centos 6 vps with nginx installed. I have multiple domains on this 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. 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.

同样,我尝试了数十种不同的教程和stackoverflow答案,但是它们都不适合我.我不得不恢复到刚开始时使用的nginx配置,以便可以在浏览器中查看我的应用程序.我将在这里发布我所拥有的东西,如果有人能指出我正确的方向,将不胜感激!

Again, I've tried dozens of different tutorials and stackoverflow answers, but none of them work for me. I've had to revert back to the nginx config that I started with just so I can view my app in a browser. 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";


    }
}

推荐答案

我们在设置中执行了类似的操作.我们的nginx配置用于将所有http请求重定向到https"看起来像这样:

We do something like this for our setup. Our nginx config for a "redirect all http requests to https" looks something like this:

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";
    }
}

因此,侦听端口80的HTTP服务器总是向侦听端口443的HTTPS服务器发出HTTP 301重定向.

Thus the HTTP server listening on port 80 always issues an HTTP 301 redirect to the HTTPS server listening on port 443.

希望这会有所帮助!

这篇关于使用现有的反向代理将所有http转发到nginx上的https(node.js应用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆