skip to Main Content

I have a Ruby on Rails application and a WordPress blog hosted on separate EC2 instances.

I’m trying to make the WordPress blog to act like a subfolder of the Rails application (example.com/blog instead of blog.example.com) for better SEO

  • The Rails application can be accessed through http and https (http is redirecting to https)

https://www.nginx.com/resources/admin-guide/reverse-proxy/

I tried using nginx reverse proxy function and I think it’s my best option right now but my attempt was unsuccessful.

  1. The main page of the blog opens as expected (example.com/blog) but
    without css.
  2. A URL with arguements (example.com/blog/args) redirects me back to
    the Rails application (example.com/args)

I set the desired blog url in wp-config.php as the following:

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

This is the nginx configuration I use:

  location ^~ /blog {
   proxy_pass http://<<BLOGIP>>/blog;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

It’s really important for the Rails application and the WordPress blog to stay separated for auto-scaling, redundancy and deployment purposes.

If there’s another way achieving this, I’m open to suggestions.

3

Answers


  1. Chosen as BEST ANSWER

    Solved with Tarun Lalwani's help

    1. Wordpress should be accessable from

      blog-ip/blog

    2. nginx config on example.com

        location ^~ /blog {
          proxy_pass http://<<blog-ip>>/blog;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
      
          proxy_redirect http://<<blog-ip>>/ https://$host/;
          proxy_cookie_domain <<blog-ip>> $host;
        }
      
    3. added this to wp-config.php

      define('FORCE_SSL_ADMIN', true);
      
      if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
          $_SERVER['HTTPS']='on';
      
    4. changed the url

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

  2. As an alternative solution, you could use AWS CDN CloudFront and have multiple origins. One origin for your site and other for the blog. (separated so you can scale them apart)

    For setting up WordPress you could follow this post: http://www.danneh.org/2015/04/setting-wordpress-amazon-cloudfront/

    Within your CloudFront Distribution the Behaviors, the Path Pattern may look like this:

    cloudfront path pattern

    Within the CDN you could do the “http to https” besides also only allowing traffic from CloudFront if required.

    Your Nginx conf regarding the location may look like this:

    location /blog {
        try_files $uri $uri/ /blog/index.php;
    }
    

    The /blog is because CloudFront won’t remove the Path Patterns so all request to your origins will be prefixed with /blog

    Login or Signup to reply.
  3. Now that you have your blog working at http://<blogip>/blog you need fix few more things

    location ^~ /blog { 
       proxy_pass http://<<BLOG_IP>>/blog; 
       proxy_set_header X-Real-IP $remote_addr; 
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    
       proxy_redirect http://<ip>/ https://$host/;
       proxy_cookie_domain <ip> $host;
       proxy_set_header X-Forwarded-Proto $scheme;
    
    }
    

    Also add below to your wp-config.php

    define('FORCE_SSL_ADMIN', true); 
    
    if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') 
    $_SERVER['HTTPS']='on';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search