使用外部文件将旧 URL 映射到新 URL - 配置无效

stw*_*sel 4 nginx mapping

我正在将旧网站移至基于 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。现在我有两个问题:

  1. 有没有一种方法,以获得更详细的错误,一个,告诉我什么是错的,哪里
  2. 怎么了?我需要更改包含文件的内容吗?我需要移动该部分吗?

非常感谢帮助。

Ale*_*Ten 6

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)


stw*_*sel 5

我想通了,感谢阿列克谢的指点。有一个 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 个条目的魅力一样。