我正在将旧网站移至基于 nginx 的新主机。为了保留 URL(完全更改),我有一个列出映射的文件。我想将它与map 模块一起使用。
在里面/etc/nginx/nginx.conf http{ ... }
我指的是 PERL 和小写函数:
#Include PERL and have a function for lowercasing the incoming URL
perl_modules perl/lib;
# function to lowercase incoming strings like the URL
perl_set $uri_lowercase 'sub {
my $r = shift;
my $uri = $r->uri;
$uri = lc($uri);
return $uri;
}';
Run Code Online (Sandbox Code Playgroud)
我的站点配置在文件中/etc/nginx/sites-enabled/notessensei
(Thx Alexey 指出)如下所示:
server {
listen www.notessensei.com:80;
root /home/stw/www;
index index.html;
server_name www.notessensei.com notessensei.com;
location / {
map $uri_lowercase $new {
include /home/stw/www/blognginx.map;
}
if ($new) {
rewrite ^ $new redirect;
}
}
error_page 404 /blog/404.html;
}
Run Code Online (Sandbox Code Playgroud)
映射文件blognginx.map
如下所示:
/blog/d6plinks/shwl-6bv35s /blog/2005/04/garbage-in-.html;
/blog/d6plinks/shwl-6c6ggp /blog/2005/05/just-me.html;
/blog/d6plinks/shwl-6c6gh4 /blog/2005/05/it-is-quotmake-your-own-caption-quot-time.html;
/blog/d6plinks/shwl-6c997j /blog/2005/05/big-business-wwjd.html;
/blog/d6plinks/shwl-6ca5qb /blog/2005/05/domino-on-solaris-anyone.html;
/blog/d6plinks/shwl-6ce65j /blog/2005/05/going-places-vietnam.html;
/blog/d6plinks/shwl-6ce6c9 /blog/2006/02/umsys-as-old-as-unix-sort-of.html;
Run Code Online (Sandbox Code Playgroud)
大约1300行。当我做 a 时,service nginx configtest
我会失败。当我不使用该include
语句时,我会得到一个 OK。现在我有两个问题:
非常感谢帮助。
map
指令必须是直接子http
块。
语法:映射字符串 $variable { ... }
默认: -
上下文:http
在您的情况下,您必须将它放在server
块旁边,因为您的自定义配置文件包含http
在 nginx.conf 的块内。
map $uri_lowercase $new {
include /home/stw/www/blognginx.map;
}
server {
...
}
Run Code Online (Sandbox Code Playgroud)
我想通了,感谢阿列克谢的指点。有一个 4 倍的问题:
service nginx configtest
告诉小于nginx -t
,再次阿列克谢指出我在那里现在我/etc/nginx/nginx.conf
在该http {}
部分还有 2 行:
## Increase bucket for big redirects
map_hash_bucket_size 256;
map_hash_max_size 4092;
Run Code Online (Sandbox Code Playgroud)
该/etc/nginx/sites-enabled/notessensei
文件如下所示:
map $uri_lowercase $new {
include /home/stw/www/blognginx.map;
}
server {
listen www.notessensei.com:80;
root /home/stw/www;
index index.html index.htm;
server_name www.notessensei.com notessensei.com;
location / {
if ($new) {
return 301 $new;
}
}
error_page 404 /blog/404.html;
}
Run Code Online (Sandbox Code Playgroud)
如果您想看到它的实际效果,请从wissel.net选择任何博客条目并将 uri 部分应用到notessensei.com - 就像拥有 > 1200 个条目的魅力一样。
归档时间: |
|
查看次数: |
9091 次 |
最近记录: |