Ad

Nginx - Run Wordpress As Subdomain Where Another App Is Running On Main Domain

- 1 answer

I'm having trouble deploying Wordpress in a subdomain where the main domain is running a different app (Shiny server). For the purpose of the question, my-domain.com is the main domain, and the location where I would like to deploy the Wordpress site is my-domain.com/blog. This is the current configuration file I have (in /etc/nginx/sites-available/my-domain.com symlinked into sites-enabled):

server { 
  root /var/www/my-domain.com; # Wordpress directory

  server_name my-domain.com www.my-domain.com;

  index index.html index.htm index.nginx-debian.html index.php;

  ## Shiny server
  location / {
    proxy_pass http://MY_IP:SHINY_SERVER_PORT;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }

  ## Wordpress subdomain location
  location /blog {
    try_files $uri $uri/ /index.php$is_args$args;

    location ~ /\.ht {
      deny all;
    }

    ## Wordpress restrictions 
    location = /blog/favicon.ico { log_not_found off; access_log off; }
    location = /blog/robots.txt { log_not_found off; access_log off; allow all; }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
    }
  }

  ## Added PHP config locations for MySQL/WP
  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
  }

  ## SSL configuration added by certbot
  # listen [::]:443 ssl ipv6only=on; # commented out as the server is not ipv6
  listen 443 ssl; # managed by Certbot
  ssl_certificate /etc/letsencrypt/live/my-domain.com/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/my-domain.com/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
  if ($host = www.my-domain.com) {
      return 301 https://$host$request_uri;
  } # managed by Certbot


  if ($host = my-domain.com) {
      return 301 https://$host$request_uri;
  } # managed by Certbot


  listen 80;
  listen [::]:80;

  server_name my-domain.com www.my-domain.com;
  return 404; # managed by Certbot

}

The previous version of this site was only Shiny server, and I have a fairly good understanding the configuration works for running just that app under the main domain. For reference, the additional bits were added based on this tutorial. The above configuration runs the Shiny server app just fine on my-domain.com, but when I navigate to my-domain.com/blog, the following "broken" version of Wordpress appears:

enter image description here

I've verified that I have configured Wordpress correctly as well: it works if I comment out the proxy_pass through proxy_set_header lines and add the try_files line to the main location / block and remove the location /blog block). I believe my issue is a misunderstanding of both the var/www directory, no knowledge of php, and a general amateur understanding on Nginx overall. What am I doing wrong here? This question seems to be close to accomplishing what I want, but after implementing this, my-domain.com/blog downloads a php file instead of loading anything. Please let me know if I can provide any additional information - I'm at a loss. Thanks!

Ad

Answer

In case anyone is curious, I figured this out. I was quite close, but a few things were off.

1.) The Wordpress files were located in the main /var/www/my-domain.com directory, but they needed to be moved to a corresponding subdirectory that lined up with the extension I was trying to move Wordpress to. In this case: /var/www/my-domain.com/blog.

2.) After this, the Nginx configuration from my original question needs to be updated as follows:

From:

## Wordpress subdomain location
location /blog {
  try_files $uri $uri/ /index.php$is_args$args;

To:

## Wordpress subdomain location
location /blog {
  try_files $uri $uri/ /blog/index.php$is_args$args;

3.) Additionally, and I'm not sure if this matters, but my original root directory was set as /var/www/my-domain.com, and I changed this to /var/www/my-domain.com/. This might not matter at all, but that's the only other thing I can see that's different.

Simple things really that in hindsight seem kind of obvious, but my knowledge of Nginx just wasn't quite there. Anyway, hope this helps anyone who stumbles on this.

Ad
source: stackoverflow.com
Ad