如何在 apache 反向代理后面将 nginx 从 http 重定向到 https?

如何在 apache 反向代理后面将 nginx 从 http 重定向到 https?

我有一个 wordpress 网站在 nginx(localhost;端口 8081)上运行,位于 apache 2.4 反向代理后面。一切正常。现在我想将所有 http 请求重定向到 https,因此我修改了我的默认 apache 虚拟主机,如下所示:

ProxyPreserveHost On
ProxyRequests Off
# NO SSL - OLD
#ProxyPass / http://localhost:8081/
#ProxyPassReverse / http://localhost:8081/
# SSL - NEW
SSLProxyEngine On
RequestHeader set Front-End-Https "On"
ProxyPass / https://localhost:8081/
ProxyPassReverse / https://localhost:8081/

我的 nginx 配置如下:

server {
    listen 8081 ssl;
    server_name example.com;
    root /home/mywordpress;
    index index.php;

    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

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

    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

    location / {
            try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
            include fastcgi_params;
            fastcgi_intercept_errors on;
            fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
    }

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
}

并添加了此默认 SSL - apache 虚拟主机:

<VirtualHost _default_:443>

    # SSL
    SSLProxyEngine On
    RequestHeader set Front-End-Https "On"
    ProxyPass / https://localhost:8081/
    ProxyPassReverse / https://localhost:8081/

    ProxyPreserveHost On
    ProxyRequests Off

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
</VirtualHost>

Let's encrypt 工作正常。现在,当我转到 http 版本时,我会被重定向到 https 版本,但是...我可以看到文本、链接和条目,但看不到 javascript、css 或图像等静态内容(404 错误)。静态内容仍然可以访问。如果我在浏览器上输入它的非 https url,我可以得到它(200 OK),所以 SSL 和那种内容有些问题。我查找了有关类似问题的信息,但没有找到任何答案。我做错了什么?我快疯了。谢谢。

相关内容