AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 781576
Accepted
xpt
xpt
Asked: 2024-08-08 12:52:55 +0800 CST2024-08-08 12:52:55 +0800 CST 2024-08-08 12:52:55 +0800 CST

nginx 添加 SSL 证书后重定向次数过多

  • 772

在添加ssl_certificate之前,我的nginx.conf非常简单:

server {
    listen 80 default_server;

    index index.php index.html index.htm;

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info  ^(.+\.php)(/.+)$;
        fastcgi_index            index.php;
        fastcgi_pass             php:9000;
        include                  fastcgi_params;
        fastcgi_read_timeout     1200s;
        fastcgi_param   PATH_INFO       $fastcgi_path_info;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

然后我按照这里使用 Nginx 设置 letsencrypt([domain-name]从头到尾进行替换),现在我的nginx.conf样子:

server {
    listen 80 default_server;

    server_name [domain-name] www.[domain-name];
    server_tokens off;

    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    location / {
        return 301 https://[domain-name]$request_uri;
    }
}

server {
    listen 443 default_server ssl http2;
    listen [::]:443 ssl http2;

    server_name [domain-name];

    ssl_certificate /etc/nginx/ssl/live/[domain-name]/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/live/[domain-name]/privkey.pem;
    
    location / {
        proxy_pass http://[domain-name];
    }

    index index.php index.html index.htm;

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info  ^(.+\.php)(/.+)$;
        fastcgi_index            index.php;
        fastcgi_pass             php:9000;
        include                  fastcgi_params;
        fastcgi_read_timeout     1200s;
        fastcgi_param   PATH_INFO       $fastcgi_path_info;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

请参阅此处的更改 - https://www.diffchecker.com/bAfVjewE/,

在此处输入图片描述

我认为这非常简单、直接且合理。

但是,我的 php 网站完全崩溃了——我的 chrome 浏览器说它进入了无限重定向(“重定向次数过多”),请参阅注释 2。

可能的原因是什么?如何解决?

笔记,

  1. 添加 ssl_certificate 很好,但是即使我在空站点上进行测试,也会出现无限重定向。
  2. 当发生无限重定向时,nginx 日志只会打印...[08/Aug/2024:15:xx:yy +0000] "GET / HTTP/1.1" 301 162 "-" "Mozilla/5.0 (X11; Linux x86_64)...,即使我已经看到浏览器上的协议已从 更改http为https。

如果我使用 访问它curl,我会得到:

$ curl -i https://my.site.name:443/
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Thu, 08 Aug 2024 15:42:45 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Location: https://my.site.name/

<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

服务器日志为:

[08/Aug/2024:15:42:45 +0000] "GET / HTTP/1.1" 301 162 "-" "curl/8.5.0" "my.ip"
[08/Aug/2024:15:42:45 +0000] "GET / HTTP/1.1" 301 162 "-" "curl/8.5.0" "-"

错误日志为空,因为这是我的 ngix 日志配置方式:

cd /var/log/nginx/

root@5b6a9033cb31:/var/log/nginx# ls -l
total 0
lrwxrwxrwx 1 root root 11 Jul 23 07:14 access.log -> /dev/stdout
lrwxrwxrwx 1 root root 11 Jul 23 07:14 error.log -> /dev/stderr
nginx
  • 1 1 个回答
  • 50 Views

1 个回答

  • Voted
  1. Best Answer
    Stina Andersson
    2024-08-09T11:03:28+08:002024-08-09T11:03:28+08:00

    问题是第一个服务器块监听端口 80(HTTP)并将所有流量重定向到 HTTPS(https://[domain-name]$request_uri)。

    第二个服务器块监听端口 443(HTTPS)并将流量代理回同一个域(proxy_pass http://[domain-name];)。

    这会形成重定向循环。

    用户请求http://my.site.name

    服务器重定向到https://my.site.name

    HTTPS 服务器将请求代理回http://my.site.name

    HTTP 服务器重定向到https://my.site.name

    循环继续......

    要修复此问题,您必须确保 HTTPS 服务器未代理回 HTTP 服务器。相反,它应该直接提供内容。

    像这样:

    server {
        listen 80 default_server;
        server_name [domain-name] www.[domain-name];
        server_tokens off;
    
        location /.well-known/acme-challenge/ {
            root /var/www/certbot;
        }
    
        location / {
            return 301 https://$host$request_uri;
        }
    }
    
    server {
        listen 443 default_server ssl http2;
        listen [::]:443 ssl http2;
        server_name [domain-name];
    
        ssl_certificate /etc/nginx/ssl/live/[domain-name]/fullchain.pem;
        ssl_certificate_key /etc/nginx/ssl/live/[domain-name]/privkey.pem;
    
        root /var/www/html;  # Adjust this path to your web root if needed
    
        index index.php index.html index.htm;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass php:9000;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    

    因此,HTTPS 块中的 try_files $uri $uri/ /index.php?$query_string; 可确保正确提供请求,而无需代理回 HTTP。

    位置 ~.php$ { ... } 块设置为直接在 HTTPS 服务器中处理 PHP 文件。

    HTTP 块中的 return 301 https://$host$request_uri; 确保所有 HTTP 请求都重定向到 HTTPS。

    更正代码,然后运行nginx -s reload,它就应该可以工作了。

    ############### 已编辑 ################

    您无需删除 Nginx 反向代理即可解决此问题。通过确保代理在同一协议(即 HTTPS)内进行,您仍然可以从反向代理中受益。

    一种方法是调整 HTTPS 服务器块中的 proxy_pass 指令以使用 https:// 而不是 http://。这可确保一旦流量重定向到 HTTPS,它就会停留在 HTTPS 上下文中,从而防止出现循环:

    location / {
        proxy_pass https://[domain-name];
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
    

    这样,您仍然可以利用 Nginx 反向代理的优势,同时解决重定向循环问题。如果您需要有关此设置的任何进一步帮助,请随时联系我们!

    • 2

相关问题

  • nginx 在 Debian 稳定版中安装失败

  • Nginx gzip_types - 在某些情况下是冗余指令?

  • Nginx 版本不可知的 php-fpm 配置

  • Nginx 位置 ~ /\.ht

  • 在 Nginx Ubuntu 14.04 上托管多个应用程序

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve