2017-01-11 189 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