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.
How to make root directory subfolders available through nginx?
Since nobody was able to reply to me, here's how I finally got it working.
My FIRST ISSUE was with permissions and file ownership:
/var/www
was owned by the root
user and group, as were all its subdirectories and files. nginx thus wasn't able to read any file - even if configured right -, except weirdly my index.html from /var/www/my-site
(which I don't quite understand as of now).
To remedy this, I first changed the owner and group of /var/www/my-site
and all its subdirectories and files to www-data
, instead of root
. www-data
seems to be the default user, defined by nginx. It should be specified at the top of the nginx.conf from /etc/nginx
as user www-data
.
sudo chown -R www-data:www-data /var/www/my-site
After that I also set the SetGID permission flags s
for the www-data
group and the /var/www/my-site
directory and its subdirectories (not the files though).
sudo chmod g+s /var/www/my-site /var/www/my-site/images /var/www/my-site/css
This means that all future files, created in /var/www/my-site
and its subdirectories will get the group ownership applied to them (not any new subdirectories though).
Now, I still needed to allow the new group to write to /var/www/my-site
and its subdirectories with the appropriate permissions (i.e. rwxrwxrx
= 775):
sudo chmod -R 775 /var/www/my-site
Lastly, I added myself - the non-root
user - to the www-data
group, in order to be able to upload (or write) files with for instance SFTP.
sudo usermod -a -G www-data [my username]
a
means "append", and -G www-data
"to the existing group www-data
".
My SECOND ISSUE was in the my-site.net config from /etc/nginx/sites-available
, which was initially copied from /etc/nginx/sites-available/default
.
Turns out that in my case, I only needed to specify the relevant subdirectories of /var/www/my-site
, which is the nginx root (cf. server {...}
block), as location
s.
I already had /var/www/my-site/images
set up with this snippet that is meant to prevent hot or deep linking of images:
location /images/ {
valid_referers none blocked www.my-site.net my-site.net;
if ($invalid_referer) {
return 403;
}
}
All that remained was to specify another location for /var/www/my-site/css
:
location /css/ {
}
Finally, I saved the config and reloaded nginx and everything worked!
Nginx set root to subfolder
If your directory structure follows your URI structure, you should probably set the root
to /var/www/html
at the server
level and use /var/www/html/download
for one location
only.
For example:
server {
...
root /var/www/html;
location / {
root /var/www/html/download;
fancyindex on;
}
location /account/ {
... # PHP stuff
}
location /css/ { }
location /js/ { }
location = /robots.txt { }
location = /terms.html { }
The last four blocks can be empty, as they inherit the value of root
from the surrounding block. See this document for details.
nginx: location of static content within a subfolder
I figured it out myself. Deleting everything out of the location /memorygame
block did the trick. Nginx does what I was trying to achieve by default.
nginx location with unknown subfolder depth
If you set the root
like in your example to /var/www/html/public/image
, then a request to /image/user/foo.jpg
will end up at /var/www/html/public/image/image/user/foo.jpg
which is undesired. So I don't think you should touch root
(it must stay pointed to public
dir).
Simply:
location ^/image/.*/.*\.(jpg|jpeg|gif|png|css|ico)$ {
expires 30d;
}
will do the job.
If your filenames are somehow hashed/versioned (e.g. foo.123.jpg
), you can use immutable
for aggressive caching:
location ^/image/.*/.*\.(jpg|jpeg|gif|png|css|ico)$ {
immutable on;
}
Setup example.com/subdirectory/ in Nginx
Put the following into server
section:
location /subdirectory {
root /var/www/site/web;
}
Or other solution which is working when /url_directory
and /local_directory
doesn't equal:
location /subdirectory {
alias /var/www/site/web/subdirectory;
}
See more on nginx/location, nginx/root and nginx/alias.
Related Topics
How to Run a .Bat File from PHP
How to Check If a Date Is in a Given Range
Pdo Binding Values For MySQL in Statement
I Have an Array of Integers, How to Use Each One in a MySQL Query (In PHP)
Achieve Hierarchy, Parent/Child Relationship in an Effective and Easy Way
Limit Keyword on MySQL With Prepared Statement
How Do PHP Sessions Work? (Not "How Are They Used")
How to Test a Url For 404 in PHP
Remove Excess Whitespace from Within a String
Upload Photo to Album With Facebook'S Graph API
PHP With MySQL 8.0+ Error: the Server Requested Authentication Method Unknown to the Client
How to Convert a String to a Number in PHP