Skip to content

Instantly share code, notes, and snippets.

@chrisjhoughton
Last active May 14, 2024 15:08
Show Gist options
  • Save chrisjhoughton/8646918 to your computer and use it in GitHub Desktop.
Save chrisjhoughton/8646918 to your computer and use it in GitHub Desktop.
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];
}
?>
@Masplus
Copy link

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 !

@tradenet
Copy link

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 !

Interesting I did this and I get the loop.

@tradenet
Copy link

:$server_port

Hmmm...I moved this around to just after location. Seems to work now. Strange.

@mehdiMj-ir
Copy link

Hi, any suggestion how am I able to do this with in a subpath?
I want to serve it with /blog on nginx but static files like css & js won't load and I get 404 error.

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