为什么我的nginx位置规则不起作用?

为什么我的nginx位置规则不起作用?,nginx,Nginx,我的目标是匹配以/key/0/开头的请求: /key/0/(.*) 转到page20.html。但当请求发生时,服务器会以404响应: 198.81.214.48 - - [07/Mar/2013:19:13:46 +0000] "GET /key/0/i-digit/index.php/sample-sites HTTP/1.0" 404 169 "-" "-" 我重新启动了nginx,但仍然没有效果。这是我的配置文件: server { listen 80 defau

我的目标是匹配以
/key/0/
开头的请求:

/key/0/(.*)
转到
page20.html
。但当请求发生时,服务器会以404响应:

198.81.214.48 - - [07/Mar/2013:19:13:46 +0000] "GET /key/0/i-digit/index.php/sample-sites HTTP/1.0" 404 169 "-" "-"
我重新启动了nginx,但仍然没有效果。这是我的配置文件:

server {
        listen   80 default;
        server_name  localhost;

        access_log  /var/log/nginx/localhost.access.log;
        location ~ ^/key/0/(.*)$ {
                alias  page20.html#$1;

        }

        location / {
                root   /var/www;
                index  index.html index.htm;
        }

        location /doc {
                root   /usr/share;
                autoindex on;
                allow 127.0.0.1;
                deny all;
        }

        location /images {
                root   /usr/share;
                autoindex on;
        }

        #error_page  404  /404.html;

        # redirect server error pages to the static page /50x.html
        #
        #error_page   500 502 503 504  /50x.html;
        #location = /50x.html {
        #       root   /var/www/nginx-default;
        #}

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
                #proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
                #fastcgi_pass   127.0.0.1:9000;
                #fastcgi_index  index.php;
                #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
                #includefastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
                #deny  all;
        #}
}

alias
指令正在本地文件路径上查找该页面。然而,您在该位置块中没有任何根集,我确实看到您在
location/
中将根集设置为
/var/www

所以我相信你要找的是,不是alias

因此,请尝试以下方法:

location ~ ^/key/0/(.*)$ {
    set $target $1;
    rewrite /(.*)$ page20.html#$target permanent;
}