大网数据广告活动
服务器运维问答

After adding SSL certificate i got site too many redirects loop (nginx)

· 52CE 我爱测

Hello and thank you for helping me. I recently added an ssl certificate to my site. It worked for a little bit but now I am getting a "too many redirects loop" in the browsers. I don't know much about servers (I'm following an on…

问题原文

原帖链接 · Jakxna360 · CC BY-SA 3.0

Hello and thank you for helping me. I recently added an ssl certificate to my site. It worked for a little bit but now I am getting a "too many redirects loop" in the browsers. I don't know much about servers (I'm following an online guide) so it might just be a rookie mistake.

Here is the file:

upstream app_server {
    server unix:/var/run/unicorn.sock fail_timeout=0;
}

server {
    listen 80;
    server_name example.com;
    return 301 http://www.example.com$request_uri;
}

server {
    listen   443 ssl;
    listen  [::]:80 default ipv6only=on;
    root /home/example/public;
    server_name www.example.com;
    rewrite     ^(.*)   https://www.example.com$1 permanent;
    ssl_certificate /example.com.chained.crt;
    ssl_certificate_key /example.com.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;
    index index.htm index.html;

    location / {
            try_files $uri/index.html $uri.html $uri @app;
    }

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|$
                    try_files $uri @app;
            }

     location @app {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://app_server;
    }
}

已采纳解答

原帖链接 · Michael Hampton · CC BY-SA 3.0

It's a rookie mistake.

You have placed the redirect in your https server block:

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

Which just redirects back to the same URL.

When you remove that, you should be fine. The redirect should only be in the block serving http on port 80, not in the block serving https on port 443.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-01-01)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。