title: Setting up Wordpress and Rails in Nginx
body: <p>I have nginx set up for Rails application that has been working beautifully for me. Now I want to move my Wordpress blog from <code>blog.website.com</code> to <code>website.com/blog</code>, so web crawlers treat it as part of the site.</p>
  
  <p>I created a symbolic link in my Rails app's <code>public/</code> directory and added the following to my nginx configuration:</p>
  
  <pre><code># Rails server
  server {
      root /project/path/current/public;
      server_name project.com;
      passenger_enabled on;
      rails_env production;
      client_max_body_size 20m;
  
      if ($http_x_forwarded_proto != 'https') {
          return 301  https://$host$request_uri;
      }
  
      location /blog {
          index    index.html  index.php;
          try_files $uri $uri/ /blog/index.php?q=$uri&amp;$args;
  
          if (!-e $request_filename) {
              rewrite ^.+?(/blog/.*.php)$ $1 last;
              rewrite ^ /blog/index.php last;
          }
      }
  
      location ~ \.php$ {
      try_files $uri =404;
      fastcgi_intercept_errors on;
      include fastcgi_params;
      fastcgi_index  index.php;
      fastcgi_param SCRIPT_NAME $fastcgi_script_name;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_pass unix:/var/run/php5-fpm.sock;
      }
  }
  </code></pre>
  
  <p>This works great.
  But what I'd like to do is skip the symbolic link part. I'd like nginx to route directly to the wordpress directory. I tried changing the <code>/blog</code> block to:</p>
  
  <pre><code>location ^~ /blog {
    root /home/ubuntu/apps/blog;
    index    index.html  index.php;
    try_files $uri $uri/ /blog/index.php?q=$uri&amp;$args;
  
    if (!-e $request_filename) {
      rewrite ^.+?(/blog/.*.php)$ $1 last;
      rewrite ^ /blog/index.php last;
    }
  }
  </code></pre>
  
  <p>This works, as in I'm being correctly redirected to the wordpress folder but my server doesn't know what to do with php files and sends them to my browser as files to download.</p>
  
  <p>I tried nesting the <code>\.php$</code> location inside the <code>/blog</code> location but it results in 404 error.</p>
  
  <p><strong>What's wrong with the following piece and how to fix it?</strong></p>
  
  <pre><code>location ^~ /blog {
    root /home/ubuntu/apps/blog;
    index    index.html  index.php;
    try_files $uri $uri/ /blog/index.php?q=$uri&amp;$args;
  
    if (!-e $request_filename) {
      rewrite ^.+?(/blog/.*.php)$ $1 last;
      rewrite ^ /blog/index.php last;
    }
  
    location ~ \.php$ {
      try_files $uri =404;
      fastcgi_intercept_errors on;
      include fastcgi_params;
      fastcgi_index  index.php;
      fastcgi_param SCRIPT_NAME $fastcgi_script_name;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
  }
  </code></pre>