Skip to content

Fix directory alias handling in Nginx - #84

Open
iliaross wants to merge 1 commit into
masterfrom
fix/nginx-directory-aliases
Open

Fix directory alias handling in Nginx#84
iliaross wants to merge 1 commit into
masterfrom
fix/nginx-directory-aliases

Conversation

@iliaross

Copy link
Copy Markdown
Member

Hi Jamie,

This PR fixes Nginx directory aliases that were incorrectly being used as filesystem paths in rewrite rules. It converts alias destinations to internal URL paths, prevents rewrite loops, and ensures regular redirects are processed before aliases.

It also keeps alias listing and deletion working, including existing aliases.

This fixes applications with a separate public directory, such as Roundcube 1.7.

Tested on Debian 12 with different alias and redirect configurations.

This PR addresses Nginx Directory aliases that were incorrectly used as filesystem paths in Nginx rewrite rules. This converts them to internal URL paths, prevents rewrite loops, and ensures regular redirects are processed before aliases.

It also keeps alias listing and deletion working, including existing aliases.

This fixes applications with a separate public directory, such as Roundcube 1.7.

Tested on Debian 12 with different alias and redirect configurations.
@jcameron

Copy link
Copy Markdown
Collaborator

Wow this looks really complex! Can you add some more details of exactly what kind of fixes it is doing, and when?

@iliaross

Copy link
Copy Markdown
Member Author

Wow this looks really complex! Can you add some more details of exactly what kind of fixes it is doing, and when?

Yeah, sure!

It looks complex because Virtualmin must be able to create, find, and delete the same Nginx alias safely.

So we care about the following:

  • When an alias is created, it converts the directory path into a URL path Nginx can use. For example, /home/user/public_html/app/public_html becomes /app/public_html;
  • It prevents the alias from rewriting the same URL again and causing a loop;
  • It supports all alias options such as root paths, subdirectories, exact matches, ignored subpaths, trailing slashes, and HTTP/HTTPS limits;
  • It places normal redirects before aliases. This matters because an alias can stop Nginx from checking rules below it;
  • It allows Virtualmin to find and delete aliases it created, including older aliases;
  • It leaves custom Nginx rewrite rules alone;
  • It rejects alias directories outside the website's public_html, because they cannot be converted safely;
  • For PHP websites, it avoids sending an empty PATH_INFO value after an alias is created.

And as you remember, Roundcube 1.7 exposed the problem because now its public files are inside a separate public_html directory. And, the fix applies to any application with the same directory structure.

@jcameron

Copy link
Copy Markdown
Collaborator

Ah so this fixes a case where an alias points to a sub-directory of the original directory?

@iliaross

Copy link
Copy Markdown
Member Author

Ah so this fixes a case where an alias points to a sub-directory of the original directory?

Yeah, that is one example, but the fix is more general. The alias API receives a filesystem path, while an Nginx rewrite needs a URL path.

This change converts alias destinations under the website's document root into the correct internal URL.

It handles both root and subdirectory aliases, and the other supported alias options. It also prevents the generated rewrite from matching itself again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants