Apache反向代理背后的WordPress站点不断重定向到localhost

6jjcrrmo  于 2023-01-16  发布在  WordPress
关注(0)|答案(4)|浏览(335)

我尝试使用podman-compose(工作原理类似于docker-compose)在生产服务器上部署容器化的WordPress。Web服务在主机的端口8080上公开。然后,我利用Apache的ProxyPass指令创建反向代理,并将请求发送到WordPress服务器的localhost:8080,从而使WordPress在http://example.com上可用。
我成功地将数据库和wordpress卷迁移到了服务器上。我遇到的第一个问题是,WordPress在wpoptions中将localhost存储为siteurlhome,因此它在生产服务器上无法工作。在将这些值更改为example.com(我的域)后,它一直重定向到localhost。疯了!以下是我的docker-compose.yaml:

version: "3.8"
services: 
  web:
    image: wordpress
    restart: always
    volumes:
      - wordpress:/var/www/html
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: user
      WORDPRESS_DB_NAME: db
      WORDPRESS_DB_PASSWORD: mypass
      WORDPRESS_DEBUG: 0
    depends_on:
      - db
    networks:
      - wpnet
  db:
    image: mariadb:10.5
    restart: always
    ports:
      - 6603:3306

    volumes:
      - wpdbvol:/var/lib/mysql

    environment:
      MYSQL_DATABASE: db
      MYSQL_USER: user
      MYSQL_PASSWORD: mypass
      MYSQL_ROOT_PASSWORD: mypass
    networks:
      - wpnet
volumes:
  wordpress: {}
  wpdbvol: {}

networks:
  wpnet: {}

下面是我的反向代理配置:

<VirtualHost *:80>

ServerName example.com
ServerAlias www.example.com

ProxyPass "/"  "https://localhost:8080"

#Allows modification of Location: headers from backend server to point to the reverse proxy
ProxyPassReverse "/"  "http://localhost:8080"
</VirtualHost>
hgb9j2n6

hgb9j2n61#

在您的情况下,问题可能是在代理传递url的末尾缺少/
在我的情况下,我想把它送达:https://example.com
(wordpress网站5.8.1)
在数据库中(或直接在UI中),我必须更新siteurlhome

update wp_options set option_value="https://example.com" where option_name in ("siteurl", "home");

同时粘贴Apache的反向代理设置(X-Forwarded-ProtoX-Forwarded-Port是关键设置):

<VirtualHost *:443>
  ServerName example.com
  SSLEngine on
  SSLCertificateFile "/sslcert/server.crt"
  SSLCertificateKeyFile "/sslcert/server.key"

   ProxyRequests Off
   KeepAlive Off
   <Proxy *>
      Order deny,allow
      Allow from all
   </Proxy>

   # These are required for https reverse proxy to work with wordpress  (They are read from `wp-config.php`)
   RequestHeader set X-Forwarded-Proto "https"
   RequestHeader set X-Forwarded-Port "443"   

   ProxyPass / http://localhost:8080/
   ProxyPassReverse / http://localhost:8080/
   ProxyPreserveHost On
   ErrorLog "logs/wordpress.revproxy-sslerror_log"
   CustomLog "logs/wordpress.revproxy-sslaccess_log" common
</VirtualHost>

注意:在我的例子中,我已经测试了reverse-proxy是docker-compose的一个服务,所以在上面的设置中,我使用的是http://wpcontainer/而不是http://localhost:8080/

ddhy6vgd

ddhy6vgd2#

这是一个很长的机会,很可能不会工作。
也许可以尝试在compose ymlenvironment``WORDPRESS_CONFIG_EXTRA设置中定义https://example.com作为你的主页url。

services: 
  web:
    image: wordpress
    restart: always
    volumes:
      - wordpress:/var/www/html
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: user
      WORDPRESS_DB_NAME: db
      WORDPRESS_DB_PASSWORD: mypass
      WORDPRESS_DEBUG: 0
      WORDPRESS_CONFIG_EXTRA: |
        
        /** define our home url */
        define('WP_HOME', 'https://example.com');
        define('WP_SITEURL', 'https://example.com');

    depends_on:
      - db
    networks:
      - wpnet

可能强制wordpress安装到它的目标,并阻止它重定向回localhost
另外,当我在本地wordpress环境中遇到ngrok隧道问题时,我不得不安装Relative URL wordpress插件,使我的隧道工作。
不确定这是否与您的问题相关,但可能值得安装Relative URL插件,并在尝试上述WORDPRESS_CONFIG_EXTRA想法之前,看看这是否修复了您的本地主机重定向问题。
这个插件已经有2年没有更新了,但是在ngrok上仍然可以很好地工作。插件实际上是20行php,我猜可以放进你的函数中。
Ngrok允许我通过类似http://example.ngrok.io/的https协议url访问我的本地docker compose wordpress环境,这对我处理本地webhook响应回调(我与stripe一起使用)很方便。

avwztpqn

avwztpqn3#

对于每个人遇到这个与WP运行在ssl:
如果你有Nginx作为Apache的代理,HTTPS全局变量必须在PHP中显式设置(最好在wp-config.php中),否则WP将继续重定向,因为Nginx运行在ssl上,而Apache不是,WP只能看到Apache环境。

tktrz96b

tktrz96b4#

这可能太晚了,但我在使用nginx和apache反向代理时遇到了同样的问题。解决方案非常简单,但同样很难找到根本原因。解决方案:
nginx配置-〉将$request_uri添加到代理传递
代理传递
更多信息请访问:
https://www.digitalocean.com/community/questions/redirect-loop-with-wordpress-on-apache-with-nginx-reverse-proxy-and-https-on-ubuntu-16

相关问题