How to run WordPress site behind Nginx reverse proxy

You need to edit 3 config file

1st is the nginx conf. My domain name is szeles.me and my server address is 192.168.0.5 listening on port 80
sudo nano /etc/nginx/conf.d/yourconfigfile.conf



server {

    server_name szeles.me;
    server_tokens off;

    root /var/www/html/wordpress/;
    index index.php index.html index.htm;
    client_max_body_size 100M;
    location / {
        try_files $uri @apache;
    }

    location @apache {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $host;
        proxy_pass http://192.168.0.5:80;
    }

    location ~[^?]*/$ {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $host;
        proxy_pass http://192.168.0.5:80;
    }

    location ~ \.php$ {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $host;
        proxy_pass http://192.168.0.5:80;
    }

    location ~/\. {
        deny all;
        access_log off;
        log_not_found off;
    }

The second file is the wp-config.php
sudo nano /var/www/html/wordpress/wp-config.php

define('WP_HOME','https://szeles.me');
define('WP_SITEURL','https://szeles.me');
define('FORCE_SSL_ADMIN', true);
if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
    $_SERVER['HTTPS']='on';

And the 3rd one is the wordpress.conf file on the htaccess
sudo nano /etc/apache2/sites-available/wordpress.conf

<VirtualHost *:80>
    ServerAdmin YOURMAILADRESS@gmail.com
    DocumentRoot /var/www/html/wordpress/
    ServerName szeles.me
    ServerAlias www.szeles.me

    <Directory /var/www/html/wordpress/>
        Options FollowSymLinks
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
        Require all granted
    </Directory>

     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

One thought on “How to run WordPress site behind Nginx reverse proxy

  • 8th September 2019 at 12:03 pm
    Permalink

    it’s a nice analysis, and I cannot agree more. In my experience, clients (and the clients’ teams) tend to put the emphasis in any cause that suits their prejudices instead of looking at the real causes. Server performance, cache, bad content and CMS misconfiguration tend to be much more an issue than any external tools used. But it’s often hard to explain that they are looking at the wrong place.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *