• Kim

    (@khaverblad)


    Spent a fair deal of time to get figure out the proper apache reversed proxy settings to get wordpress fully to work and so far it either ends up with that only main index pagee shows up correct and sub-pages won’t view correctly or doesn’t show at all. So trying to tap into the knowledge heere what the proper tweaks are to get WordPress to work when incoming traffic is coming via Apache proxy server.

    This is the virtual host file from the Apache proxy server:

    <VirtualHost *:80>
      ServerName www.example.com
      Redirect permanent "/" "https://www.example.com/"
    </VirtualHost>
    
    <VirtualHost *:443>
      ServerName www.example.com
    
      ProxyRequests Off
      ProxyPreserveHost On
      ProxyPass / https://192.168.202.52/
      ProxyPassReverse / https://192.168.202.52/
    
      RequestHeader set X-Forwarded-Proto "https"
      RequestHeader set X-Forwarded-Port "443"
    
      SSLCertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
      SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
    </VirtualHost>

    and added the following to code to wp-config.php to check if site is being served over HTTPS after the reverse proxy

    if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
            $_SERVER['HTTPS'] = 'on';
    }

    What is it that I’m missing out on to get the WP site to work together with reverse proxy?

    • This topic was modified 2 years, 8 months ago by Yui. Reason: moved to fixing wordpress
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    If requests are routed to the WP installation, it shouldn’t matter how they got there. If you can get the home page displayed, requests are getting there. Failure to display sub-pages indicates an issue with local server or WP configuration.

    Are you able to access the login page and log into the back end? Back end pages don’t directly involve the CMS, so should work as well as the home page works. If you can log in, change your permalink settings to “Plain”. Sub-pages should display this way if requests are getting to WP.

    If plain permalinks work but “pretty” ones do not, there’s an issue related to the .htaccess file. Either file permissions are wrong or certain modules or configurations are not in place that will permit pretty permalinks to work. I’m assuming Apache server. If something else, it’s still an issue with how pretty permalinks are rewritten.

    I got this EXACT problem figured out, and I finally got all iterations of URL’s to work listed at the bottom.
    Here is the code that works for me.

    Proxy:

    <VirtualHost *:80>
    
       ServerName test.x.com
       ServerAlias www.test.x.com
       Redirect / https://test.x.com/
    
    </VirtualHost>
    
    <VirtualHost *:443>
    
       ServerName test.x.com
       ServerAlias www.test.x.com
    
       Redirect /www.test.x.com/ /test.x.com/
    
       RequestHeader set X-Forwarded-Proto "https"
       RequestHeader set X-Forwarded-Port "443"
    
       <Proxy *>
            Order deny,allow
            Allow from all
       </Proxy>
    
       ProxyPreserveHost On
       SSLProxyEngine on
       ProxyPass / https://192.168.1.19:80/
       ProxyPassReverse / https://192.168.1.19:80/   
    
       SSLEngine on
       Include /etc/letsencrypt/options-ssl-apache.conf
       SSLCertificateFile /etc/letsencrypt/live/x.com-0002/fullchain.pem
       SSLCertificateKeyFile /etc/letsencrypt/live/x.com-0002/privkey.pem
    
    </VirtualHost>

    This is the end server:

    <VirtualHost *:80>
    DocumentRoot /var/www/html
    </VirtualHost>

    In the wp-config.php file, you have to add:

    if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
    	$_SERVER['HTTPS'] = 'on';
    }

    That’s it. That’s the magic formula that finally made everything work:

    https://www.x.test.com
    https://www.x.test.com/wp-admin
    https://x.test.com
    https://x.test.com/wp-admin
    https://www.x.test.com
    https://www.x.test.com/wp-admin
    https://x.test.com
    https://x.test.com/wp-admin

    This is a setup for 4 domains on 4 raspberry Pi’s, one of them performing proxy, the other 3 on their own proxied boxes.

    I hope this helps someone. I found Apache very simple, but so many out there had code that broke when trying to go to /wp-admin or enter www. The last puzzle was to fix the fact that my cert didn’t support www subdomain, therefore the redirect in the :443 section.

    The live site that uses this is test.qso.com and you can see how this code works just as described.

    • This reply was modified 2 years, 8 months ago by n7okn.
    • This reply was modified 2 years, 8 months ago by n7okn.
    • This reply was modified 2 years, 8 months ago by n7okn.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to get WordPress to work behind Apache Reversed Proxy?’ is closed to new replies.