Nginx https reverse proxy to Wordpress with Apache, http and different port

3 minute read

Today I had to hide a Wordpress 3.8.1 blog behind an Nginx reverse proxy configured to use only https. Nginx was behind an external firewall which forwarded to Nginx using https (port 4443). The difficulty was that whe Wordpress blog was installed in an Apache HTTP server in port 80 and it worked using http!. Lets say that my site was and I had to put the blog in Thus my list of burdens was the following:

  1. Configure Nginx to use only https and redirect http to https.
  2. Reverse proxy in Nginx from to in Apache.
  3. Force Wordpress links to use port 8080

The following is the diagram summarised my server configuration:

      |  Firewall    |
      | | 
      +–––––––––+                  +––––––––––––––+
      |  Nginx  |      http        |  Wordpress   |
      | (https) |+---------------->| Apache HTTP  |
      |         |                  | (port 80)    |
      +–––––––––+                  +––––––––––––––+ 

So you will say: “Are you completely insane ??!!” I was asked to do it! That is my answer!

Finally after one hour of searching I used a combination of two support articles (post1, post2), some PHP knowledge and experience and a little of imagination, I created a solution:

Configure Nginx to reverse proxy all requests to /blog

This is a fragment of my /etc/nginx/sites-available/default-ssl Nginx configuration file:

upstream blog-webservers {

# redirect http to https
server {
    listen 80;
    rewrite ^(.*) https://$host$1 permanent;

server {
    listen *:443;

    ssl on;
    ssl_certificate     /etc/nginx/crypto/server.crt;
    ssl_certificate_key /etc/nginx/crypto/server.key;

    ssl_client_certificate /etc/nginx/crypto/ca.crt;
    ssl_verify_client optional;
    ssl_verify_depth 10;

    # pem key asking for password problem

    access_log  /var/log/nginx/ssl.access.log;
    error_log   /var/log/nginx/ssl.error.log;
    error_page  404 /404.html;

    # reverse proxy to blog web servers
    location /blog { 
        proxy_pass http://blog-webservers/wordpress/;
        proxy_redirect https://server_name http://blog-webservers/wordpress;
        proxy_read_timeout       3500;
        proxy_connect_timeout    3250;

        proxy_set_header   X-Real-IP          $remote_addr;
        proxy_set_header   Host               $host;
        proxy_set_header   X-Forwarded-For    $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto  https;
        proxy_set_header   SSL_PROTOCOL $ssl_protocol;
        proxy_set_header   SSL_CLIENT_CERT $ssl_client_cert;
        proxy_set_header   SSL_CLIENT_VERIFY $ssl_client_verify;
        proxy_set_header   SSL_SERVER_S_DN $ssl_client_s_dn;

Configure Wordpress wp-config.php

I added the following lines on top of wp-config.php file:

// If Wordpress is behind reverse proxy 
// which proxies https to http
if ( (!empty( $_SERVER['HTTP_X_FORWARDED_HOST'])) ||
     (!empty( $_SERVER['HTTP_X_FORWARDED_FOR'])) ) { 


    define('WP_HOME', '');
    define('WP_SITEURL', '');

    // rewrite blog word with wordpress
    $_SERVER['REQUEST_URI'] = str_replace("wordpress", "blog",

    $_SERVER['HTTPS'] = 'on';

If you want to debug the former PHP code fragment you can print $_SERVER variables before and after configuration changing command inside the if block:

echo '<br>Before:';
echo '<br>$_SERVER[\'HTTP_HOST\'] : ' . $_SERVER['HTTP_HOST'];
echo '<br>$_SERVER[\'REQUEST_URI\']: ' . $_SERVER['REQUEST_URI'];
echo '<br>$_SERVER[\'HTTPS\']: ' . $_SERVER['HTTPS'];
echo '<br>$_SERVER[\'REMOTE_ADDR\']: ' . $_SERVER['REMOTE_ADDR'];    
echo '<br>$_SERVER[\'SERVER_NAME\']: ' . $_SERVER['SERVER_NAME'];
echo '<br>WP_HOME: ' . WP_HOME;
echo '<br>WP_SITEURL : ' . WP_SITEURL;

I am exhausted, just writting it down! Be patient!

Leave a Comment