How to Configure Nginx Location Blocks for Subfolders with Different URI Prefixes?

Mary-Kate Olsen
Release: 2024-11-25 13:35:13
Original
541 people have browsed it

How to Configure Nginx Location Blocks for Subfolders with Different URI Prefixes?

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;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template