如何从 htaccess 应用 nginx 重写

如何从 htaccess 应用 nginx 重写

我按照此处的“完美服务器”在 ubuntu 12.10 上安装了 ispconfig/nginx: http://www.howtoforge.com/forums/showthread.php?t=61141

我需要从 .haccess 文件将重写规则应用于 nginx,文件内容如下:

重写规则^(.)/(.)-video_(.*).html$ musicvideo.php?vid=$3

重写规则 ^([^/])-视频_(。).html$ watch.php?vid=$2

我使用了在线转换器并得到了以下输出:

location / {
rewrite ^/(.*)/(.*)-video_(.*).html$ /musicvideo.php?vid=$3;

rewrite ^/([^/]*)-video_(.*).html$ /watch.php?vid=$2;
}

然后我将代码放在./sites-available/default服务器块下并重新启动 nginx 但出现错误:

duplicate location "/" in /etc/nginx/sites-enabled/default:133
nginx: configuration file /etc/nginx/nginx.conf test failed

如果我将重写规则放在现有位置内:

location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ /index.html;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
rewrite ^/(.*)/(.*)-video_(.*).html$ /musicvideo.php?vid=$3;

rewrite ^/([^/]*)-video_(.*).html$ /watch.php?vid=$2;
}

然后什么也没有发生!有人可以指出我做错了什么吗?

答案1

nginx 中的指令顺序很重要。尝试将重写规则移至顶部/ location

相关内容