
我一直在到處尋找,以找出為什麼我的 nginx 伺服器在一個網域上導致 301 重定向,而不是在另一個網域上。我在此伺服器上以 apache 樣式配置了兩個網站(網站可用)。假設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;
}
}
Domain2.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.com 的DNS 未變更為伺服器時,使用listen 8000 對domain2 的網站進行了一次測試。當測試為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 設定檔中刪除時,我會看到預設的 nginx 頁面,這意味著它工作正常。一旦我將 www.domain2.com 加入到設定中的 server_name 指令中,就會再次啟動 301 重定向。
我還在 nginx.conf 的 http 部分下添加了 port_in_redirect ,這似乎沒有做任何事情。
有人知道這裡發生了什麼事嗎?
編輯: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
.