enter image description here

I have a Rails app as https://example.com in a VPS. For SEO and other reasons, I want to host a WordPress in a separate server (shared hosting), and want to route it as https://example.com/blog. I also use Cloudflare. Here’s what I have done:

  • Setup A record in Cloudflare with blog pointing to my shared host IP address.
  • Setup WordPress as blog.example.com in the shared hosting. Accessed this fine.
  • Changed the WordPress site url and home url as https://example.com/blog (with SSL).

Here’s my full nginx:

upstream example {
  server unix:/home/deployer/example/shared/tmp/sockets/puma.sock fail_timeout=0;
}

server {
  listen 80;
  server_name example.com;

  client_max_body_size 4G;
  keepalive_timeout 10;

  error_page 500 502 503 504 /500;

  root /home/deployer/example/current/public;

  try_files $uri/index.html $uri.html $uri @example;

  location @example {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://example;
  }

  location /blog {
    access_log off;
    proxy_pass https://blog.example.com/;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

  location = /50x.html {
    root html;
  }

  location = /404.html {
    root html;
  }

  location @503 {
    error_page 405 = /system/maintenance.html;
    if (-f $document_root/system/maintenance.html) {
      rewrite ^(.*)$ /system/maintenance.html break;
    }
    rewrite ^(.*)$ /503.html break;
  }

  if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
    return 405;
  }

  if (-f $document_root/system/maintenance.html) {
    return 503;
  }
}

server {
  server_name www.example.com;
  return 301 $scheme://example.com$request_uri;
}

server {
  listen 80;
  server_name assets.example.com;

  client_max_body_size 4G;
  keepalive_timeout 10;

  root /home/deployer/example/current/public;

  location = /404.html {
    root html;
  }

  if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
    return 405;
  }

  if (-f $document_root/system/maintenance.html) {
    return 503;
  }
}

Configtest works fine. When I access https://example.com/blog, it throws me this error:

I tried to disable Cloudflare by not using proxy, and using direct DNS, the same error occurred. What have I done wrong?

I actually am trying to avoid using rack reverse proxy in Rails because I don’t want the traffic to hit Rails first, then redirect. I think this gives unnecessary load to Rails app, and it’s a lot slower too.

Read more here: WordPress in a subdirectory of a Rails app in a different server


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: