Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up Wordpress and Rails in Nginx

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;
  }
}
like image 836
makhan Avatar asked Nov 02 '22 05:11

makhan


1 Answers

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
}
like image 76
makhan Avatar answered Nov 09 '22 16:11

makhan