Nginx SSL 配置 301 http 转 https 的问题

Nginx SSL 配置 301 http 转 https 的问题

我的默认服务器配置是:

# redirection http://example.org vers https//example.org
#
server {
    server_name example.org;
    return 301 https://example.org$request_uri;
}

# redirection http ou https www.example.org vers https ou https example.org
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;
        include snippets/ssl-example.org.conf;
        include snippets/ssl-params.conf;

    server_name www.example.org;
    return 301 $scheme://example.org$request_uri;
}

#bloc server https://example.org
#
server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;

    include snippets/ssl-example.org.conf;
    include snippets/ssl-params.conf;

    server_name example.org;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;

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

        location ~/.well-known {
            allow all;
        }
}

直接访问https://example.org或者https://www.example.org很好

但是当我尝试 http 访问时 http://example.org或者http://www.example.org,我得到

This page isn’t working
example.org didn’t send any data.
ERR_EMPTY_RESPONSE

我用 curl 检查过了

$ curl -Iv http://example.org
* Rebuilt URL to: http://example.org/
*   Trying 138.197.100.98...
* TCP_NODELAY set
* Connected to example.org (138.197.100.98) port 80 (#0)
> HEAD / HTTP/1.1
> Host: example.org
> User-Agent: curl/7.51.0
> Accept: */*

无 301 返回

我的 ssl-params.conf 文件是:

# from https://cipherli.st/
# and https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
ssl_ecdh_curve secp384r1;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
 # Disable preloading HSTS for now.  You can use the commented out header line that includes
# the "preload" directive if you understand the implications.
 #add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains";
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;

ssl_dhparam /etc/ssl/certs/dhparam.pem;

我的配置有什么问题?注意:我正在使用 AVAST 防病毒软件,Chrome 版本 59.0.3071.115(官方版本)(64 位)或 Safari 版本 10.1.1 (12603.2.4)。同样的问题

答案1

我认为你应该在这里添加“listen”指令

server {
    server_name example.org;
    return 301 https://example.org$request_uri;
}

并在此处将 '$scheme' 替换为 'https'(因为如果 $scheme == http,您将重定向到 http。这有什么意义?)

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;
        include snippets/ssl-example.org.conf;
        include snippets/ssl-params.conf;

    server_name www.example.org;
    return 301 $scheme://example.org$request_uri;
}

相关内容