Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). The web service is exposed on port 8080 on the host. 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. 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. After changing these values to example.com (my domain), IT KEEPS REDIRECTING to localhost. Insane! Here's my 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>
like image 480
djnotes Avatar asked Jun 18 '26 21:06

djnotes


2 Answers

The problem in your case might be that you are missing the / at the end of ProxyPass, ProxyPassReverse urls.

In my case I wanted to serve it on: https://example.com

So, What I did (on wordpress 5.8.1) was:

1. In the database (or ui directly) I had to update both siteurl and home (no trailing / needed there)

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

2. Pasting also the reverse proxy settings for apache (X-Forwarded-Proto and X-Forwarded-Port are crucial settings):

<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 (of step 2) instead of http://localhost:8080/ I was using http://wpcontainer/. But in your case the above should work as is.

like image 115
Jannis Ioannou Avatar answered Jun 21 '26 10:06

Jannis Ioannou


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.

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.




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.

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.

The plugin has not been updated in 2 years but still works fine with ngrok. Plugin is literally 20 lines of php which I guess could be dropped into your functions.

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

like image 44
joshmoto Avatar answered Jun 21 '26 11:06

joshmoto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!