5

I have nginx set up for Rails application that has been working beautifully for me. Now I want to move my Wordpress blog from blog.website.com to website.com/blog, so web crawlers treat it as part of the site.

I created a symbolic link in my Rails app's public/ directory and added the following to my nginx configuration:

# 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&$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;
    }
}

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 /blog block to:

location ^~ /blog {
  root /home/ubuntu/apps/blog;
  index    index.html  index.php;
  try_files $uri $uri/ /blog/index.php?q=$uri&$args;

  if (!-e $request_filename) {
    rewrite ^.+?(/blog/.*.php)$ $1 last;
    rewrite ^ /blog/index.php last;
  }
}

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.

I tried nesting the \.php$ location inside the /blog location but it results in 404 error.

What's wrong with the following piece and how to fix it?

location ^~ /blog {
  root /home/ubuntu/apps/blog;
  index    index.html  index.php;
  try_files $uri $uri/ /blog/index.php?q=$uri&$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;
  }
}
4
  • Did you ever figure this out? trying to do the same thing. Commented Jul 15, 2014 at 16:58
  • Nope, sorry. I added symbolic link creation to my deployment scripts and went with the first configuration. Commented Jul 16, 2014 at 2:46
  • Would you mind posting your full nginx.conf with details removed? I am ok with having to deploy a symlink, but I can't get your example working. Commented Jul 16, 2014 at 15:50
  • I edited the question. Please see the first config. Commented Jul 17, 2014 at 3:43

1 Answer 1

2

I haven't fixed the snippet from the question but I managed to solve the problem with nginx configuration without using a symbolic link by using two server blocks.

server {
    location / {
        proxy_pass http://localhost:82/;
    }

    location /blog {
        root /var/proj/blog/current;
        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;
    }
}
# Rails server                                                                                                                                                                                                                             
server {
    listen 82;
    root /var/proj/site/current/public;
    server_name site.com;
    passenger_enabled on;
    rails_env staging;
    client_max_body_size 20m;

    # other configuration
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.