Nginx and Subdomain setup urgent!

November 10, 2014 2.2k views

Hello.

I've been trying to set up a nginx server block specifically for a subdomain without any success for days. Digitalocean direct support doesn't help as they just point me out the same articles I've already read exhaustively.

Here's my server's scenario:

I've got only one domain, let's say it is company.com, then I've set up WordPress to run my website under /var/www/site - I also have set up a ssl certificate for company.com

In order to have the mentioned set up above working, I ended up with the following nginx server blocks:

This is not the most polished nginx set up for server blocks ever, but it works out of the box

# HTTPS Secure Server
#
server {
listen 443 ssl;
server_name www.company.com  company.com;

root /var/www;
index index.php index.html index.htm;

    charset utf-8;

     ssl on;
     ssl_certificate /etc/nginx/ssl/ssl-bundle.crt;
     ssl_certificate_key /etc/nginx/ssl/www.company.com.key;

 ssl_session_timeout 10m;

     ssl_ciphers HIGH:!aNULL:!MD5:!kEDH;
     ssl_prefer_server_ciphers  on;

location / {
      #try_files $uri $uri/ /index.html;
      try_files $uri $uri/ /site/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;
}

 # Pass the PHP scripts to FastCGI server

location ~ \.php$ {
         try_files $uri $uri/ /site/index.php?q=$uri&$args;
      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;
      include fastcgi_params;
}

}

# HTTP Non Secure Server
#
server {
listen 80 default_server;
   #listen [::]:80 default_server ipv6only=on;

    root /var/www;
    #index index.html index.htm;
    index index.php index.html index.htm;

   charset utf-8;

   server_name www.company.com  company.com;

   # Uncomment bellow to Force non SSL site to redirect traffic to SSL
    return 301 https://www.company.com$request_uri;
    return 301 https://company.com$request_uri;

location / {
    #try_files $uri $uri/ /index.html;
     try_files $uri $uri/ /site/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;
}

# Pass the PHP scripts to FastCGI server

location ~ \.php$ {
     try_files $uri $uri/ /site/index.php?q=$uri&$args;
    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;
    include fastcgi_params;
  }
}

So, with the above server blocks set up I ended up with something like the following:

  • If I access http://company.com I reach /var/www directory with a few .html files
  • If I access http://company.com/site I reach /var/www/site directory path in which WordPress resides, so far so good, but now comes the pain part...

...What if I wanted another WordPress installation running on a second directory under /var/www ? I mean let's suppose the directory was named /site2/

Here's what I did so far:

First, I created a new A record under my domain's ( company.com ) DNS settings which resulted in:

test.company.com. 1800 IN A 100.100.100.100

Later on I followed the common WP installation steps and then I created another fresh WordPress website under /var/www/site2 directory - which is active a fully functional when accessing http://domain.com/site2/

And here's my nginx server block config for the subdomain test.company.com I created earlier:

# Second WordPress site
#
server {
listen 80;

    root /var/www/site2;
   #index index.html index.htm;
   index index.php index.html index.htm;

   charset utf-8;

    server_name test.company.com www.test.company.com;

location / {
     #try_files $uri $uri/ /index.html;
     try_files $uri $uri/ /site2/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;
}

 # Pass the PHP scripts to FastCGI server

location ~ \.php$ {
            try_files $uri $uri/ /site2/index.php?q=$uri&$args;
    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;
    include fastcgi_params;
   }
}

No matter what the changes I made on the server block above I just can't get the subdomain test.company.com to redirect to /var/www/site2 directory where the second wordpress installation resides...

If I access test.company.com I'm always redirected to my main domain company.com

Please I'd really appreciate your help on pointing me out where I might be making mistakes, because the available articles regarding this issue on DO community are so broad, and although I've found a few questions similar to mine, I couldn't find a guide that would cover the complete steps on setting up a nginx server block specifically for a subdomain to serve files on a different directory.

Thanks in advance.

2 comments
  • You say:

    "Later on I followed the common WP installation steps and then I created another fresh WordPress website under /var/www/site2 directory - which is active a fully functional when accessing http://domain.com/site2/"

    Then:

    "No matter what the changes I made on the server block above I just can't get the subdomain test.company.com to redirect to /var/www/site2 directory where the second wordpress installation resides..."

    So is it fully functional or not? Where does domain.com come from?

    Can you just edit your post with the real configs and real domain? This is really hard to read and would allow us to test.

  • Hi @gp+digitalocean. Thanks for your help. I've posted real info about my domain and nginx server blocks config. I'd really appreciate your further considerations. Thanks a lot!

