
それで、私は過去 8 時間これを理解しようとしてきましたが、行き詰まっているようです...
次の Nginx 設定ファイルがあります:
server_tokens off;
upstream php-handler {
server unix:/var/run/php5-fpm.sock;
}
server {
listen 80;
server_name domain.net;
access_log /var/log/nginx/domain.net-access.log;
error_log /var/log/nginx/domain.net-error.log;
location ~* \.(jpg|jpeg|gif|png|js|css|ico|eot|woff|ttf|svg|cur|htc|xml|html|tgz)$ {
expires 24h;
}
root /var/www/html/domain.net;
index index.php;
location ~ ^/cars/sale(.*) {
add_header X-Robots-Tag "noindex, nofollow" always;
try_files $uri $uri/ /index.php;
}
location ~ ^/(?:\.htaccess|config){
deny all;
}
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php(?:$|/) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass php-handler;
fastcgi_read_timeout 120s;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_ignore_client_abort on;
fastcgi_param SERVER_NAME $http_host;
}
}
問題は、何を試しても、「/cars/sale」の場所の X-Robots-Tag が追加されないことです。これは、リクエストが最終的な「.php」の場所に渡され、以前に追加されたヘッダーが忘れられるためだと推測しています。more_set_headers を使用せずに、特定の場所にのみこのヘッダーを追加する方法はありますか?
答え1
実際には次のように実行できます:
map $request_uri $robot_header {
default "";
~^/cars/sale(.*) "noindex, nofollow";
~^/bikes/sale(.*) "noindex, nofollow";
~^/motorbikes/sale(.*) "noindex, nofollow";
}
しかし、すべてがこのパターンに従う場合は、次のようにすればよいのです。
map $request_uri $robot_header {
default "";
~^/(.+?)/sale(.*) "noindex, nofollow";
}
設定がかなり乱雑です。正規表現を使用する場合、Nginx はリクエストに対応するために一致する最初のブロックを選択するため、リストする順序が重要です。
車のロケーション ブロック内に別の PHP ブロックをネストし、そこにヘッダーを追加できます。PHP ハンドラーをアップストリーム サーバーとして指定すると、毎回すべての fastcgi パラメーターを含める必要がなくなり、整理しやすくなります。
答え2
それで... ぐっすり眠った後、解決策を思いつきました。とても汚い修正ですが、私の特定のケースでは文字通りこれが唯一有効な方法です。
http ブロックに以下を追加します:
map $request_uri $robot_header1 {
default "";
~^/cars/sale(.*) "noindex, nofollow";
}
map $request_uri $robot_header2 {
default "";
~^/bikes/sale(.*) "noindex, nofollow";
}
map $request_uri $robot_header3 {
default "";
~^/motorbikes/sale(.*) "noindex, nofollow";
}
(これらは例として 3 つだけですが、実際には http ブロックに含めるファイル内に約 200 個生成されています)
そして、サーバー ブロックに以下を追加しました:
add_header X-Robots-Tag $robot_header1;
add_header X-Robots-Tag $robot_header2;
add_header X-Robots-Tag $robot_header3;
...
また、Nginx パラメータ「variables_hash_bucket_size」を 512 に増やす必要がありました。これは、必要な変数の数が多いため、デフォルトの 64 では不十分だからです。そのため、これが他の誰かの役に立つことを願っています...