Home > Backend Development > PHP Tutorial > How to Efficiently Configure Nginx Location Blocks for Serving Subfolders with Different URIs?

How to Efficiently Configure Nginx Location Blocks for Serving Subfolders with Different URIs?

Susan Sarandon
Release: 2024-11-24 01:11:15
Original
230 people have browsed it

How to Efficiently Configure Nginx Location Blocks for Serving Subfolders with Different URIs?

Nginx Location Configuration for Subfolders

In the Nginx configuration, specific folder paths can be mapped to different URIs for improved organization and routing. A common scenario is configuring access to subfolders within a directory.

Consider a path structure like /var/www/myside/ where two subfolders, /static and /manage, exist. The goal is to access these subfolders via the URIs / (for /static) and /manage (for /manage) while ensuring proper routing for PHP files.

An initial sample Nginx configuration may look like this:

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;

  location /manage {
    root $uri/manage/public;

    try_files $uri /index.php$is_args$args;
  }

  location / {
    root $uri/static/;

    index index.html;
  }

  location ~ \.php {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    fastcgi_index index.php;
    fastcgi_pass 127.0.0.1:9000;
  }
}
Copy after login

However, this configuration may not work correctly for accessing /manage. The solution is to use the alias directive instead of root when accessing a subfolder with a different URI.

Here is a revised Nginx configuration:

server {
    ...

    location ^~ /manage {
        alias /var/www/mysite/manage/public;
        index index.php;

        if (!-e $request_filename) { rewrite ^ /manage/index.php last; }

        location ~ \.php$ {
            ...
        }
    }

    ...
}
Copy after login

With this modification, the configuration maps /static to / using root and /manage to /manage using alias. Additionally, the try_files directive and if directive ensure that requests for non-existent files within these subfolders are properly handled.

By combining the alias and root directives, along with proper use of location blocks, Nginx can be configured to effectively serve content from subfolders with specific URIs.

The above is the detailed content of How to Efficiently Configure Nginx Location Blocks for Serving Subfolders with Different URIs?. 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