来自(VPS)带有终止 SSL 的 nginx 反向代理和(HOME)nginx wordpress 的混合内容/CSP 问题

问题描述 投票:0回答:1

已经几天了,我试图弄清楚我做错了什么。

DNS指向VPS 在 VPS 上,我有带有 ssl 终止功能的 nginx 反向代理,可将请求转发到家庭服务器 在家庭服务器上,我有没有 ssl 的 nginx 并安装了 Wordpress(这是来自其他域上同一页面的镜像,这是一个镜像页面,所以我将人们重定向到新服务器)

当我在浏览器中输入 URL 时,我会看到由于混合内容和 CORS 而导致请求被阻止的损坏站点。

我肯定做错了什么,nginx 只是做了我告诉他的事情,但我不知道我在哪里犯了错误。

server {
    listen 80;
    listen [::]:80;

    server_name mypage.dns;
    # rewrite ^(.*) https://$host$1 permanent;
    return 301 https://$host$request_uri; 
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name     mypage.dns;

    location / {
        add_header X-Served-By $host;
        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_set_header X-Forwarded-Proto https;
        # proxy_set_header X-Forwarded-Ssl https;
        # proxy_set_header X-Url-Scheme https;
        proxy_set_header X-Forwarded-Port 443;
        proxy_set_header Proxy "";
        proxy_pass         http://172.16.100.100;
        proxy_redirect off;
        # proxy_redirect http://172.16.100.100/ https://$host/;
        # proxy_pass_request_headers      on;
    }
    ssl_certificate /etc/letsencrypt/live/mypage.dns/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mypage.dns/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

接下来,我使用之前作为独立 www 工作的配置 - 我希望它能够作为原始页面的镜像。

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name     mypage.dns;
    keepalive_timeout 10;
    keepalive_disable msie6;
    keepalive_requests 200;

    include snippets/csp.conf;

    location / {
        #add_header X-Forwarded-Proto https;
        gzip on;
        gzip_static on;
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        #add_header X-Forwarded-Proto https;

        include fastcgi_params;
        fastcgi_intercept_errors on;

        gzip on;

        fastcgi_cache MYPAGE;
        fastcgi_cache_valid 200 301 302 10h;
        fastcgi_cache_valid 404 5m;
        fastcgi_cache_bypass $no_cache;
        fastcgi_no_cache $no_cache;
        fastcgi_cache_lock on;
        fastcgi_cache_lock_age 5s;
        fastcgi_cache_lock_timeout 5s;
        fastcgi_cache_use_stale error timeout updating invalid_header http_500 http_503;
        fastcgi_cache_min_uses 1;

        fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
        add_header X-Cache-Status $upstream_cache_status;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        fastcgi_read_timeout 600s;
        fastcgi_pass unix:/var/www/mypage.dns/php/php-mypage.sock;
    }
    location ~* .(webp|avif|webm|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        sendfile           on;
        sendfile_max_chunk 1m;
        tcp_nopush on;
        expires max;
        log_not_found off;
        access_log off;

        include snippets/csp.conf;
        add_header Cache-Control public;
        open_file_cache                 max=10000 inactive=12h;
        open_file_cache_valid           5m;
        open_file_cache_min_uses        1;
        open_file_cache_errors          off;
    }
}

还有 snippets/csp.conf

set $cors_origin "";
set $cors_cred   "";
set $cors_header "";
set $cors_method "";

if ($http_origin ~ '^https?://(mypage\.dns|cdn\.mypage\.dns|otherpage\.dns)$') {
    set $cors_origin $http_origin;
    set $cors_cred   true;
    set $cors_header $http_access_control_request_headers;
    set $cors_method $http_access_control_request_method;
}

add_header Access-Control-Allow-Origin      $cors_origin;
add_header Access-Control-Allow-Credentials $cors_cred;
add_header Access-Control-Allow-Headers     $cors_header;
add_header Access-Control-Allow-Methods     $cors_method;

add_header X-Frame-Options "SAMEORIGIN";

add_header X-XSS-Protection "1; mode=block";

add_header X-Content-Type-Options nosniff;
add_header Referrer-Policy "no-referrer-when-downgrade";

还有我从 WordPress 添加到 wp-config.php 的代码。

if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
    $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
}
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
    if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
        $_SERVER['HTTPS'] = 'on';
    }
}

define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST'] );
define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST'] );

看着它是有道理的,每个教程/设置指南/知道如何,告诉我只将标头添加到反向代理中,在 wp-config 中添加或不添加一些行,并且应该完成。

我正在使用curl /浏览器以隐身方式检查

每次我尝试使用curl时:

curl -H 'Pragma: no-cache' https://mypage.dns
我看到源代码中的http没有根据PROTO/SCHEMA更改为https。

我了解我是: 客户端 <---- ssl/443 ---> 反向代理 <---- http/80 ----> nginx WordPress 所以wordpress看到http,但根据我的理解,它应该被

$_SERVER['HTTPS'] = 'on'
覆盖 即使使用 if 逻辑并且仅使用:
$_SERVER['HTTPS'] = 'on'
我没有看到源代码有任何变化,据我所知,这是我遇到的主要问题。

我尝试添加或编辑 nginx confs、带有强制 ssl 的 wp-config、一些“神奇”或不直观的代理参数...

我尝试了 stackoverflow 的推荐帖子

我希望通过反向代理成功运行页面,而不会出现混合内容/csp 错误。

我注意到的唯一一件事是,当我尝试访问 https://mypage.dns/wp-admin/ 时,它会被重定向到镜像构成的原始页面 url - 这是另一个问题(可能是 sql 替换)或网址?)。

wordpress nginx ssl cors reverse-proxy
1个回答
0
投票
fastcgi_cache

对于任何人(也许是几年后的某个人或更可能是我)我启用了 fastcgi_cache,这样通过 php 生成的 WordPress 页面就会被缓存并快速交付....

因此,测试某些功能是否正常工作最好在禁用任何缓存机制的情况下完成。

TL;DR:在测试任何更改之前关闭缓存。

© www.soinside.com 2019 - 2024. All rights reserved.