組み込みのPerlサイトマップ プロキシ

私達はビジネスをホストするCMSを実行しています。

各webサイトは独自のドメイン上でホストされ、各webサイトはリクエストされた時にその場でsitemap.xmlを動的に生成します。

これらのサイトマップはurlをサーチエンジンに入力するのに便利です。https://www.sitemaps.org/index.html

通常、これらのサイトをサーチエンジンに登録するのはサイト所有者/webマスターの仕事です。ある人はしますが、またある人はしません。

私たちは全てのこれらのサイトを動的に提供したいと思い、これはNGINXとperlモジュールを使って行う方法です。

注意: IANASEO以外の簡単な方法があるかもしれません。

目的: 1. 全てのドメインのマスターマップをリストし、それらをサーチエンジンのスパイダーに検索させる中央サーバ。2. ドメインを横断した登録。マスターマップ内のドメインは、中央サーバがサイトマップを提供できなければなりません。

robots.txt (これも動的なスクリプトです) サイトマップの変更: http://sitemaps.example.com/domain-name.com-sitemap.xml

つまり、robots.txt は以下のようなものになります。

User-agent: *
Disallow: /cgi-bin/
Disallow: /tmp/
Disallow: /cache/
Disallow: /class/
Disallow: /images/
Disallow: /include/
Disallow: /install/
Disallow: /kernel/
Disallow: /language/
Disallow: /templates_c/
Disallow: /themes/
Disallow: /uploads/
sitemap: http://sitemaps.worldsoft-cms.info/ispman.net-sitemap.xml

domain-name.com はもちろん正しい名前に置き換えられます。これは全てのサイトマップのリクエストをNGINXが動作している中央サーバに送信します。

nginx.conf (関係する部分のみ):

http {
  include       mime.types;
  default_type  application/octet-stream;

  perl_modules lib;
  perl_require Sitemap.pm;

  keepalive_timeout  65;

  server {
    listen       8090;
    server_name  sitemaps.worldsoft-cms.info;

    location / {
      root   html;
      index  index.html index.htm;
      if (!-f $request_filename) {
        rewrite ^/(.*)-sitemap.xml$ /sitemap/$1 last;
        # If a file matches somethingsomething-sitemap.xml
        # then redirect it to /sitemap/somethingsomething
        # here somethingsomething will match a domain
      }
    }

    location /sitemap {
      perl Sitemap::handler;
    }
  }
}

lib/Sitemap.pm:

package Sitemap;
use nginx;
use LWP::Simple;

our $basedir="/usr/local/sitemapnginx/html";

sub handler {
  my $r=shift;
  my $uri=$r->uri;
  $uri=~ s!^/*sitemap/*!!g;
  $uri=~ s!/.*!!g;
  # now $uri has just the domain name such as nginx.com

  my $sitemap_url="http://$uri/sitemap.xml";
  # Get the sitemap from something like http://ispman.net/sitemap.xml (this is dynamic and fresh)

  my $sitemap_data=get($sitemap_url);
  # if the result does not include this string, return 404 Not found.
  return 404 if $sitemap_data !~ m/urlset/;

  # if found, then cache it.
  my $sitemap_file="$basedir/$uri-sitemap.xml";
  open "F", ">$sitemap_file";
  print F $sitemap_data;
  close("F");
  $r->send_http_header("application/xml");
  # return the cached file
  $r->sendfile($sitemap_file);
  $r->flush;
  return OK;
}

1;

master-map の例:


    <urlset xmlns="https://www.sitemaps.org/index.htmlschemas/sitemap/0.9"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://www.sitemaps.org/index.htmlschemas/sitemap/0.9
    https://www.sitemaps.org/index.htmlschemas/sitemap/0.9/sitemap.xsd">

<url><loc>http://sitemaps.worldsoft-cms.info/demo-domain0.de-sitemap.xml</loc></url>
<url><loc>http://sitemaps.worldsoft-cms.info/demo-domain1.de-sitemap.xml</loc></url>
<url><loc>http://sitemaps.worldsoft-cms.info/demo-domain2.de-sitemap.xml</loc></url>
<url><loc>http://sitemaps.worldsoft-cms.info/demo-domain3.de-sitemap.xml</loc></url>
<url><loc>http://sitemaps.worldsoft-cms.info/demo-domain4.de-sitemap.xml</loc></url>
<url><loc>http://sitemaps.worldsoft-cms.info/demo-domain5.de-sitemap.xml</loc></url>
<url><loc>http://sitemaps.worldsoft-cms.info/demo-domain6.de-sitemap.xml</loc></url>
<url><loc>http://sitemaps.worldsoft-cms.info/demo-domain7.de-sitemap.xml</loc></url>
<url><loc>http://sitemaps.worldsoft-cms.info/demo-domain8.de-sitemap.xml</loc></url>
<url><loc>http://sitemaps.worldsoft-cms.info/demo-domain9.de-sitemap.xml</loc></url>
...
...
...
... thousands of lines later ...
</urlset>
inserted by FC2 system