我已经通过另一个域名 (www.bbb.com) 为网络服务 (www.aaa.com) 设置了一个 NGINX 反向代理,同时向后者添加了一些额外的页面。
请求来自 www.bbb.com
(nodejs 应用程序),但它们需要看起来像来自 www.aaa.com
否则我将有一个 CORS (跨源资源共享)问题。因此 NGINX 调整 header 。
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;
但我可能用错了。 有什么指点吗?
另一个 Stackoverflow question 中描述的类似问题
最佳答案
您可以尝试使用 http://nginx.org/r/default_type ;我想到的另一件事是 http://nginx.org/r/proxy_hide_headers .
我还建议首先采取的行动是确定此问题的根本原因——错误的 MIME 类型是否来自您的上游?然后看那里。
否则,我的猜测是您得到 text/html
的原因是您的配置有其他问题,并且 404 Not Found
为您的 .js
文件生成响应(或者甚至可能是上游的 403 Forbidden
,或者 nginx 的 500
),从而导致 text/html
MIME 类型。
关于nginx - 拒绝执行脚本,因为启用了严格的 MIME 类型检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48626459/