1

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' );
  • What happens if you remove the $uri/ element from the middle of your try_files statement? – Richard Smith Nov 6 '16 at 10:54
  • That has fixed my Issue #2, where on login, it would redirect me to the wrong url. However the SSL issue is still there – nupac Nov 6 '16 at 11:12
  • If the css & js files belong to WP, they should be prefixed by WP_SITEURL. Which css & js files are using http instead of https? – Richard Smith Nov 6 '16 at 11:38
  • @RichardSmith Seems like it's jquery, and some theme files. I added an image in the question. – nupac Nov 6 '16 at 12:34
  • You mention wp-config.html, do you mean wp-config.php? And you mention #define(...);, do you mean define(...); without the leading #? – Richard Smith Nov 6 '16 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.