Reverse proxy using Nginx to wordpress app hosted using nginx

I’m trying to use nginx as reverse proxy(Server A) to my wordpress server(Server B) hosted using nginx. I am trying to setup my url in a sub-directory format, like example.com/blog

It kinda works but with some issues.

  1. Server A has a LetsEncrypt SSL cert so it forces https on all routes. This is a problem because my wordpress app is loading .js & .css files on http.

  2. After successful login, instead of redirecting to example.com/blog/wp-admin, it goes to example.com/wp-admin

My Server A nginx conf file with the appropriate location block,

location /blog {
    rewrite ^/blog/(.*)$ /$1 break;

    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://ip-address-for-server-b;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

My Server B nginx conf file

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name localhost;

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }

    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
         expires max;
         log_not_found off;
    }

    location / {
        # try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

Also in my wp-config.php, I added these lines

define( 'WP_SITEURL', 'https://example.com/blog' );
define( 'WP_HOME', 'https://example.com/blog' );

Related:


Leave a Reply