
私は、なぜ私の nginx サーバーが 1 つのドメインで 301 リダイレクトを引き起こし、他のドメインでは引き起こさないのかを解明するために、あちこち調べてきました。このサーバーには、Apache スタイル (sites-available) で 2 つのサイトが設定されています。domain1.com と domain2.com とします。PHP-FPM も実行しています。設定は次のとおりです。
ドメイン1.com
server {
listen 80; ## listen for ipv4; this line is default and implied
server_name domain1.com www.domain1.com;
root /var/www/domain1.com;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
ドメイン2.com
server {
listen 80; ## listen for ipv4; this line is default and implied
server_name domain2.com www.domain2.com;
root /home/mike/www;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
これらは、server_name と root を除いて、基本的にまったく同じであることに気付くでしょう。それ以外は同一です。ここで状況を説明します。domain1.com は問題なく正常に動作します。domain2 のサイトは、domain2.com の DNS がサーバーに変更されていないときに listen 8000 を使用して 1 回テストされました。IP:8000 としてテストすると正常に動作したため、ドメインに変更し、ポートを 80 に変更し、DNS を変更しました。その後、サーバーの電源が完全にオンオフされ、nginx と php5-fpm はおそらく 100 回再起動されました。
ブラウザで domain2.com にアクセスすると、自動的に domain2.com:8000 にリダイレクトされます。web-sniffer.net を使用して HTTP ヘッダーを確認すると、301 リダイレクトが返されます。リダイレクトを設定したことはありませんし、このサーバーのどこにも 301 が設定されていません。本当に困っているのは、www.domain2.com にアクセスすると、server_name の下の nginx 構成ファイルから www.domain2.com を削除しても、デフォルトの nginx ページが表示されることです。これは正常に動作していることを意味します。構成の server_name ディレクティブに www.domain2.com を追加すると、301 リダイレクトが再び開始されます。
nginx.conf の http セクションに port_in_redirect off を追加しましたが、何も効果がなかったようです。
ここで何が起こっているのか誰か分かりますか?
編集: curl-vhttp://domain2.com
* About to connect() to domain2.com port 80 (#0)
* Trying 162.243.XXX.XXX... connected
> GET / HTTP/1.1
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: domain2.com
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.1.19
< Date: Mon, 13 Jan 2014 17:37:07 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< X-Powered-By: PHP/5.3.10-1ubuntu3.9
< X-Pingback: http://162.243.XXX.XXX:8000/xmlrpc.php
< Location: http://domain2.com:8000/
<
* Connection #0 to host domain2.com left intact
* Closing connection #0
答え1
サーバーからの実際の出力を見ずに推測するのは少し難しいので、curl -v http://example2.com/
実際に何が返されるかを確認するには を使用することをお勧めしますが、ブラウザがリダイレクトをキャッシュしているのではないかと思います。以前にも見たことがあります。
別のブラウザを使用しても同じリダイレクトが表示されますか?
アップデート: 診断出力によると、これは ではなく、Wordpress がリダイレクトを発行したことが原因であることは明らかですnginx
。