如何将Google Cloud实例的WordPress博客映射至www.mywebsite.com/blog?
Hey there! Let's figure out how to get your WordPress blog accessible at www.mywebsite.com/blog alongside your main HTML site. You've got two solid approaches here—either connect the two separate instances with a reverse proxy (so you keep each service independent), or migrate WordPress to the same instance as your main site. Let's walk through both options step by step:
This method keeps your main site and WordPress on separate servers, using your main instance's web server to route /blog traffic to the WordPress instance.
Step 1: Configure your main site's web server as a reverse proxy
Depending on whether you use Nginx or Apache, follow these steps:For Nginx:
Edit your main site's config file (e.g.,/etc/nginx/sites-available/www.mywebsite.com) and add this location block inside theserverdirective:location /blog { proxy_pass http://[YOUR_WORDPRESS_INSTANCE_IP]/; proxy_set_header Host $host; 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; }Test the config with
nginx -t, then restart Nginx:sudo systemctl restart nginxFor Apache:
First enable required modules:sudo a2enmod proxy proxy_http proxy_html headers
Then add these lines to your main site's config file:ProxyPass /blog http://[YOUR_WORDPRESS_INSTANCE_IP]/ ProxyPassReverse /blog http://[YOUR_WORDPRESS_INSTANCE_IP]/ ProxyHTMLURLMap http://[YOUR_WORDPRESS_INSTANCE_IP]/ /blog RequestHeader set X-Forwarded-Proto "https" if "%{HTTPS} == on"Restart Apache:
sudo systemctl restart apache2
Step 2: Update WordPress site URLs
You need to tell WordPress it's now accessible at/blog:- If you can access the WordPress admin panel (via its IP), go to Settings > General and set both
WordPress Address (URL)andSite Address (URL)tohttps://www.mywebsite.com/blog - If you can't access the admin panel, edit the
wp-config.phpfile on your WordPress instance and add these lines:define('WP_HOME','https://www.mywebsite.com/blog'); define('WP_SITEURL','https://www.mywebsite.com/blog');
- If you can access the WordPress admin panel (via its IP), go to Settings > General and set both
Step 3: Fix static asset links in WordPress
Old absolute URLs might still point to the WordPress instance's IP. Fix this by:- Installing and activating the Relative URL plugin, or
- Running these SQL commands on your WordPress database (replace placeholders):
UPDATE wp_posts SET post_content = REPLACE(post_content, 'http://[YOUR_WORDPRESS_INSTANCE_IP]', 'https://www.mywebsite.com/blog'); UPDATE wp_postmeta SET meta_value = REPLACE(meta_value, 'http://[YOUR_WORDPRESS_INSTANCE_IP]', 'https://www.mywebsite.com/blog');
Step 4: Adjust Google Cloud Firewall rules
Make sure your main site instance can reach the WordPress instance's HTTP/HTTPS ports. Create a firewall rule that allows traffic from your main instance's internal/external IP to the WordPress instance's 80/443 ports.
If you prefer to manage everything on one instance, migrate WordPress to your main site server:
Step 1: Backup WordPress files and database
On your WordPress instance:- Package the WordPress directory:
sudo tar -czf wp-backup.tar.gz /var/www/html(adjust the path if your WordPress is elsewhere) - Export the database:
sudo mysqldump -u [DB_USER] -p [DB_NAME] > wp-db-backup.sql(enter your DB password when prompted)
- Package the WordPress directory:
Step 2: Transfer backups to the main instance
Usescpto send files over:scp wp-backup.tar.gz [MAIN_INSTANCE_USER]@[MAIN_INSTANCE_IP]:/tmp/ scp wp-db-backup.sql [MAIN_INSTANCE_USER]@[MAIN_INSTANCE_IP]:/tmp/Step 3: Restore WordPress on the main instance
- Create a
/blogsubdirectory in your main site's root:sudo mkdir /var/www/html/blog - Unpack the WordPress backup:
sudo tar -xzf /tmp/wp-backup.tar.gz -C /var/www/html/blog --strip-components=1 - Install MySQL/MariaDB if it's not already on the main instance:
sudo apt install mysql-server php-mysql - Create a new database and user for WordPress, then import the backup:
mysql -u root -p -e "CREATE DATABASE [NEW_DB_NAME]; CREATE USER '[NEW_DB_USER]'@'localhost' IDENTIFIED BY '[DB_PASSWORD]'; GRANT ALL PRIVILEGES ON [NEW_DB_NAME].* TO '[NEW_DB_USER]'@'localhost'; FLUSH PRIVILEGES;" mysql -u [NEW_DB_USER] -p [NEW_DB_NAME] < /tmp/wp-db-backup.sql
- Create a
Step 4: Update WordPress config
Edit/var/www/html/blog/wp-config.phpto use the new database credentials, and add:define('WP_HOME','https://www.mywebsite.com/blog'); define('WP_SITEURL','https://www.mywebsite.com/blog');Step 5: Fix file permissions
Ensure the web server can access the/blogdirectory:sudo chown -R www-data:www-data /var/www/html/blog
- Always use HTTPS: Install an SSL certificate for
www.mywebsite.com(e.g., with Let's Encrypt's Certbot) to avoid mixed-content errors in WordPress. - Clear cache: After making changes, test in incognito mode or clear your browser cache to avoid old cached URLs causing issues.
- Check permalinks: In WordPress admin, go to Settings > Permalinks and save your settings (even if they look correct) to refresh rewrite rules.
内容的提问来源于stack exchange,提问作者Eduardo Longo

