nginx reverse proxy from rails to wordpress

匿名 (未验证) 提交于 2019-12-03 09:02:45

问题:

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.

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


回答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'); 


回答3:

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:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!