Nginx with both PHP and Rails

I’ve just been playing with getting a new Nginx installation configured to handle both some PHP code that we got from a contractor, and some new Rails code that I’ve been working on. It’s functioning properly now, but only due to a hack. Nonetheless, it may be useful to others, and when I work out a better way to do it, I’ll post here again. I will note, though, that the easiest way to do this would be to set up two subdomains with separate server containers in the Nginx config; I just didn’t want to do that if I could avoid it.

So, let’s say that the PHP app is under /var/www/phpapp and the Rails app is under /var/www/railsapp, to keep things simple. I installed Nginx with FastCGI for the PHP, and Passenger for Rails. The basic Nginx config contains:

    # Set up the PHP app at the root
    location / {
      root   /var/www/phpapp;
      index  index.php index.html index.htm;

    # Set up the Rails app under /railsapp
    location /railsapp {
      root /var/www/railsapp/current/public;
      passenger_enabled on;
      rails_env production;

    # Pass PHP scripts to FastCGI
    location ~ \.php$ {
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME
            include fastcgi_params;

So, that will almost but not quite do it, allowing me to go to http://myserver/ to access the PHP app, and http://myserver/railsapp to access the Rails app. Except that when I try to access the Rails app, it fails because it thinks that the routes should all contain the prefix “/railsapp”.

So, I added a relative URL root. In my Rails app’s /config/environment.rb file, I put this into the config block (after do |config|):

        = "/railsapp"

Now when I access the rails app with http://myserver/railsapp, I get the expected page! Except…no images, CSS, or Javascript. Because all of the URLs have /railsapp in front of them, which doesn’t work.

Here’s the hack: I put a link in my app’s /public directory, pointing to itself:

    ln -s /var/www/railsapp/current/public

Sadly, this works. I say sadly because it’s obviously not anything that I’ll keep around. This is just on an internal development server right now, so it’ll let me play with the two apps together, but there’s no way I’ll push to production with a hacked-up environment.rb and a symlink. Nonetheless, I figured I’d share the experiment, and hopefully I’ll find a better way soon.

Oh, and I should mention that I did try using the directive “passenger_base_uri /railsapp” in the nginx config, but that doesn’t do it. Although it tells Passenger that /railsapp is a distinct Rails application, it seems to still leave the app expecting routes to match the “/railsapp/x” path.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: