You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从Apache迁移到Nginx后iFrame因混合内容问题无法加载

问题:迁移到Nginx后HTTPS页面因混合内容无法加载iFrame

我正将网站从Apache迁移至Nginx,其中包含iFrame的页面(https://wpdemo.hotjoomlatemplates.com/)在Nginx上因混合内容问题无法加载iFrame,但iFrame的src已设置为HTTPS。该页面在Apache环境的VPS上可正常运行,无混合内容错误,且页面源码中也没有任何HTTP引用。

以下是我的Nginx配置:

server {
  listen 80;
  listen [::]:80;
  listen 443 quic;
  listen 443 ssl;
  listen [::]:443 quic;
  listen [::]:443 ssl;
  http2 on;
  http3 off;
  {{ssl_certificate_key}}
  {{ssl_certificate}}
  server_name wpdemo.hotjoomlatemplates.com;
  {{root}}

  {{nginx_access_log}}
  {{nginx_error_log}}

  if ($scheme != "https") {
    rewrite ^ https://$host$request_uri permanent;
  }

  location ~ /.well-known {
    auth_basic off;
    allow all;
  }

  {{settings}}

  location / {
    {{varnish_proxy_pass}}
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_hide_header X-Varnish;
    proxy_redirect off;
    proxy_max_temp_file_size 0;
    proxy_connect_timeout      720;
    proxy_send_timeout         720;
    proxy_read_timeout         720;
    proxy_buffer_size          128k;
    proxy_buffers              4 256k;
    proxy_busy_buffers_size    256k;
    proxy_temp_file_write_size 256k;
  }

  location ~* ^.+\.(css|js|jpg|jpeg|gif|png|ico|gz|svg|svgz|ttf|otf|woff|woff2|eot|mp4|ogg|ogv|webm|webp|zip|swf|map|mjs)$ {
    add_header Access-Control-Allow-Origin "*";
    add_header alt-svc 'h3=":443"; ma=86400';
    add_header X-Frame-Options "ALLOWALL";
    expires max;
    access_log off;
  }

  location ~ /\.(ht|svn|git) {
    deny all;
  }

  if (-f $request_filename) {
    break;
  }
}

server {
  listen 8080;
  listen [::]:8080;
  server_name wpdemo.hotjoomlatemplates.com;
  {{root}}

  include /etc/nginx/global_settings;

  try_files $uri $uri/ /index.php?$args;
  index index.php index.html;
  
  autoindex off;

  location ~ \.php$ {
    include fastcgi_params;
    fastcgi_intercept_errors on;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    try_files $uri =404;
    fastcgi_read_timeout 3600;
    fastcgi_send_timeout 3600;
    fastcgi_param HTTPS "on";
    fastcgi_param SERVER_PORT 443;
    fastcgi_pass 127.0.0.1:{{php_fpm_port}};
    fastcgi_param PHP_VALUE "{{php_settings}}";
  }

  if (-f $request_filename) {
    break;
  }
}

排查与解决方法
  1. 清空Varnish缓存
    配置中启用了Varnish代理,可能缓存了旧的HTTP内容,执行命令清空缓存后再测试:

    varnishadm "ban req.url ~ .*"
    
  2. 优化HTTPS强制跳转逻辑
    替换原配置中if ($scheme != "https")的判断,改用单独的server块实现强制跳转,避免逻辑漏洞:

    # 单独创建80端口的server块,替换原配置中对应的80监听逻辑
    server {
      listen 80;
      listen [::]:80;
      server_name wpdemo.hotjoomlatemplates.com;
      return 301 https://$host$request_uri;
    }
    
  3. 添加HSTS头强制HTTPS访问
    在443端口的server块中添加HSTS头,强制浏览器始终使用HTTPS,避免潜在的HTTP fallback:

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    
  4. 补充后端PHP的协议识别头
    在8080端口的php location块中添加X-Forwarded-Proto头,确保PHP应用正确识别请求协议:

    fastcgi_param X-Forwarded-Proto https;
    
  5. 扫描页面实际响应内容
    即使源码无HTTP引用,动态生成的内容(如插件、主题输出)可能存在硬编码HTTP链接,用命令扫描:

    curl -s https://wpdemo.hotjoomlatemplates.com/ | grep -i "http://"
    

内容的提问来源于stack exchange,提问作者Milos Stankovic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 10:42:39