2009-07-07 9 views
18

couchdbのプロキシフロントエンドとしてnginx 0.6.32を実行しています。私はデータベースにrobots.txtを持っていて、http://www.example.com/prod/_design/mydesign/robots.txtという名前でアクセス可能です。同様のURLに動的に生成されたsitemap.xmlもあります。robots.txtとsitemap.xmlのURLにリダイレクトするようにnginxを設定するには

私は、次の設定を試してみました:

server { 
    listen 80; 
    server_name example.com; 
    location/{ 
    if ($request_method = DELETE) { 
    return 444; 
    } 
    if ($request_uri ~* "^/robots.txt") { 
    rewrite ^/robots.txt http://www.example.com/prod/_design/mydesign/robots.txt permanent; 
    } 

    proxy-pass http://localhost:5984; 
    proxy_redirect off; 
    proxy_set_header Host $host; 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
} 

これは、リダイレクトとして動作しているように見えますが、簡単な方法はありますか?

答えて

49

location/{ 
    rewrite ^/robots\.txt$ /static/robots.txt last; 
    ... 
} 
それとも、単に独自の場所に置くことができる - これも動作するはず、と 少し良い行うこと

location /robots.txt { 
    alias /Directory-containing-robots-file/robots.txt; 
} 
+1

これは最も簡単な解決策のようですが、完璧に動作します。 – Gattster

+3

'directory-containing-file'と言うと、私はあなたがファイル名*を含んでいると思いますか?私はエイリアス/ www/static;を試しましたが、スラッシュの有無にかかわらず失敗しました。私が 'alias /www/static/robots.txt;'を使用したとき、うまくいきました。 –

+1

John C - いいえ、ディレクトリだけです。 'alias/directory;を置くのではなく' root/opt/directory /; 'とすることもできます。 – PlanetUnknown

5

http:// ...でリライトするとリダイレクトが行われます。あなたはおそらく探しているでしょう:

 
rewrite ^/robots.txt /prod/_design/mydesign/robots.txt last; 
+0

ニーズ仕事に等しいです。 xml $ /index.php?xxxxxxxxx permanent; } ' –

1

最初のパラメータは正規表現ですので、おそらくエスケープする必要があります。行末に一致します。

NginxHttpRewriteModule

3

location /robots.txt { alias /var/www/django/mysite/static/robots.txt; } 
18

私はあなたが場所を設定したいと思う。重要な部分は "="ディレクティブです。以下のようにそれを設定

location = /robots.txt { 
    alias /your_full_path/robots.txt ; 
} 

(私は=が含まれていなかった場合には、それはまだデフォルトのnginxのルートの場所に行ってきました)

は、すべての/robots.txt*要求は外に読み取ることが原因となります/ var/foo。だから、/robots.txt.bingはディスクの/var/foo/robots.txt.bingの読み込みを試みます。 "^〜"は、要求の開始時に正規表現一致であることを示します。

location ^~ /robots.txt { 
    root /var/foo; 
} 
0

robots.txtは以下が私のために働いた、/var/www/mysite/static/robots.txtに位置していることを考慮すると: `場所= /sitemap.xml { \t書き換え^ /サイトマップ\を:

location = /robots.txt { 
    root /var/www/mysite/static/; 
} 
関連する問題