![让 Wordpress+Apache 在 nginx 反向代理后面工作](https://linux22.com/image/772525/%E8%AE%A9%20Wordpress%2BApache%20%E5%9C%A8%20nginx%20%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86%E5%90%8E%E9%9D%A2%E5%B7%A5%E4%BD%9C.png)
我正在尝试设置以下内容:
- localhost 上的 nginx docker 容器作为 Wordpress 的反向代理,监听 localhost:80。它还用作其他微服务的反向代理。
- docker-compose 容器包含 Wordpress+Apache,监听 localhost:4261。docker-compose.yaml 取自官方示例。‘ports’ 指令已从“8000:80”更改为“4261:80”。
我尝试了多种指南和故障排除程序,但 nginx 一直响应“111 连接被拒绝”。类似问题(对我不起作用):这里和这里。
有任何想法吗?
nginx配置:
server {
# Listen HTTPS
listen [::]:443 ssl ipv6only=on;
listen 443 ssl;
server_name ${NGINX_URL};
ssl_certificate /certs/certificate.pem;
ssl_certificate_key /certs/private.key;
location /wp/ {
rewrite ^/wp/?(.*)$ /$1 break;
proxy_pass http://localhost:4261/;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $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 $scheme;
proxy_set_header X-Forwarded-Port 443;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass_request_headers on;
}
...
}
wp-config.php:
...
// If we're behind a proxy server and using HTTPS, we need to alert WordPress of that fact
// see also https://wordpress.org/support/article/administration-over-ssl/#using-a-reverse-proxy
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false) {
$_SERVER['HTTPS'] = 'on';
}
...
答案1
你的 nginx 正在尝试连接到 localhost里面容器。您必须使用容器的主机网络,或者您需要链接 nginx 容器到 WordPress 容器。
在 docker-compose 文件中将是:
external_links:
- wordpress:wordpress
然后你可以使用
proxy_pass http://wordpress/
在您的 nginx 配置中。您不需要在 WordPress 容器中公开任何端口。