2017-01-11 270 views
3

我有一個網站在LEMP堆棧上運行。我已經在網站上啓用了cloudflare。我正在爲https使用cloudflare靈活的SSL證書。當我用chrome打開網站時,它顯示的網站被重定向了太多次,並且在firefox中檢測到服務器以永遠不會完成的方式重定向該地址的請求。我試圖看到其他問題的答案,但他們似乎都沒有解決問題。 NGINX conf文件: -HTTP到HTTPS Nginx太多重定向

server { 
listen 80 default_server; 
listen [::]:80 default_server; 
server_name mydomain.com www.mydomain.com; 
return 301 https://$server_name$request_uri; 
} 
server { 
listen 443 ssl http2 default_server; 
listen [::]:443 ssl http2 default_server; 
root /var/www/html; 

index index.php index.html index.htm index.nginx-debian.html; 

location/{ 
    try_files $uri $uri/ =404; 
} 

location ~ \.php$ { 
    include snippets/fastcgi-php.conf; 
    fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
} 

location ~ /\.ht { 
    deny all; 
} 
} 

如果有人能指出我做錯了什麼,我將非常感激。

+0

我假設你正在使用CloudFlare的靈活的SSL選項HTTPS投放內容,你沒有一個安全的服務器阻止你的Nginx配置。看看https://serverfault.com/questions/653976/redirect-loop-using-cloudflares-flexible-ssl/654018#654018 –

回答

8

由於您使用CloudFlare的靈活的SSL您的nginx的配置文件,無線本地環路是這樣的: -

server { 
    listen 80 default_server; 
    listen [::]:80 default_server; 
    server_name mydomain.com www.mydomain.com; 

    if ($http_x_forwarded_proto = "http") { 
     return 301 https://$server_name$request_uri; 
    } 

    root /var/www/html; 

    index index.php index.html index.htm index.nginx-debian.html; 

    location/{ 
    try_files $uri $uri/ =404; 
    } 

    location ~ \.php$ { 
    include snippets/fastcgi-php.conf; 
    fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
    } 

    location ~ /\.ht { 
    deny all; 
    } 
} 
+0

非常感謝你解決了我的問題。 –

+0

This Works,thanks lot –

+0

如果我切換/繞過cloudflare,那麼問題中提到的配置工作。假設我們已經允許SSL證書加密的場景,我們是否可以在兩種情況下都有一個配置? – Sandeep