【问题标题】:how to redirect https to http in nginx如何在 nginx 中将 https 重定向到 http
【发布时间】:2019-03-28 10:49:31
【问题描述】:

我有一个 tomcat 服务器,目前在端口 8082(http) 上运行。我必须在不触及服务器代码或 tomcat 配置的情况下创建站点 https。我安装了 nginx 并且可以将 https 重定向到 http,但浏览器仍然说站点不安全。我们怎样才能让客户端使用https,然后nginx内部重定向到http,但是对客户端所有的调用都是https。

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    server {
        listen       80;
        server_name  localhost;
        location / {
            proxy_pass "http://localhost:8082/app/";
        }
    }

    server {
        listen       443 ssl;
        server_name  localhost;
        ssl_certificate      nginx-selfsigned.crt;
        ssl_certificate_key  nginx-selfsigned.key;
        rewrite ^(.*) http://localhost:8082/app$1 permanent;
    }
}

【问题讨论】:

  • 您正在寻找的是反向代理,这是在 Nginx 中使用 proxy_pass 实现的。

标签: nginx


【解决方案1】:

您应该使用Richard提到的proxy_pass,这样您就不会通过端口8082重定向到后端URL(这就是broswer直接访问http协议的原因。 您还需要放置防火墙规则来保护对该端口的外部访问

【讨论】:

  • worker_processes 1;事件 {worker_connections 1024; } http { 包括 mime.types; default_type 应用程序/八位字节流;发送文件;服务器{听80; server_name 本地主机;位置 / { proxy_pass "localhost:8082/app"; } } 服务器 { 听 443 ssl; server_name 本地主机; ssl_certificate nginx-selfsigned.crt; ssl_certificate_key nginx-selfsigned.key;重写 ^(.*) localhost:8082/app$1 永久; } }
  • 修改了原来的查询。当我输入 https//localhost:443 时,它会从 nginx 重定向到 http,但是一旦用户登录,浏览器 url 就会再次返回 http
【解决方案2】:

对于您当前的配置,只需使用 443,并在此侦听器中使用 proxy_pass,就像

server {
        listen       443 ssl;
        server_name  localhost;
        ssl_certificate      nginx-selfsigned.crt;
        ssl_certificate_key  nginx-selfsigned.key;

        location / {
            proxy_pass "http://localhost:8082/app/";
        }
 }

你可能对 80 端口感到困惑,应该将其重定向到 443 以确保所有流量都是 HTTPS,例如

server {
        listen       80;
        rewrite ^(.*) https://localhost/$1 permanent;
}

另请注意,这些是我从您的配置中复制的伪代码,您需要尝试并稍后更改

【讨论】:

    猜你喜欢
    • 2022-08-14
    • 2011-03-29
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 2014-10-11
    相关资源
    最近更新 更多