Nginx重写。仅重写0-9a-z字符需要帮助吗

Nginx重写。仅重写0-9a-z字符需要帮助吗,nginx,rewrite,Nginx,Rewrite,我的nginx虚拟主机文件中有以下指令: try_files $uri $uri/ /profile.php?nickname=$uri&$args; 它工作正常,但我只想重写0-9a-z字符,这样用户就有了不错的配置文件地址,例如: http://www/mydomain.com/john 但是nginx重写了一切。例如,如果我输入地址: http://www.mydomain.com/non-existing-holder/nonexisting-filename.html 它

我的nginx虚拟主机文件中有以下指令:

try_files $uri $uri/ /profile.php?nickname=$uri&$args;
它工作正常,但我只想重写0-9a-z字符,这样用户就有了不错的配置文件地址,例如:

http://www/mydomain.com/john
但是nginx重写了一切。例如,如果我输入地址:

http://www.mydomain.com/non-existing-holder/nonexisting-filename.html
它没有返回404错误,而是重写了所有内容

有没有办法只重写0-9-a-z字符

server {
    listen 80;
    server_name www.mydomain.com;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    root /var/www/mydomain;
    error_page 404 /4044.html;
    location / {
        index index.html index.htm index.php;
        try_files $uri $uri/ /profile.php?nickname=$uri&$args;
        rewrite ^/forum/([0-9-]+)/([0-9a-z-]+).html$ /forum.php?tid=$1&title=$2 last;
    }

    location ~ \.php$ {
        try_files  $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

您可以尝试以下配置

  • 请求/testUSER将返回404
  • 请求/forum/100/something.html将转到/forum.php?tid=100&title=something
  • 对/昵称的请求将转到/profile.php?昵称=昵称


Igor,我尝试过这个,但King重写了现有目录,例如:(现有目录)重写为profile.php?昵称=论坛。工作正常。不幸的是,如果你的根目录中有自定义目录,你只需要列出它们。如果我相信,你不能两全其美。您可以有动态昵称或动态目录。我添加了另一个位置,所以试试这个。我有很多costom目录(12),所以最好不要更改它,这样如果目录不存在,nginx就会重写任何字符。我希望它不会引起任何其他问题。我认为您误解了位置/块的
try\u文件部分<代码>try_文件$uri$uri//profile.php…$args
将首先尝试查找实际存在的文件,然后将URI传递到
profile.php
。这就是为什么你没有404。您的
profile.php
必须执行404处理。这也是为什么重写规则永远不会运行。。。因为URI被发送到
profile.php
,并且不再符合重写规则。@Brendan请将其作为答案发送,以便进一步操作。
location / {
    index index.html index.htm index.php;
    rewrite ^/([0-9a-z-]+)/?$ /profile.php?nickname=$1&$args last;
    try_files $uri $uri/ =404;
}
location /forum {
    rewrite ^/forum/([0-9-]+)/([0-9a-z-]+).html$ /forum.php?tid=$1&title=$2 last;
    alias /var/www/mydomain/forum;
}