Home > Backend Development > PHP Tutorial > How Can I Configure Nginx to Serve Nested Subfolders from Different URIs?

How Can I Configure Nginx to Serve Nested Subfolders from Different URIs?

Barbara Streisand
Release: 2024-11-29 02:28:13
Original
165 people have browsed it

How Can I Configure Nginx to Serve Nested Subfolders from Different URIs?

Nginx Location Configuration for Nested Subfolders

In the context of Nginx configuration, accessing subfolders with specific URIs can be challenging. This is where location directives come into play.

Consider the following scenario: you have a directory structure like /var/www/mysite/ containing two subfolders, /static and /manage. You wish to access /static via the root URI (e.g., "http://example.org/") and /manage via "/manage" (e.g., "http://example.org/manage").

Let's break down the provided Nginx configuration:

server {
  listen 80;
  server_name example.org;
  ...

  # Static folder location
  location / {
    root $uri/static/;
    index index.html;
  }

  # Manage folder location (attempt 1)
  location /manage {
    root $uri/manage/public;
    try_files $uri /index.php$is_args$args;
  }

  # PHP processing location
  location ~ \.php {
    ...
  }
}
Copy after login

While the / location works correctly, the /manage location fails. This is because the root directive is incorrect. To use a subfolder within an alias, alias should be used instead of root.

The updated location for /manage should be as follows:

location ^~ /manage {
  alias /var/www/mysite/manage/public;
  ...
}
Copy after login

With these modifications, Nginx will correctly serve the static files from /static at the root URI and the dynamic content from /manage at "/manage".

The above is the detailed content of How Can I Configure Nginx to Serve Nested Subfolders from 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