Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample Nginx reverse proxy to Apache set up for Wordpress.
<VirtualHost *:{PORT}>
ServerName www.yourdomain.com
ServerAdmin mail@domain.com
DocumentRoot /var/www/yourdir/
<Directory /var/www/yourdir>
Options Indexes FollowSymLinks
AllowOverride all
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
# Add this file to your /etc/nginx/sites-available directory, renaming it to
# the domain of your website.
# Change the: server_name, port
# Credit to http://mattkirman.com/2011/06/01/how-to-speed-up-wordpress-with-nginx/.
server {
listen 80;
server_name www.yourdomain.com; # change this
# global gzip on
gzip on;
gzip_min_length 10240;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
gzip_disable "MSIE [1-6]\.";
add_header Cache-Control public;
location / {
proxy_pass http://127.0.0.1:{PORT}; # change this
proxy_buffering on;
proxy_buffers 12 12k;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
}
}
<?php
// Add this to the top of your wp-config.php file.
// Handle reverse proxy, passing the IP to the server.
// This is used by some plugins to fetch the user's IP.
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['REMOTE_ADDR'] = $ips[0];
}
?>
@Scepticalist

This comment has been minimized.

Copy link

@Scepticalist Scepticalist commented Feb 14, 2019

I have my Wordpress running under Apache on a separate server - do you know if this config will work for that (changing the proxy_pass to the server IP?)

@hyleck

This comment has been minimized.

Copy link

@hyleck hyleck commented Mar 22, 2019

When you put a ssl to domain certificate, it stops working.

@heltonvalentini

This comment has been minimized.

Copy link

@heltonvalentini heltonvalentini commented Jun 3, 2019

Thanks mate this was really useful form me.

@mubasshir

This comment has been minimized.

Copy link

@mubasshir mubasshir commented Jul 1, 2019

When you put a ssl to domain certificate, it stops working.

@hyleck, did you find any solution for this?

@sinabio

This comment has been minimized.

Copy link

@sinabio sinabio commented Sep 5, 2019

When you put a ssl to domain certificate, it stops working.

@hyleck, did you find any solution for this?

use following code on wp-config.php

/**

  • Handle SSL reverse proxy
    */
    if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
    $_SERVER['HTTPS']='on';

if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
}

@cornerstone-ccl

This comment has been minimized.

Copy link

@cornerstone-ccl cornerstone-ccl commented May 21, 2020

I also want to enable https. I added the SSL reverse proxy code on wp-config.php (see below) but https still does not work. Any idea why? Your help is appreciated. Thanks

//Handle SSL reverse proxy
if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
$_SERVER['HTTPS']='on';
if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
}

// Handle reverse proxy, passing the IP to the server.
// This is used by some plugins to fetch the user's IP.
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['REMOTE_ADDR'] = $ips[0];
}

@teacherpan

This comment has been minimized.

Copy link

@teacherpan teacherpan commented Jul 23, 2020

Must add $server_port to Host to avoid 301 loop error:

proxy_set_header Host $host:$server_port;
@serome111

This comment has been minimized.

Copy link

@serome111 serome111 commented Aug 29, 2020

I also have a problem with the ssl does not work and when I enter the domain looks like this
example.com.co:81
Could someone please help me or explain what I am doing wrong?

  server_name example.com.co; # change this

  gzip on;
  gzip_min_length 10240;
  gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
  gzip_disable "MSIE [1-6]\.";

  add_header Cache-Control public;

  location / {
    proxy_set_header Host $host:$server_port;
    proxy_pass http://127.0.0.1:81/example; # change this
    proxy_buffering on;
    proxy_buffers 12 12k;
    proxy_redirect off;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
  }



    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com.co/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com.co/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = example.com.co) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


  listen 80;
  server_name example.com.co;
    return 404; # managed by Certbot


}
`

I'm just installing the wp-config.php so there is no such file.
@mh4nx7net

This comment has been minimized.

Copy link

@mh4nx7net mh4nx7net commented Nov 1, 2020

greatt

@Masplus

This comment has been minimized.

Copy link

@Masplus Masplus commented Feb 16, 2021

Must add $server_port to Host to avoid 301 loop error:

proxy_set_header Host $host:$server_port;

This was the absolute solution for my nightmare, just this :$server_port.
Thank you very much !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment