Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have the following set up:

  • nginx as reverse proxy
  • apache (with passenger) for serving content

Now I have simple Sinatra application and the following happens if I try to access different things:

  1. domain.com/hi - Sinatra says me Hi as was coded

  2. domain.com/readme.txt (static file) - it is fetched correctly from the 'public_html' folder by nginx

  3. And finally - domain.com/ or domain.com - 403 error

In the last case nginx probably tries to create listing but that's uncalled for (I want him to pass '/' request to apache). Permissions itself are fine.

Error itself (from /var/log/nginx/error.log):

2012/05/12 23:39:40 [error] 19012#0: *1 directory index of "/home/com_domain/public_html/" is forbidden, client: 66.77.88.99, server: domain.com, request: "GET / HTTP/1.1", host: "domain.com"

And configuration:

server {
    listen 80;
    server_name domain.com;

    access_log /var/log/nginx/com_domain.access.log;
    charset utf-8;
    root /home/com_domain/public_html;

    # Include @apache location
    include /etc/nginx/sites-available/_apache;

    location ~ /\.ht {
       deny all;
    }

    location / {
       try_files $uri $uri/ @apache;
    }
}
share|improve this question
    
One of Apache or nginx is not needed. –  womble May 13 '12 at 0:04
    
Why, both are needed - apache would run app itself, nginx serve static content for it. –  Stonerain May 13 '12 at 0:11
    
You don't need Apache to "run the app". –  womble May 13 '12 at 0:16
    
Don't see your point. Phusion Passenger was developed to make easier deployment of Ruby application. And yes, it can be installed as module for Apache.. –  Stonerain May 13 '12 at 0:25
2  
a) What womble is trying to say is that having 2 servers running adds unnecessary overhead, if one can do the job of both. Specifically, Nginx can serve your Passenger app as well as your static files, so removing Apache from your setup will result in a simpler and more efficient setup. (I realize it is tempting to say that Nginx will serve static files faster - however, the reverse proxy will slow down dynamic requests and consume unnecessary resources. b) At a guess, you can do one of two things - either add (or update) an index directive; or add a location = / {} block. –  cyberx86 May 13 '12 at 0:59

1 Answer 1

up vote 1 down vote accepted

Firstly, a point of mention about the setup - reverse proxy to Apache seems like a good choice at first - static files are served by Nginx and you still retain the functionality of Apache. However, the extra server adds a good bit of overhead - you have added more complexity to the system and an extra layer. Since Nginx can interface with other servers - such as passenger and PHP-FPM, you can match most of Apache's functionality with far less overhead. Getting rid of Apache will decrease the complexity and maintainance of your setup and consequently improve overall performance and stability and make future debugging easier.

Nginx is trying to display the contents of the root directory - you have told it to try serving the specified file and specified directory before proxying to apache. Nginx will look for a file matching those specified by the index directive, if there are no matches, it will attempt to display the contents of the directory (i.e. a directory listing), which is where it is failing.

If you have an index file that you would like to serve, you should update your index directive to include it. If you do not have such a file, and you want Apache to handle the request entirely, you need to create a matching location block and proxy_pass requests to Apache.

As per the Nginx wiki, location directives are matched in a particular order:

  1. Directives with the "=" prefix that match the query exactly (literal string). If found, searching stops.
  2. All remaining directives with conventional strings. If this match used the "^~" prefix, searching stops.
  3. Regular expressions, in the order they are defined in the configuration file.
  4. If #3 yielded a match, that result is used. Otherwise, the match from #2 is used.

Creating an additional location block, matching exactly (i.e. using an equal sign) the root path, will therefore, allow you to specify special rules for that single location. This location block should proxy requests to Apache if it is to work correctly.:

location = / {
 #proxy_pass ...;
 #error 200 = @apache;
 #include proxy_info;
 #try_files non_existent_file @apache;
}

Depending on your setup, there may be several ways to pass the request to the proxy, some of which are alluded to above.

You still should keep your location / {} block - it will match all requests not matched by your other location blocks. Note that while a try_files directive may work under the server {} block, it is better under the location block, since it will only be processed selectively.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.