Nginx 将 http 重写为 https 并代理到另一个端口,ERR_TOO_MANY_REDIRECTS 错误

兰萨纳卡马拉

在这里尝试使用 Nginx 作为反向代理。这就是我想要实现的目标:

  • 重定向example.comwww.example.comhttps://example.com
  • 将请求代理到另一个端口。

这是流程:example.com-> Nginx->Go web server listening on port 5000

似乎重写工作正常,导致我在浏览器中得到https://example.com,但是我在浏览器中收到此错误:

ERR_TOO_MANY_REDIRECTS

如果重要的话,我的 DNS 设置如下:

@   - A - 11.XX.XX.XX
www - A - 11.XX.XX.XX

这是我的/etc/nginx/nginx.conf文件:

events {
    worker_connections 1024;
}

http {
    server {
        listen      80;
        listen      [::]:80;
        server_name www.example.com example.com;

        rewrite ^(.*)$ https://example.com$request_uri permanent;

        location / {
                proxy_pass http://127.0.0.1:5000;
        }
    }

    server {
        listen      443 ssl;
        listen      [::]:443 ssl;
        server_name www.example.com example.com;

        ssl_certificate "/etc/letsencrypt/live/example.com/fullchain.pem";
        ssl_certificate_key "/etc/letsencrypt/live/example.com/privkey.pem";

        rewrite ^(.*)$ https://example.com$request_uri permanent;

        location / {
            proxy_pass http://127.0.0.1:5000;
        }
    }
}

任何帮助,将不胜感激。网络小白在这里。

兰萨纳卡马拉

另外两个答案对于解决这个问题的答案非常有帮助(重定向循环)。然而,还有另一个错误,即即使我重定向到非 www https 版本,www 也每次都出现。

这是执行以下操作的更新配置:

  • 将 www 转为非 www
  • 将 http 转为 https

    events {
        worker_connections 1024;
    }
    
    http {
        server {
            listen      80;
            listen      [::]:80;
            server_name www.example.com example.com;
    
            return 301 https://example.com$request_uri;
        }
    
        server {
            listen      443 ssl;
            listen      [::]:443 ssl;
            server_name www.example.com;
    
            ssl_certificate "/etc/letsencrypt/live/example.com-0001/fullchain.pem";
            ssl_certificate_key "/etc/letsencrypt/live/example.com-0001/privkey.pem";
    
            return 301 https://example.com$request_uri;
        }
    
        server {
            listen      443 ssl;
            listen      [::]:443 ssl;
            server_name example.com;
    
            ssl_certificate "/etc/letsencrypt/live/example.com-0001/fullchain.pem";
            ssl_certificate_key "/etc/letsencrypt/live/example.com-0001/privkey.pem";
    
            location / {
                proxy_pass http://127.0.0.1:5000;
            }
        }
    }
    

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Nginx将HTTP重写为HTTPS表示重定向循环?

URL将HTTP重写为https

URL将http重写为https并进行加密

IIS Url将规则HTTP重写为HTTPS并添加WWW

如何将url http重写为https

NGINX:将url和反向代理重写到另一个端口

使用htaccess将所有内容从http重写为https,但响应时带有错误代码以返回api

如何将Nginx从http配置为https

EC2 Ubuntu NGINX配置HTTPS重定向给我错误:ERR_TOO_MANY_REDIRECTS

将 htaccess 重写为 nginx

NGINX 将 http 重定向到 https

Web.config 将规则 http 重写为 https 和 angular 路由组合

.htaccess-将非www重写为www; php到html; http到https

在将ISAPI重写到位的同时,将HTTPS重定向到一个目录的HTTP

NGINX 将位置重写到另一个端口

Nginx HTTP未重定向到HTTPS 400错误请求“普通HTTP请求已发送到HTTPS端口”

nginx:将所有内容从http重定向到https,除了一个url模式

Nginx将http重定向到https并通过一个重定向删除尾部斜杠

用路由将htaccess重写为https

处理nginx 400“普通HTTP请求已发送到HTTPS端口”错误

普通的HTTP请求已发送到HTTPS端口错误Docker Nginx

将所有URL重写为除nginx中的一个以外的其他URL

将 Nginx/Apache 重写规则重写为 IIS

Nginx Reserve-Proxy https和http连接错误

ActionController::InvalidAuthenticityToken 使用nginx代理https请求到其他nginx(http)代理到rails(http)

将Nginx HTTP / HTTPS www重定向到非www

kubernetes nginx入口无法将HTTP重定向到HTTPS

Nginx-将HTTP重定向到HTTPS

将 VPS 上的 nginx 从 http 转换为 https