Nginx Location Configuration for Subfolders
Nginx server configuration allows for the routing of requests to specific locations based on URI patterns. This question explores how to configure Nginx to access subfolders within a specified path, specifically /static and /manage, with different URI prefixes.
To configure access to the /static folder at the root URI (/), define a location block with the root set to the /static subfolder:
location / { root /var/www/mysite/static; index index.html; }
For the /manage folder, which contains a Slim PHP framework, the root path must be set to the public subfolder where the index.php file resides. Since the public subfolder is a location, an alias directive is used instead of root:
location ^~ /manage { alias /var/www/mysite/manage/public; index index.php; }
Additionally, if PHP files are present in both /static and /manage folders, two location ~ .php blocks are required:
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; }
and
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 ensures that the prefix location for /manage takes precedence over the regular expression location ~ .php$.
The above is the detailed content of How to Configure Nginx Location Blocks for Subfolders with Different URI Prefixes?. For more information, please follow other related articles on the PHP Chinese website!