拒绝执行脚本,因为启用了严格的 MIME 类型检查
Refused to execute script because strict MIME type checking is enabled
我已经通过另一个域名 (www.bbb.com) 为 Web 服务 (www.aaa.com) 设置了一个 NGINX 反向代理,同时向后者添加了一些额外的页面。
请求来自 www.bbb.com
(nodejs 应用程序),但它们需要看起来像是来自 www.aaa.com
,否则我将遇到 CORS(Cross-origin 资源共享)问题。因此 NGINX 调整 headers.
NGINX 配置
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
include servers/*;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 443 ssl;
server_name www.bbb.com;
ssl_certificate /etc/pki/nginx/server.crt;
ssl_certificate_key /etc/pki/nginx/private/server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_set_header X-Real-IP 127.0.0.1;
proxy_set_header Host www.aaa.com;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass https://www.aaa.com;
proxy_http_version 1.1;
}
}
}
似乎其中一个资源没有通过反向代理获得正确的 MIME 类型
https://www.aaa.com/scripts/some.script.api.js
错误
Refused to execute script from 'https://www.bbb.com/scripts/some.script.api.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
text/html
确实不正确,我会期待 application/javascript
1.为什么会这样?
我认为 MIME 类型是自动设置的?会不会是 www.aaa.com 实施了一些新的 CORS 安全规则?
2。有没有办法告诉 NGINX 为该特定文件设置正确的 MIME 类型?我尝试了一些方法都无济于事。
例如
add_header Content-Type application/javascript;
但我可能用错了。
有什么指点吗?
另一个 Whosebug question
中描述的类似问题
您可以尝试使用 http://nginx.org/r/default_type; another thing that comes to mind would be http://nginx.org/r/proxy_hide_headers。
我还建议首先采取的行动是确定此问题的根本原因 — 错误的 MIME 类型是否来自您的上游?那就看那里。
否则,我的猜测是您获得 text/html
的原因是您的配置存在其他问题,并且为您的 .js
生成了 404 Not Found
响应文件(或者甚至 403 Forbidden
由上游,或 500
由 nginx),从而导致 text/html
MIME 类型。
我已经通过另一个域名 (www.bbb.com) 为 Web 服务 (www.aaa.com) 设置了一个 NGINX 反向代理,同时向后者添加了一些额外的页面。
请求来自 www.bbb.com
(nodejs 应用程序),但它们需要看起来像是来自 www.aaa.com
,否则我将遇到 CORS(Cross-origin 资源共享)问题。因此 NGINX 调整 headers.
NGINX 配置
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
include servers/*;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 443 ssl;
server_name www.bbb.com;
ssl_certificate /etc/pki/nginx/server.crt;
ssl_certificate_key /etc/pki/nginx/private/server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_set_header X-Real-IP 127.0.0.1;
proxy_set_header Host www.aaa.com;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass https://www.aaa.com;
proxy_http_version 1.1;
}
}
}
似乎其中一个资源没有通过反向代理获得正确的 MIME 类型 https://www.aaa.com/scripts/some.script.api.js
错误
Refused to execute script from 'https://www.bbb.com/scripts/some.script.api.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
text/html
确实不正确,我会期待 application/javascript
1.为什么会这样? 我认为 MIME 类型是自动设置的?会不会是 www.aaa.com 实施了一些新的 CORS 安全规则?
2。有没有办法告诉 NGINX 为该特定文件设置正确的 MIME 类型?我尝试了一些方法都无济于事。
例如
add_header Content-Type application/javascript;
但我可能用错了。 有什么指点吗?
另一个 Whosebug question
中描述的类似问题您可以尝试使用 http://nginx.org/r/default_type; another thing that comes to mind would be http://nginx.org/r/proxy_hide_headers。
我还建议首先采取的行动是确定此问题的根本原因 — 错误的 MIME 类型是否来自您的上游?那就看那里。
否则,我的猜测是您获得 text/html
的原因是您的配置存在其他问题,并且为您的 .js
生成了 404 Not Found
响应文件(或者甚至 403 Forbidden
由上游,或 500
由 nginx),从而导致 text/html
MIME 类型。