Symfony2 app in subdirectory of nginx configuration

Uthr asked:

I’m trying to set up my environment such that surveyadmin.local points at one directory and surveyadmin.local/api points at another.

However, no matter what I change, the /api URL always results in a call to the directory that surveyadmin.local is pointing at. Here’s a sample error from the error log specified in the vhost config:

2013/08/09 17:20:07 [error] 8911#0: *1 open() "/srv/http/surveytool/admin/build/app.php" failed (2: No such file or directory), client:, server: surveyadmin.local, request: "GET /api/organizations HTTP/1.1", host: "surveyadmin.local"

Note that the error refers to a path of /srv/http/surveytool/admin/buid/, instead of the /srv/http/surveytool/api/SurveyTool/web/ that was expected.

Here’s my config for the vhost in question:

server {
    listen 80;
    server_name surveyadmin.local;

    location / {
        root /srv/http/surveytool/admin/build;
        index index.html;

    location /api {
        root /srv/http/surveytool/api/SurveyTool/web;
        index app.php;
        try_files $uri $uri/ /app.php?$query_String;
        location ~ \.php(?|$) {
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index app.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include /etc/nginx/fastcgi_params;
    access_log  /var/log/nginx/surveyadmin.access.log;
    error_log   /var/log/nginx/surveyadmin.error.log debug;

I have tried changing the word “root” to “alias” under the location /api block. I’ve tried setting up a second virtual host and doing a proxy forward (with proxy_pass) to that vhost.

This seems like it shouldn’t be that difficult, so I’m assuming my error is a simple one that I just haven’t seen yet.

Can anyone spot any obvious problems with the above config?

My answer:

Don’t use root inside location blocks. This is one of the most common nginx misconfigurations.

Instead, place the root in the server block, and use alias inside location blocks that need specific directories outside the normal document root.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.