Nginx重写。需要帮助才能只重写0-9a-z-字符

时间:2012-09-23 12:13:08

标签: 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

而不是返回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;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试以下配置。

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

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;
}