将www和非www重定向到https非www

时间:2016-03-22 22:02:08

标签: redirect nginx

所以我正在尝试这个,将http www和非www重定向到https non www。

但我只是注意到重定向仅适用于chrome,另一方面它们只是在尝试一段时间后死亡,无法打开页面。我认为在chrome工作中因为过去的缓存,所以它实际上不起作用......任何想法?

    server {
        listen 80;
        server_name www.domain.com domain.com;
        return 301 $scheme://domain.com$request_uri;
        if ($scheme = http) {
                    return 301 https://$server_name$request_uri;
            }

    }



    server {

            listen 443 default_server ssl http2;

            server_name domain.com;
            access_log  off;
            ssl_certificate /etc/ssl/private/cert_chain.crt;
            ssl_certificate_key /etc/ssl/private/server.key;
            ssl_session_cache   shared:SSL:10m;
            ssl_session_timeout 10m;
            ssl_prefer_server_ciphers On;
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
            ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS;

              if ($allowed_country = no) {
                return 444;
    }

 }

这是一个curl -i结果,似乎有一个重定向...

HTTP/1.1 301 Moved Permanently
Server: nginx/1.9.12
Date: Tue, 22 Mar 2016 22:10:34 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://domain.com/

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.9.12</center>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

创建server块来处理重定向。

server {
    listen       80;
    server_name  www.domain.com domain.com;
    return 301   https://www.domain.com$request_uri;
}
server {
    listen 443   ssl http2;
    server_name  www.domain.com;

    # Add ssl_<*> directives here, if necessary.

    return 301   https://domain.com$request_uri;
}
server {
    listen       443 ssl http2;
    server_name  domain.com;

    # Add ssl_<*> directives here, if necessary.
    # And other configurations...
}