Nginx aliases not working with php

I have an application that runs on Nginx with a server working block like this:

server {

  listen 80;
  server_name example.com;
  root /home/deployer/apps/my_app/current/;
  index index.php;

  location / {
    index index.php;
    try_files $uri $uri/;
  }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/home/deployer/apps/shared/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
  }

  location /foo {
    root /home/deployer/apps/modules/;
    # tried this:
    # alias /home/deployer/apps/modules/foo/;
    # but php is not working with alias, only with root
  }

}

      

When I visit / foo, Nginx looks for the path / home / deployer / apps / modules / foo / for the index.php file and it works.

Problem:

I installed a deployment script using capistrano, which is deployed to the foo directory:

/home/deployer/apps/modules/foo/

      

Capistrano creates a "current" directory in the "foo" directory to contain the application files pulled from Github, so I needed to change the root path:

/home/deployer/apps/modules/foo/current/

      

But Nginx adds a location directive to the end of the root directive .... so when you visit / foo, Nginx tries to look:

/home/deployer/apps/modules/foo/current/foo/

      

Using an alias should ignore the / foo given in the location directive and serve files from the exact path of the aliases (which checks for log confirmation), but when I use the alias directive the php config doesn't get applied correctly and I get a 404 back.

If I go back to the root directive and delete the "current" directory entirely, it works fine. I need files to be served from the current directory to work smoothly with my Capistrano deployment, but can't figure out how to get the alias directive to work with php.

Anyone have any ideas or advice am I missing something?

+3


source to share


2 answers


thanks to @ xavier-lucas for suggesting not being able to use try_files with an alias.

To use an alias with php, I had to remove the try_files directive from the php location block mentioned in the original question:

try_files $uri =404;

      



I actually had to recalculate the php location block at the / foo location and remove the above line. It looked like this:

location /foo {
  alias /home/deployer/apps/modules/foo/;
  location ~ \.php$ {
    # try_files $uri =404; -- removed this line
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/home/deployer/apps/shared/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
  }
}

      

This allows php files to be processed directly from the directory specified in the alias directive.

+3


source


Use

location /foo/ {
    alias /home/deployer/apps/modules/foo/current/;
}

      



instead

location /foo {
    alias /home/deployer/apps/modules/foo/;
}

      

+1


source







All Articles