1

I have 2 apps in 2 different folders. I want to use the root folder for everything except when there is /SOMENAME as a folder in the url, in such a case i want to point to that folder.

I tried several location / alias blocks but none of them worked (the location was correct but php files were not served only static content)... see my config below.

server {
    listen 80;
    #disable_symlinks off;
    server_name join.mydomain.com; #allow few domains
    root /www/main;

    #removing the 'index.php'
    location / {
            if (!-e $request_filename){
                rewrite ^(.*)$ /index.php;
            }
            index index.html index.php;
    }

    location /SOMENAME/ {
            root /www/somename;
    }

    #######################
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass        unix:/var/run/php5-fpm.sock;
        include             fastcgi_params;
        fastcgi_param       PATH_INFO $fastcgi_script_name;
        fastcgi_param HTTPS on;
        fastcgi_index       index.php;
        fastcgi_param       SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}
Danny Valariola
  • 263
  • 1
  • 3
  • 12

1 Answers1

1

You have two PHP apps, hosted in different document roots, which means that you need two location ~ \.php$ blocks. Assuming that SOMENAME and somename are actually the same, you can use something like this:

root /www/main;
index index.html index.php;

location / {
    try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass  unix:/var/run/php5-fpm.sock;
    include       fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $request_filename;
}

location ^~ /somename {
    root /www;
    try_files $uri $uri/ /somename/index.php;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass  unix:/var/run/php5-fpm.sock;
        include       fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
    }
}

The ^~ modifier on the location directive, make this prefix location take precedence over the regular expression location block above it, so that PHP scripts are processed by the correct location block. See this document for details.

The try_files directive is documented here.

Richard Smith
  • 13,284