Chrome 如何设置基于nginx的upgrade-insecur-requests

x7rlezfr  于 2023-06-19  发布在  Go
关注(0)|答案(2)|浏览(311)

我已经将我的站点更改为https,但我在代码中使用了静态文件的cdn。它不能工作,chrome控制台显示这样的错误:

Mixed Content: The page at 'https://a.example.com/static/' was loaded over HTTPS, but requested an insecure stylesheet 'http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css'. This request has been blocked; the content must be served over HTTPS.

我在nginx配置文件中添加了add_header Content-Security-Policy upgrade-insecure-requests;如下:

server {
    listen 80;
    listen 443;
    server_name a.example.com;
    add_header Content-Security-Policy upgrade-insecure-requests;

    if ($scheme != "https") {
       return 301 https://$server_name$request_uri;
       #rewrite ^ https://$server_name$request_uri? permanent;
    }

    ssl on;
    ssl_certificate /etc/nginx/ssl/example.crt;
    ssl_certificate_key /etc/nginx/ssl/example.key;

    ssl_session_timeout 5m;

    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
    ssl_prefer_server_ciphers on;

    gzip on;
    gzip_proxied any;
    gzip_types text/plain application/xml application/json;
    client_max_body_size 8M;
    access_log /var/log/nginx/example.log;
    location / {
            proxy_pass http://10.10.10.110:5000;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header Host $host;
    }
    location ^~ /static/ {

            proxy_pass http://10.10.10.110:8888;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header Host $host;
           #proxy_set_header Content-Security-Policy upgrade-insecure-requests;
    }

}
但现在还没用有人能告诉我怎么解决吗?thx:)

1tuwyuhd

1tuwyuhd1#

请注意,并非所有浏览器都支持upgrade-insecure-requests,例如Safari和IE
我建议你在代码中替换HTTP请求。您可以使用//相对于调用它的协议加载它,如下所示:

//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css

这意味着,如果您从HTTPS上下文打开Web应用程序,它将使用HTTPS协议加载它,否则将使用HTTP。

fafcakar

fafcakar2#

对于其他任何人谁遇到这一点,并希望有一个更好的方式,是guaruntied工作在所有浏览器(虽然一切都应该在理论上支持它现在,除了IE)
包含的文件只是你在每个服务器上重复使用的标准nginx设置,tls是所有的“ssl”配置,你只是按照你想要的方式设置。
任何转到端口80的内容都将永久重定向到HTTPS站点,您还可以添加content-security-policy,如果您有任何不安全的链接,将确保浏览器(如果支持)直接转到httpsurl,防止它向http站点发出额外请求,然后被重定向
此配置还将基本域请求重定向到www。这样你就可以很容易地为静态内容设置无cookie域(只需添加另一个配置,与上一个类似,但用于static.example.com)
理论上应该更有效,具有单独的配置,这样基本域和端口80配置专门用于其任务,并且在主网站上必须进行更少的处理以考虑这些场景。

server {
    listen 80;
    server_name example.com www.example.com;
    include /etc/nginx/common/defaults.conf;

    add_header Content-Security-Policy upgrade-insecure-requests;

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

server {
    listen 443 ssl http2;
    server_name example.com;
    include /etc/nginx/common/defaults.conf;
    include /etc/nginx/common/tls_modern.conf;

    add_header Content-Security-Policy upgrade-insecure-requests;

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

server {
    listen 443 ssl http2;
    server_name www.example.com 127.0.0.1 localhost;
    include /etc/nginx/common/defaults.conf;
    include /etc/nginx/common/tls_modern.conf;

    add_header Content-Security-Policy upgrade-insecure-requests;
    
    index index.html;
    try_files /index.html =404;

    location / {
        root /usr/share/nginx/html/www;
    }
}

相关问题