Nginx: http, https'ye yönlendiriliyor (hata: çok fazla yönlendirme)

Dec 20 2016

Nginx'i çalıştırıp yeniden yönlendirmeye çalışıyor:

www'dan www olmayanlara

http'den https'ye

Benzer konular olduğunu biliyorum ama hiçbiri aynı senaryoya sahip değil.

Webmin / Virtualmin ve Fast-CGI'yi kurdum. Bu sunucuda birçok hesabım / sitem var. Sitem için bu sunucu bloğudur:

server {
    listen my_server_IP;
    server_name example.com www.example.com;
    return 301 https://example.com$request_uri;
    root /home/example/public_html;
    index index.html index.htm index.php;
    access_log /var/log/virtualmin/example.com_access_log;
    error_log /var/log/virtualmin/example.com_error_log;
    fastcgi_param GATEWAY_INTERFACE CGI/1.1;
    fastcgi_param SERVER_SOFTWARE nginx;
    fastcgi_param QUERY_STRING $query_string;
    fastcgi_param REQUEST_METHOD $request_method;
    fastcgi_param CONTENT_TYPE $content_type;
    fastcgi_param CONTENT_LENGTH $content_length;
    fastcgi_param SCRIPT_FILENAME /home/example/public_html$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    fastcgi_param REQUEST_URI $request_uri;
    fastcgi_param DOCUMENT_URI $document_uri;
    fastcgi_param DOCUMENT_ROOT /home/example/public_html;
    fastcgi_param SERVER_PROTOCOL $server_protocol;
    fastcgi_param REMOTE_ADDR $remote_addr;
    fastcgi_param REMOTE_PORT $remote_port;
    fastcgi_param SERVER_ADDR $server_addr;
    fastcgi_param SERVER_PORT $server_port;
    fastcgi_param SERVER_NAME $server_name;
    fastcgi_param HTTPS $https;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/php-nginx/148180748420424.sock/socket;
    }
    # htaccess
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    #
    listen my_server_IP:443 ssl;
    ssl_certificate /home/example/ssl.cert;
    ssl_certificate_key /home/example/ssl.key;
}

Şimdi, şimdi göründüğü şekliyle yapılandırma, #htaccess biti (Apache'nin .htaccess'inden Wordpress'te "güzel bağlantıları" etkinleştirmek için dönüştürülmüş) ve "dönüş 301" satırı dışında hiç yapmıyorum.

SSL sertifikası, eğer fark yaratıyorsa, LetsEncrypt'den alınır.

Nginx'i kaydettim ve yeniden başlattım.

Chrome'da siteyi isterken bana "ERR_TOO_MANY_REDIRECTS" diyor

Neyi yanlış yapıyorum? Herhangi bir şey eklemem veya kaldırmam gerekiyor mu?

Yanıtlar

3 Drifter104 Dec 20 2016 at 19:30

Bu, mevcut yapılandırmanızın basitleştirilmiş bir sürümüdür (şu anda içerik açısından hiçbir şey yapmayan tüm bitler hariç) bazı notlar eklenmiş, böylece neler olduğunu görebilirsiniz. Son iki satır, işleri başlangıca geri göndermeye devam ediyor. Bu nedenle birçok yeniden yönlendirme hatası.

server {
    listen my_server_IP; #listen on IP x.x.x.x
    listen my_server_IP:443 ssl; #listen on IP x.x.x.x on 443
    server_name example.com www.example.com; #Of the requests on IP x.x.x.x look for those with with one of these headers. send to line below       
    return 301 https://example.com$request_uri; #Send everything to the line above
    } 

İhtiyacın olan şey bu, bir ififade kullanabilirsin ama bu sefer değil, sırf neler olup bittiğini açıkça görebilmen için. Daha fazla satırı var ama işe yarayacak.

server {
    listen my_server_IP;
    server_name example.com www.example.com; #Listen for non-https requests 
    return 301 https://example.com$request_uri; #Send to the correct https address
    }
server {
    listen my_server_IP:443 ssl;
    server_name www.example.com; #Listen for https (www) requests
    return 301 https://example.com$request_uri; #Send to the correct https 
    ssl_certificate /home/example/ssl.cert;
    ssl_certificate_key /home/example/ssl.key;
    }
server {
    listen my_server_IP:443 ssl;
    server_name example.com;
    ssl_certificate /home/example/ssl.cert;
    ssl_certificate_key /home/example/ssl.key;
    <Rest of your config from above, fastcgi etc>
    }