【问题标题】:Can't redirect request from IP to domain name (nginx). Got "ERR_TOO_MANY_REDIRECTS"无法将请求从 IP 重定向到域名 (nginx)。得到“ERR_TOO_MANY_REDIRECTS”
【发布时间】:2018-03-14 08:50:13
【问题描述】:

我有一个在 Nginx (ubuntu 16) 上运行的服务器。我还有一个重定向到此服务器 IP 的域名。当然,我想在地址栏中向用户显示域名,而不是 IP(现在这样)。为此,我将/etc/nginx/sites-aviable文件夹中的站点配置设置更改为以下内容:(该项目是用symfony编写的,位置主要来自它上面的docks)

server {
    listen 80;
    server_name **.***.***.***; #My server ip
    return 301 $scheme://example.com$request_uri;
}
server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com/web;
    index app.php app_dev.php;

    location / {
        try_files $uri /app.php$is_args$args;
        proxy_buffer_size          128k;
        proxy_buffers              4 256k;
        proxy_busy_buffers_size    256k;

    }
    # DEV
    location ~ ^/(app_dev|config)\.php(/|$) {
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
    }
    # PROD
    location ~ ^/app\.php(/|$) {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        internal;
    }
    # Phpmyadmin Configurations
        location /phpmyadmin {
            root /usr/share/;
            index index.php index.html index.htm;
            location ~ ^/phpmyadmin/(.+\.php)$ {
                try_files $uri =404;
                root /usr/share/;
                #fastcgi_pass 127.0.0.1:9000;
                #fastcgi_param HTTPS on; # <-- add this line
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME document_root$fastcgi_script_name;
                include fastcgi_params;
        }
       location ~*^/phpmyadmin/(.+\.jpg|jpeg|gif|css|png|js|ico|html|xml|txt))${
                root /usr/share/;
       }
    }

    location /phpMyAdmin {
        rewrite ^/* /phpmyadmin last;
    }

    location ~ \.php$ {
        return 404;
    }

    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;
}

结果,现在用户在地址栏中看到了域名,但并没有带来欢乐——浏览器写了ERR_TOO_MANY_REDIRECTS,并没有显示内容。

据我了解,在某些地方存在递归重定向。除了*/nginx/sites-aviable/example.com,此文件夹中没有其他配置(默认文件已完全注释掉)。

会不会是服务器接收到地址**.***.***.***:80的请求重定向到example.com,而域服务捕捉到请求会重定向到**.***.***.***:80,如此循环?

然后呢?还是本地配置中的某个地方有问题?

UPD 是尝试打开站点一次后的 access.log 文件的内容: (该行重复 9 次,。* - 我的服务器的 IP)

**. ***. ***. *** - - [03/Oct/2017: 11: 59: 07 +0300] "GET / HTTP/1.1" 301 194 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv: 54.0) Gecko/20100101 Firefox/54.0"

更新 2 我试试curl -L -I http://mysite 卷曲结果:

'HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Tue, 03 Oct 2017 09:49:32 GMT
Content-Type: text/html
Content-Length: 154
Connection: keep-alive
Location: http://**.***.***.***  //(my server IP)

HTTP/1.1 301 Moved Permanently
Server: nginx/1.10.3 (Ubuntu)
Date: Tue, 03 Oct 2017 09:49:32 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
Location: http://example.com //(my cite)

....
// some repeats this
....

curl: (52) Empty reply from server'

在我的配置中描述了重定向 301。 为什么会有重定向 302 - 我不知道。这是 DNS 服务的结果吗?

【问题讨论】:

  • 如果刚刚做了这个改动,需要清除浏览器缓存,否则会记住之前的重定向,会产生循环。
  • 是的,我已经清除了缓存,但没有帮助。另外,在使用配置时,我将 301 更改为 302,据我所知,浏览器无法缓存 302。
  • 使用nginx -T 确保nginx 正在正确读取您的配置。查看访问日志以确保您的服务器负责整个重定向周期。检查您的 PHP 应用程序以确保 IP 地址没有硬连线到代码或数据库中。
  • 我看了一下nginx-T,没看出配置有什么问题。这是尝试打开站点一次后access.log文件的内容:(该行重复9次,**.***.***.*** - 我的服务器的IP)**. ***. ***. *** - - [03 / Oct / 2017: 11: 59: 07 +0300] "GET / HTTP / 1.1" 301 194 "-" "Mozilla / 5.0 (X11; Fedora; Linux x86_64; rv: 54.0) Gecko / 20100101 Firefox / 54.0 "我们可以在这个日志上说些什么吗?

标签: redirect nginx dns ip


【解决方案1】:

尝试使用 curl 进行调试:

例如:

curl -L -I http://yoursite

-L 选项将跟随重定向,-I 将仅显示标题。

在输出中搜索HTTP/1.1 301 Moved PermanentlyLocation: htt.....

还可以尝试将您的 conf 更改为使用 http 或 https 在许多情况下是循环发生的地方:

return 301 $scheme://

return 301 https://

【讨论】:

  • 我无法将结果附加到评论中,并编辑了问题,在“UPD”中添加了 curl 查询的结果
【解决方案2】:

错误不在我的服务器端或nginx配置,我有域名时没有正确配置DNS。我没有创建 A 记录,而是将重定向设置为我的服务器的 IP

【讨论】:

    猜你喜欢
    • 2017-05-05
    • 2021-04-28
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 2021-03-25
    相关资源
    最近更新 更多