可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
- The main page of the blog opens as expected (example.com/blog) but without css.
- 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
Wordpress should be accessable from
blog-ip/blog
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; }
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';
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