我在同一个 Nginx 服务器上有两个站点。
网站 www.example.com 包含 HTTP 标头,但不包含 analytics.example.com
当我访问 analytics.example.com 网站时,控制台中出现一条错误消息:
混合内容:位于 'https://analytics.s1biose.com/' 已通过 HTTPS 加载,但尝试连接到不安全的 WebSocket 端点 'ws://analytics.example.com:7890/'。此请求已被阻止;此端点必须通过 WSS 提供。
我认为显示此消息是因为 HTTP 标头:
add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
来自 www.example.com
但是为什么 analytics.example.com 从 www.example.com 继承了 HTTP 标头?
HTTP 标头应应用于 www.example.com,但不应用于 analytics.example.com,因为这两个站点完全不同。
如何纠正这个问题?
www.example.com
server {
listen 80 default_server;
listen [::]:80 ipv6only=on default_server;
server_name example.com www.example.com;
location ^~ /.well-known/acme-challenge/ {
default_type "text/plain";
root /var/www/letsencrypt;
}
location / {
return 301 https://www.example.com$request_uri;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2 ipv6only=on;
server_name www.example.com;
root /var/www/www-example-com/web;
index index.php;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;
brotli on;
brotli_comp_level 6;
brotli_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;
expires 1209600s;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~* \.(txt|log)$ {
deny all;
}
location ~ \..*/.*\.php$ {
return 403;
}
location ~ ^/sites/.*/private/ {
return 403;
}
location ~ ^/sites/[^/]+/files/.*\.php$ {
deny all;
}
location ~* ^/.well-known/ {
allow all;
}
location ~ (^|/)\. {
return 403;
}
location / {
try_files $uri /index.php?$query_string;
}
location @rewrite {
rewrite ^/(.*)$ /index.php?q=$1;
}
location ~ /vendor/.*\.php$ {
deny all;
return 404;
}
location ~ '\.php$|^/update.php' {
expires off;
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
include fastcgi_params;
fastcgi_param HTTP_PROXY "";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param QUERY_STRING $query_string;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
location ~ ^/sites/.*/files/styles/ {
try_files $uri @rewrite;
}
location ~ ^(/[a-z\-]+)?/system/files/ {
try_files $uri /index.php?$query_string;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
try_files $uri @rewrite;
expires max;
log_not_found off;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
location / {
return 301 https://www.example.com$request_uri;
}
}
analytics.example.com
server {
listen 80;
listen [::]:80;
server_name analytics.example.com;
location / {
return 301 https://analytics.example.com$request_uri;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name analytics.example.com;
root /var/www/analytics-example-com/web;
index report.html;
auth_basic "Protected";
auth_basic_user_file /var/www/analytics-example-com/web/.htpasswd;
ssl_certificate /etc/letsencrypt/live/analytics.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/analytics.example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
答案1
server { }
块之间没有继承。
即使您没有发送任何内容安全策略标头,许多浏览器也会默认阻止混合内容。
大多数情况下,某个http://
或ws://
某个资源(您的具体情况)会被阻止在https://
网站上加载。
nginx 与此无关。只需在应用程序的 HTML 输出中更新对其安全等效项 ( wss://
, ) 的任何引用即可。https://