Announcements
This site is in read only until July 22 as we migrate to a new platform; refer to this community post for more details.
Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Issue in wordpress website blog.example.com redirect to example.com/blog

I want to redirect my sub-domain (blog.example.com) redirect to the subdirctory (example.com/blog) in vm instance. I have a statically served site (using nginx).  My example.com is hosted on Kubernetes engine and my subdomain blog.example.com (wordpress website) on compute engine.

I tried this :-

 

location /blog/ {
    proxy_pass https://blog.example.com;
    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;
}

 

on nginx but it doesn't work.

I follow this link also but it doesn't work.

please help me out if any one have any solution thanks in advance

0 1 786
1 REPLY 1

dionv
Former Googler

Hello arunsinghbhati,

You can check this documentation as reference for the steps provided below:

1. Install Nginx

To begin, access your server’s terminal via SSH. Then use the apt-get command to update your distribution’s packages list and install Nginx on your web server.

sudo apt update
sudo apt install nginx

2. Configure Nginx to Proxy Requests

Next, you need to configure Nginx to proxy requests for domains hosted on Apache. To do that, create a new virtual host file. Here, I’m using the nano editor to add the code, but you can use any code editor of your choice.

sudo nano /etc/nginx/sites-available/example.com.conf

Then set Nginx directives to forward requests to Apache by adding the following server {...} and location blocks:

server {
listen      80;
server_name example.com www.example.com;
index       index.php;
root        /var/www/example.com/public    # fallback for index.php
location / {
try_files $uri $uri/ /index.php?$query_string;
}location /blog {
proxy_pass http://blog.domain.com;proxy_http_version                 1.1;
proxy_cache_bypass                 $http_upgrade;

# Proxy headers
proxy_set_header Upgrade           $http_upgrade;
proxy_set_header Connection        "upgrade";
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;
proxy_set_header X-Forwarded-Host  $host;
proxy_set_header X-Forwarded-Port  $server_port;

# Proxy timeouts
proxy_connect_timeout              60s;
proxy_send_timeout                 60s;
proxy_read_timeout                 60s;
}

In the code above, I’m defining a subdirectory example.com/blog link that will be served by the Apache server. Ensure that you use your proxied website’s public IP address (or URL) in the proxy_pass directive. In my case, my proxied website is hosted on the blog.domain.com subdomain.

Note: Ensure that the proxied website is installed and ready to be served before you make any changes.

You can learn more about all the reverse proxy directives used here in Nginx’s detailed index of directives.

3. Save the Virtual Host File Created

Then activate the new virtual host by creating a symlink for the files named example.com.conf in both the /etc/nginx/sites-available and the /etc/nginx/sites-enabled directories.

sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/example.com.conf

4. Test Nginx for Errors

After that, test Nginx for any configuration errors.

sudo nginx -t

If there are no errors, reload Nginx to enforce the changes.

sudo systemctl reload nginx

You’ve successfully set up Nginx to work as a reverse proxy now. To confirm this, you can use the phpinfo() function to check the PHP variables loaded when you visit your proxied site.

Under the SERVER_SOFTWARE and DOCUMENT_ROOT PHP variables, you’ll see that Apache serves this domain on the backend. But HTTP_X_REAL_IP and HTTP_X_FORWARDED_FOR PHP variables confirm that Nginx was used as a reverse proxy to forward the requests.

If the steps above are not what you're looking for, here's another reference that you can check

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([a-z0-9-]+)/? http://$1.example.com [R=301,NC,L]

Replace both instances of "example.com" with your domain name