Nginx subdirectory root with PHP
If your files are in /etc/nginx/www
you will need to use an alias
directive, rather than a root
directive. See this document for details.
For example:
location ^~ /web {
index index.php index.html;
alias /etc/nginx/www;
if (!-e $request_filename) { rewrite ^ /web/index.php last; }
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
fastcgi_pass php:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
Use $request_filename
to obtain the correct path to the aliased file. Avoid try_files
with alias
due to this issue. See this caution on the use of if
.
Nginx - location with root in other directory and PHP
Running two PHP applications side-by-side, you either need a common document root, or you need two location ~* \.php
(or similar) blocks to ensure the correct SCRIPT_FILENAME
is sent to the fastcgi backend.
Use nested location
blocks to isolate the /cut
subdirectory, and use the ^~
modifier at the top level to avoid other top level regular expression location
blocks from interfering (see this documentation).
The alias
directive (see this documentation) is used to map /cut
to /var/www/cut/public
. The root
directive can only concatenate, which would make /var/www/cut/public/cut
(which you do not want).
However, I would not recommend using the alias
directive with the try_files
directive because of this long term issue.
So, a solution would be to silently rewrite /cut
to /cut/public
and use a value of root /var/www
.
For example:
location ^~ /cut {
rewrite ^/cut(.*)$ /cut/public$1 last;
}
location ^~ /cut/public {
root /var/www;
try_files $uri $uri/ /cut/index.php$is_args$args;
location ~* \.php(/|$) {
fastcgi_pass php:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Nginx location configuration (subfolders)
To access a path like /var/www/mysite/manage/public
with a URI like /manage
, you will need to use alias
rather than root
. See this document for details.
I am assuming that you need to run PHP from both roots, in which case you will need two location ~ \.php
blocks, see example below. If you have no PHP within /var/www/mysite/static
, you can delete the unused location
block.
For example:
server {
listen 80;
server_name example.org;
error_log /usr/local/etc/nginx/logs/mysite/error.log;
access_log /usr/local/etc/nginx/logs/mysite/access.log;
root /var/www/mysite/static;
index index.html;
location / {
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
location ^~ /manage {
alias /var/www/mysite/manage/public;
index index.php;
if (!-e $request_filename) { rewrite ^ /manage/index.php last; }
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
}
The ^~
modifier causes the prefix location to take precedence over regular expression locations at the same level. See this document for details.
The alias
and try_files
directives are not together due to this long standing bug.
Be aware of this caution in the use of the if
directive.
nginx: how to serve file from a subfolder of root?
Fixed adding
location /fonts {
root /var/www/prod.revisions;
}
Nginx with php in one subdirectory
It's likely that your fastcgi_param
should be /www$fastcgi_script_name
because the variable is the full URI request. Source.
Related Topics
Select Statement with Fetch_Array in MySQLi Prepared Statements
How to Allow a User to Download a File Which Is Stored Outside of the Webroot
Sort Array by Date in Descending Order by Date in PHP
Nohup on Windows, Exec Without Waiting for Finish
Output (Echo/Print) Everything from a PHP Array
Merge Two Png Images with PHP Gd Library
Unable to Scrape Content from a Website
JavaScript or Jquery Doesn't Work in Dynamically Generated Content
How Does Codeigniter Receive the Ajax Post Data in Controller
Differences Between a While Loop and a for Loop in PHP
Check If Username Exists in MySQL Table via PHP
Laravel Store Array in Session
Laravel 5 - Session Doesn't Work
Php: How to Load the Content of a Web Page into a Variable