首页 文章

无法将请求从IP重定向到域名(nginx) . 得到了“ERR_TOO_MANY_REDIRECTS”

提问于
浏览
0

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

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"

UPD 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服务的结果吗?

2 回答

  • 0

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

  • 0

    尝试使用 curl 进行调试:

    例如:

    curl -L -I http://yoursite
    

    选项 -L 将遵循重定向, -I 将仅显示 Headers .

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

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

    return 301 $scheme://
    

    return 301 https://
    

相关问题