AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 599372
Accepted
stwissel
stwissel
Asked: 2014-05-30 01:50:52 +0800 CST2014-05-30 01:50:52 +0800 CST 2014-05-30 01:50:52 +0800 CST

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

  • 772

我正在将一个旧网站移动到一个新的基于 nginx 的主机。为了保留 URL(完全改变),我有一个列出映射的文件。我想将它与地图模块一起使用。

在里面/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;
    }';

我的站点配置,在文件中/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;

}

映射文件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;

大约1300行。当我做 a 时,service nginx configtest我会失败。当我不使用该include语句时,我得到一个 OK。现在我有两个问题:

  1. 有没有办法得到更详细的错误,告诉我哪里出了问题?
  2. 怎么了?我需要更改包含文件的内容吗?我需要移动部分吗?

非常感谢您的帮助。

nginx
  • 2 2 个回答
  • 9991 Views

2 个回答

  • Voted
  1. Best Answer
    Alexey Ten
    2014-05-30T02:13:43+08:002014-05-30T02:13:43+08:00

    map指令必须是直接子http块。

    语法:映射字符串 $variable { ... }

    默认: -

    上下文:http

    在您的情况下,您必须将它放在server块旁边,因为您的自定义配置文件包含http在 nginx.conf 的块内。

    map $uri_lowercase $new {
      include /home/stw/www/blognginx.map;
    }
    
    server {
      ...
    }
    
    • 7
  2. stwissel
    2014-05-30T08:37:50+08:002014-05-30T08:37:50+08:00

    我想通了,谢谢阿列克谢的指点。有一个 4 折的问题:

    • 地图声明不在正确的位置,Alexey 指出
    • 该文件内部有一些错误(几行中缺少空格)
    • 地图尺寸太小(请参阅下面的解决方案)
    • service nginx configtest告诉不到,nginx -t阿列克谢又指给我看

    现在我/etc/nginx/nginx.conf的部分中有 2 行http {}:

    ## Increase bucket for big redirects
    map_hash_bucket_size 256;
    map_hash_max_size 4092;
    

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

    如果您想查看它的实际效果,请从wissel.net中选择任何博客条目并将 uri 部分应用到notessensei.com - 就像拥有超过 1200 个条目的魅力一样。

    • 5

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve