简体   繁体   English

Apache 后面的 WordPress 站点反向代理不断重定向到本地主机

[英]WordPress site behind Apache reverse proxy keeps redirecting to localhost

I am trying to deploy a containerized Wordpress on a production server using podman-compose (works similar to docker-compose).我正在尝试使用 podman-compose(类似于 docker-compose)在生产服务器上部署容器化 Wordpress。 The web service is exposed on port 8080 on the host. web 服务暴露在主机的 8080 端口上。 Then, I'm leveraging Apache's ProxyPass directive to create a reverse proxy and send requests to localhost:8080 where WordPress is being served, thus making WordPress available on http://example.com . Then, I'm leveraging Apache's ProxyPass directive to create a reverse proxy and send requests to localhost:8080 where WordPress is being served, thus making WordPress available on http://example.com .
I successfully managed to migrate database and wordpress volumes to the server.我成功地将数据库和 wordpress 卷迁移到服务器。 The first problem I had was that WordPress stores localhost as siteurl and home in wpoptions so it will not work on the production server.我遇到的第一个问题是 WordPress 将 localhost 存储为siteurl并将home存储在 wpoptions 中,因此它无法在生产服务器上运行。 After changing these values to example.com (my domain), IT KEEPS REDIRECTING to localhost.将这些值更改为 example.com(我的域)后,它保持重定向到 localhost。 Insane.疯狂的。 Here's my docker-compose:yaml:这是我的 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: {}

And here's my reverse proxy configuration:这是我的反向代理配置:

<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>

This is a long shot and more than likely won't work.这是一个很长的镜头,很可能不会奏效。

Maybe try defining https://example.com as your home url in the compose yml environment WORDPRESS_CONFIG_EXTRA settings.也许尝试在撰写 yml environment 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

This might force the wordpress installation to it's destination and stop it redirecting back to localhost .可能会强制 wordpress 安装到它的目的地并停止它重定向回 l localhost




Also when I was having issues getting ngrok tunnelling to hit my local wordpress environment, I had to install the Relative URL wordpress plugin which got my tunnel working.此外,当我遇到问题让ngrok 隧道访问我的本地 wordpress 环境时,我必须安装相对 URL wordpress 插件,这让我的隧道工作。

Not sure if this is relevant to your question but might be worth installing Relative URL plugin and seeing if this fixes your localhost redirect issue before trying the above WORDPRESS_CONFIG_EXTRA idea.不确定这是否与您的问题有关,但可能值得安装相对 URL插件,并在尝试上述WORDPRESS_CONFIG_EXTRA想法之前查看这是否解决了您的本地主机重定向问题。

The plugin has not been updated in 2 years but still works fine with ngrok .该插件已 2 年未更新,但仍可与ngrok 正常工作。 Plugin is literally 20 lines of php which I guess could be dropped into your functions.插件实际上是 20 行 php 我猜可以放入你的函数中。

Ngrok allows me to access my local docker compose wordpress environment through a https protocol url like http://example.ngrok.io/ , which is handy for me to handle local webhook response callbacks (which I use with stripe). Ngrok allows me to access my local docker compose wordpress environment through a https protocol url like http://example.ngrok.io/ , which is handy for me to handle local webhook response callbacks (which I use with stripe).

For everyone that encounters this with WP running over ssl:对于在 WP 上运行 ssl 时遇到此问题的每个人:

If you have Nginx as a proxy for Apache, the HTTPS global has to be explicitly set in PHP (preferable in wp-config.php) with $_SERVER['HTTPS'] = 'On'; If you have Nginx as a proxy for Apache, the HTTPS global has to be explicitly set in PHP (preferable in wp-config.php) with $_SERVER['HTTPS'] = 'On'; , otherwise WP will keep redirecting, because Nginx runs on ssl, but Apache isn't and WP only sees the Apache environment. , otherwise WP will keep redirecting, because Nginx runs on ssl, but Apache isn't and WP only sees the Apache environment.

This might be too late, but i hit on the same problem while using nginx and apache reverse proxy.这可能为时已晚,但我在使用 nginx 和 apache 反向代理时遇到了同样的问题。 Solution was very easy, but on the same very hard to find root cause.解决方案很容易,但同样很难找到根本原因。 Solution:解决方案:

nginx config-> add $request_uri to the proxy_pass nginx 配置-> 将 $request_uri 添加到 proxy_pass

proxy_pass http://127.0.0.1:8080$request_uri; proxy_pass http://127.0.0.1:8080$request_uri;

More information here:更多信息在这里:

https://www.digitalocean.com/community/questions/redirect-loop-with-wordpress-on-apache-with-nginx-reverse-proxy-and-https-on-ubuntu-16 https://www.digitalocean.com/community/questions/redirect-loop-with-wordpress-on-apache-with-nginx-reverse-proxy-and-https-on-ubuntu-16

The problem in your case might be that you are missing the / at the end of proxy pass urls.您的问题可能是您在代理传递 URL 的末尾缺少/

In my case I wanted to serve it on: https://example.com就我而言,我想将其提供给: https://example.com

(wordpress 5.8.1) (wordpress 5.8.1)

In the database (or ui directly) I had to update both siteurl and home在数据库(或直接 ui)中,我必须同时更新siteurlhome

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

Pasting also the reverse proxy settings for apache ( X-Forwarded-Proto and X-Forwarded-Port are crucial settings):还粘贴 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>

Note: In my case I have tested it with reverse-proxy being a service of docker-compose so in the above settings instead of http://localhost:8080/ I was using http://wpcontainer/ .注意:在我的情况下,我已经使用反向代理作为 docker-compose 的服务对其进行了测试,因此在上述设置中而不是http://localhost:8080/我使用的是http://wpcontainer/ But in your case the above should work as is.但在您的情况下,上述内容应该按原样工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM