Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX try_files + alias directives

Tags:

nginx

I'm trying to serve request to /blog subdirectory of a site with the php code, located in a folder outside document root directory. Here's my host config:

server {     server_name  local.test.ru;     root   /home/alex/www/test2;      location /blog {         alias   /home/alex/www/test1;         try_files $uri $uri/ /index.php$is_args$args;          location ~ \.php$ {             fastcgi_split_path_info ^(/blog)(/.*)$;             fastcgi_pass unix:/var/run/php5-fpm.sock;             include fastcgi_params;         }     } } 

And I get for requests like

wget -O - http://local.test.ru/blog/nonExisting

just a code of index.php file from /home/alex/www/test2/ folder.

However, this config:

server {     server_name  local.test.ru;     root   /home/alex/www/test2;      location /blog {         alias   /home/alex/www/test1;         try_files $uri $uri/ /blog$is_args$args;         index index.php;          location ~ \.php$ {             fastcgi_split_path_info ^(/blog)(/.*)$;             fastcgi_pass unix:/var/run/php5-fpm.sock;             include fastcgi_params;         }     } } 

gives me index.html file from /home/alex/www/test2/. Please give me a clue - why? And how can I force NGINX to process /home/alex/www/test1/index.php instead?

like image 446
Alex Kovytin Avatar asked Dec 06 '13 14:12

Alex Kovytin


People also ask

What is try_files in nginx?

The try_files directive can be used to check whether the specified file or directory exists; NGINX makes an internal redirect if it does, or returns a specified status code if it doesn't.

What is alias directive in nginx?

NGINX Alias Directive Alias directive allows you to remap URLs to a different directory other than root location. It is useful for serving static files from a different directory. For example, if Alias is /var/www/html/css for location /static/, then when a user requests /static/style.

What is try_files $URI?

The try_file directive is in the server and location blocks and specifies the files and directories in which Nginx should check for files if the request to the specified location is received. A typical try_files directive syntax is as: location / { try_files $uri $uri/ /default/index.html; }


1 Answers

We could not get it to work by specifying root within the location block. The solution for us was to use alias instead. Note that it is necessary repeat the location's path twice in the try_files directive, and then also in the .php configuration block:

server {     server_name localhost;     root /app/frontend/www;      location /backend/ {          alias /app/backend/www/;          # serve static files direct + allow friendly urls         # Note: The seemingly weird syntax is due to a long-standing bug in nginx: https://trac.nginx.org/nginx/ticket/97         try_files $uri $uri/ /backend//backend/index.php?$args;          location ~ /backend/.+\.php$ {             include fastcgi_params;             fastcgi_buffers 256 4k;             fastcgi_param SCRIPT_FILENAME $request_filename;             fastcgi_param HTTPS $proxied_https;             fastcgi_pass phpfiles;         }      } # / location  } 

Source: nginx/conf.d/app.conf from the debian-php-nginx stack in the docker-stack project

like image 55
Motin Avatar answered Sep 26 '22 04:09

Motin