pereorga / minimalist-web-notepad

Minimalist Web Notepad
https://notes.orga.cat
1.13k stars 282 forks source link

xxx?raw can't be used in Nginx #59

Closed nibazshab closed 1 year ago

nibazshab commented 1 year ago

Apache all is OK, but Nginx not.

location / {
    rewrite ^/([a-zA-Z0-9_-]+)$ /index.php?note=$1$is_args$args;
}

Use $is_args$args, url xxx and argument ?raw will become url xxx?raw, it then be matched by !preg_match('/^[a-zA-Z0-9_-]+$/', $_GET['note']) and be rewrited. So just need remove it.

location / {
    rewrite ^/([a-zA-Z0-9_-]+)$ /index.php?note=$1;
}

I've already tested it.

So maybe https://github.com/pereorga/minimalist-web-notepad/commit/38459b4d713d4a18098ab669166239030851487d is not a good idea

pereorga commented 1 year ago

Thanks, I'll revert this then

pereorga commented 1 year ago

Reverted in https://github.com/pereorga/minimalist-web-notepad/commit/071d5b9fc9de9c62e29cf430f2acdcd7f216a998

mouism commented 1 month ago
location ~* ^/notes/([a-zA-Z0-9_-]+)$ {
    try_files $uri /notes/index.php?note=$1;
}

With the current nginx configuration when there are directories, the ?raw parameter cannot be passed. In other words, when accessing /notes/test?raw, it only becomes /index.php?note=test While the previous configuration would actually result in /index.php?note=test?raw

location ~* ^/notes/([a-zA-Z0-9_-]+)$ {
    try_files $uri /notes/index.php?note=$1$is_args$args;
}

Because $is_args has a value of ? when there are parameters Therefore, the configuration logic that can actually pass parameters might be like this:

location ~* ^/notes/([a-zA-Z0-9_-]+)$ {
    set $args_append "";
    if ($is_args) {  # if 块有问题,改为 map
        set $args_append "&$args"; 
    }
    try_files $uri /notes/index.php?note=$1$args_append; 
}

However, the if block can be problematic. If Is Evil So, try using the map directive in the HTTP block context to create a new variable $args_append The map directive is defined in the http block of the configuration and can create more reliable conditional variables at an early stage of request processing.

http {
    map $is_args $args_append {
        default "";
        "?" "&$args";
    }
    server {
        # ... other server configurations ...
        location ~* ^/notes/([a-zA-Z0-9_-]+)$ {
            try_files $uri /notes/index.php?note=$1$args_append;
        }
    }
}

This way, parameters can be passed normally without errors. Of course, it seems that directly writing it as:

location ~* ^/notes/([a-zA-Z0-9_-]+)$ {
    try_files $uri /notes/index.php?note=$1&$args;
}

also doesn't seem to have any issues. After testing, both methods work. I don't have a better solution. This is just for discussion.