Wordpress not loading on shared NGINX server name

118 Views Asked by At

I am successfully running a Wordpress installation alongside Meteor using NGINX. I have no real experience with Wordpress or php, so this may be a simple fix.

The following configuration works:

server_tokens off;

map $http_upgrade $connection_upgrade {
  default upgrade;
  ''    close;
}

upstream mydomain-production {
  server localhost:8000;
}

# redirect all non-subdomain traffic to https
server {
  listen 80;
  server_name www.mydomain.com mydomain.com;
  rewrite ^ https://$host$request_uri? permanent;
  access_log /var/log/nginx/mydomain.access.log;
  error_log /var/log/nginx/mydomain.error.log;
}

# this non-subdomain serves meteor correctly
server {
  listen 443 ssl spdy;
  server_name www.mydomain.com mydomain.com;
  access_log /var/log/nginx/mydomain.secure.access.log;
  error_log /var/log/nginx/mydomain.secure.error.log debug;

  ssl_certificate #...removed...# ;
  ssl_certificate_key #...removed...# ;

  ssl_stapling on;
  ssl_session_cache shared:SSL:10m;
  ssl_session_timeout 5m;

  ssl_prefer_server_ciphers on;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers #...removed...# ;

  add_header Strict-Transport-Security "max-age=31536000;";

  ################################
  # additional code will go here #
  ################################

  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://mydomain-production;
    proxy_redirect off;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    if ($uri != '/') {
      expires 30d;
    }
  }
}

# this temporary subdomain serves wordpress correctly
server {
  listen 80;
  server_name wordpress.mydomain.com;
  access_log /var/log/nginx/mydomain.access.log;
  error_log /var/log/nginx/mydomain.error.log;
  index index.php index.html index.htm;
  root /var/www;

  location / {
    try_files $uri $uri/ /index.php?q=$uri&$args;
  }

  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;

  location /50x.html {
    root /usr/share/nginx/html;
  }

  location ~ \.php$ {
    try_files $uri =404;

    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_intercept_errors on;

    include fastcgi_params;
  }
}

So since I have Wordpress functioning on a temporary subdomain, I want to make it work on the same domain as Meteor and include location directives to route certain requests to Wordpress instead.

I tried adding the following to the 443 server name:

# additional code

  location /blog {
    root /var/www;
    try_files $uri $uri/ /index.php?q=$uri&$args;
    index index.php index.html index.htm;
  }

  location /wp-admin {
    root /var/www;
    try_files $uri $uri/ /index.php?q=$uri&$args;
    index index.php index.html index.htm;
  }

  location ~ \.php$ {
    try_files $uri =404;

    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_intercept_errors on;

    include fastcgi_params;
  }

After doing this, I get an NGINX 404 page at mydomain/blog. So the location directive is successfully sending the request to /var/www instead of Meteor, but for some reason it is not getting to the Wordpress router. I have linked my NGINX error debug output here.

1

There are 1 best solutions below

0
On

This was somehow solved by simply moving the root /var/www; and index index.php index.html index.htm; outside of the location directive(s). I would be interested to know why this is necessary if anyone can shed any light on this.