How to seamlessly combine WordPress and Ruby on Rails application on the same backend, one domain, one server – Wasya Co
Published

Suppose you want to serve some paths with rails, and some other paths with wordpress. or you want the app to serve rails pages if they are available, and fall back on wordpress otherwise.

For SEO and generally to have good structure of online resources, maybe you want the apps (we’ll call them upstreams) to share the namespace. In other words, if you’re service categories, you want the paths to look like /categories/tea-kettles , /categories/coffee-mugs rather than /blog/categories/tea-kettles and /rails-app/categories/coffee-mugs . The question then is, what is the technical solution to this challenge?

We’ll use nginx to address this issue.

  upstream backends {
      server 192.2.0.1;
      server 192.2.0.2;
      ...
  }

  server {
      ...

      location / {
          proxy_pass http://backends;
          proxy_next_upstream error timeout http_404;
      }
  }

where the upstream servers are rails and wordpress, in that order.

Let’s try it on localhost!

And for files on disk, maybe static assets, the config is as follows:

  location / {
      root /path/to/root;
      try_files $uri @fallback;
  }

  location @fallback {
      proxy_pass http://...
  }

Another, less preferable and untested solution would be:

  location / {
      # Send 404s to B
      error_page 404 = @backendB;
      proxy_intercept_errors on;
      log_not_found  off;

      # Try the proxy like normal
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP  $remote_addr;
      proxy_pass http://A;
  }

  location @backendB {
      # If A didn't work, let's try B.
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP  $remote_addr;
      proxy_pass http://B;

      # Any 404s here are handled normally.
  }

By Victor Pudeyev

A technical lead and business developer residing in Austin, TX. I specialize in systems built with ruby, javascript and solidity.


0 comments

Leave a Reply