5

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.

8
  • Can you first get your blog working directly using https://www.example.com/blog then it would be easier to proxy_pass Commented Sep 7, 2017 at 5:30
  • @TarunLalwani No man, I installed it using a pre-built image on Amazon Commented Sep 7, 2017 at 6:55
  • @TarunLalwani I've managed to route /blog to Wordpress on it's server. You're saying that now the nginx config should work properly? Commented Sep 7, 2017 at 8:58
  • Yes if it works there now nginx reverse proxy also should work Commented Sep 7, 2017 at 9:02
  • @TarunLalwani it's working but doesn't load css and js because chrome says it's unsafe. My Rails application is using SSL and the Wordpress is not. Do you know how can I fix this? Commented Sep 7, 2017 at 9:20

3 Answers 3

2

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');
    
Sign up to request clarification or add additional context in comments.

Comments

1

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';

1 Comment

Just to make sure, <ip> is the same should be the BLOG ip, right? I did that but the assets are still picked up from http://
0

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

6 Comments

Will this work if the blog is under root path on it's server and not /blog? webmasters.stackexchange.com/questions/98520/… I'm really confused on how to make this work. Can you please elaborate on how to make a reverse proxy from example.com/blog to blog.example.com and example.com as the main server?
The tricky part is moving WordPress to be served from the /blog directory mainly because CloudFront won't remove the /blog in the request, probably with some Nginx rewrite conditions could be solved.
I've managed to move the blog to /blog on it's server. Please tell me if I understand this right, I should create an origin with the domain blog.example.com with /blog* path and make example.com as an alternate domain?
your first origin will be something like mainsite.example.com Default(), your second origin will be blog.example.com and there in the Behaviors you will create a Path Pattern "/blog"
I did what you said, made a distribution with the origin example.com with the default settings, then added another origin blog.example.com and finally a behavior for /blog* path for the blog origin, but example.com/blog redirects me to the main page of my Rails application (there I have a rule that any unset route will redirect to the main page) I don't understand how this thing works
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.