2 Answers

Did you create a link to the config in the site-enabled directory?

  • Oh yeah. I created the symbolic link to the config and the website is active. I've answered @gp+digitalocean with real information about my domain and server block set up.

    Thanks for your help, any further consideration is welcome.

Sorry, I cannot edit the question anymore.

My real domain is acaiah.com.br

I already run a WordPress website under /var/www/site directory, properly set up with a ssl certificate as per the following nginx server blocks:

# HTTPS Secure Server
#
server {
listen 443 ssl;
server_name www.acaiah.com.br  acaiah.com.br;

root /var/www;
index index.php index.html index.htm;

charset utf-8;

 ssl on;
 ssl_certificate /etc/nginx/ssl/ssl-bundle.crt;
 ssl_certificate_key /etc/nginx/ssl/www.acaiah.com.key;

 ssl_session_timeout 10m;

 ssl_ciphers HIGH:!aNULL:!MD5:!kEDH;
 ssl_prefer_server_ciphers  on;

location / {
    #try_files $uri $uri/ /index.html;
    try_files $uri $uri/ /site/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;
}

 # Pass the PHP scripts to FastCGI server

 location ~ \.php$ {
       try_files $uri $uri/ /site/index.php?q=$uri&$args;
       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;
       include fastcgi_params;
     }
}


# HTTP Non Secure Server
#
server {
   listen 80;
  #listen [::]:80 default_server ipv6only=on;

  server_name www.acaiah.com.br  acaiah.com.br;

# Force non SSL site to redirect traffic to SSL
  return 301 https://www.company.com$request_uri;
  return 301 https://company.com$request_uri;

  root /var/www;
  index index.php index.html index.htm;

  charset utf-8;

   ssl on;
   ssl_certificate /etc/nginx/ssl/ssl-bundle.crt;
   ssl_certificate_key /etc/nginx/ssl/www.acaiah.com.key;

   ssl_session_timeout 10m;

   ssl_ciphers HIGH:!aNULL:!MD5:!kEDH;
   ssl_prefer_server_ciphers  on;

  location / {
     #try_files $uri $uri/ /index.html;
     try_files $uri $uri/ /site/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;
}

 # Pass the PHP scripts to FastCGI server

     location ~ \.php$ {
         try_files $uri $uri/ /site/index.php?q=$uri&$args;
         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;
         include fastcgi_params;
     }
}

The above set up is great for me because it allows me to serve a static html site or whatever under /var/www while a fully working WP website resides under /var/www/site - then I can redirect the main domain manually to /var/www/site by placing wordpress index.php file in the root /var/www directory

e.g:

<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
/**require( dirname( __FILE__ ) . '/wp-blog-header.php' ); */
require( dirname( __FILE__ ) . '/site/wp-blog-header.php' );

Now I'd like to run another separate WordPress website under /var/www/treinup directory, and for this purpose I've created the subdomain treinup.acaiah.com.br

  • I'm not interested in the WordPress Multisite configuration because I just want to develop a new temporary WP website using the subdomain treinup.acaiah.com.br which I'll then migrate to another Droplet with its own domain, so I'd like people to get to http://acaiah.com.br/treinup/ when accessing treinup.com.br subdomain.

here's the server block for treinup.acaiah.com.br

# HTTP Non Secure Server
#
server {
listen 80;

root /var/www/treinup;
#index index.html index.htm;
index index.php index.html index.htm;

charset utf-8;

server_name treinup.acaiah.com.br www.treinup.acaiah.com.br;

location / {
  # Uncomment bellow to use the original try-files scheme
  #try_files $uri $uri/ /index.html;
  # Uncomment bellow to use the the optimized try-files scheme for WordPress on its own directory
  try_files $uri $uri/ /treinup/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;
}

# Pass the PHP scripts to FastCGI server

location ~ \.php$ {
     # repeat the same try-files scheme here as above
     try_files $uri $uri/ /treinup/index.php?q=$uri&$args;
    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;
    include fastcgi_params;
}

}

The issue here is that the subdomain treinup.acaiah.com.br is not redirecting to /var/www/treinup, or better yet, is not redirecting to http://acaiah.com.br/treinup/

As it can be seen, I've set the root to /var/www/treinup; on the above server block, but if I access treinup.acaiah.com.br I'm always redirected to my main domain acaiah.com.br

Any chances it might be a DNS issue?

Thanks so much in advance.

Have another answer? Share your knowledge.