【问题标题】:NGINX Redirect to HTTPS only works after refreshNGINX 重定向到 HTTPS 仅在刷新后有效
【发布时间】:2017-03-07 19:31:57
【问题描述】:

我希望流量转到https://example.com。 不允许使用 www 前缀,并且 需要 SSL。

我们遇到的问题是,许多(尽管不是全部)首次访问者在点击刷新之前不会被重定向到 HTTPS。

您是否在我的配置中看到任何允许这种行为的内容?

server {
    listen 80;
    listen 443 ssl;

    server_name www.example.com;

    return 301 https://example.com$request_uri;
}

server {
    listen 80;
    listen 443 ssl;

    server_name example.com;

    root /var/www/html/mm;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Forwarded-Port 443;
    proxy_set_header Host $http_host;

    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";

    client_max_body_size 200m;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
        index index.php index.html index.htm install.php;
        client_max_body_size 200m;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        #fastcgi_pass unix:/var/run/php-fpm/www.sock;
        fastcgi_pass   php-fpm;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_param PHP_VALUE "upload_max_filesize = 150M \n upload_max_filesize=151M";
        fastcgi_param PHP_VALUE "post_max_size = 150M \n post_max_size=151M";
        include fastcgi_params;
    }

【问题讨论】:

  • 如果人们在没有www. 的情况下进入,你永远不会重定向 http -> https。刷新后,由于 HSTS 标头,它可以工作。
  • 在第一个块中,return 301 https://example.com$request_uri; 是重定向。
  • 是的,如果人们输入 with www.
  • 请使用正确的配置添加答案,如果可行,我会给你功劳。
  • 不确定它是否对未来的读者有帮助,因为它基本上归结为“这个问题是由一个无法再复制的问题或一个简单的印刷错误引起的。虽然类似的问题可能是在这里,这个问题的解决方式不太可能帮助未来的读者。这通常可以通过在发布之前识别并仔细检查重现问题所需的最短程序来避免。”

标签: ssl nginx https configuration


【解决方案1】:

正如@PeeHaa 所提到的,对于 www.example.com,您缺少从 http 到 https 的重定向。试试这个,我重新安排了服务器块,以将 HSTS 标头添加到 www 服务器并解决潜在的安全错误配置,其中http://www 直接重定向到https://(notwww)(根据https://wiki.mozilla.org/Security/Guidelines/Web_Security#HTTP_Redirections):

# HTTP server (non-www) -- redirect to https://example.com
server {
    listen 80;
    server_name example.com;
    return 301 https://example.com$request_uri;
}

# HTTP server (www) -- redirect to https://www.example.com
server {
    listen 80;
    server_name www.example.com;
    return 301 https://www.example.com$request_uri;
}

# HTTPS server (www) -- redirect to https://example.com -- Add HSTS header
server {
    listen 443 ssl;
    server_name www.example.com;
    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
    return 301 https://example.com$request_uri;
}

# HTTPS server (non-www)
server {
    listen 80;
    listen 443 ssl;

    server_name example.com;

    root /var/www/html/mm;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Forwarded-Port 443;
    proxy_set_header Host $http_host;

    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";

    client_max_body_size 200m;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
        index index.php index.html index.htm install.php;
        client_max_body_size 200m;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        #fastcgi_pass unix:/var/run/php-fpm/www.sock;
        fastcgi_pass   php-fpm;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_param PHP_VALUE "upload_max_filesize = 150M \n upload_max_filesize=151M";
        fastcgi_param PHP_VALUE "post_max_size = 150M \n post_max_size=151M";
        include fastcgi_params;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 2018-01-02
    • 2020-09-11
    • 2018-04-27
    • 2020-03-21
    相关资源
    最近更新 更多