我一直在寻find处找出为什么我的nginx服务器在一个域上导致301redirect,而不是其他。 我有两个站点configuration在这个服务器上的apache风格(网站可用)。 让我们说,domain1.com和domain2.com。 我也运行PHP-FPM。 这里是configuration
Domain1.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 8000testing了domain2的站点。 当它被testing为IP:8000时,它工作正常,所以我把它改成了域名,把端口改成了80,并且修改了DNS。 服务器已经完全重新启动,nginx和php5-fpm已经重启了100次。
如果我在浏览器中访问domain2.com,它会自动redirect到domain2.com:8000。 如果我使用web-sniffer.net并查看HTTP头,它将返回301redirect。 我从来没有设置redirect,并且在此服务器上的任何地方都没有301的设置。 实际上,如果我访问www.domain2.com,当我从server_name下的nginxconfiguration文件中删除它,我会看到默认的nginx页面,这意味着它工作正常。 将www.domain2.com添加到config中的server_name指令后,再次启动301redirect。
我也在nginx.conf中的http部分中添加了port_in_redirect,这似乎没有任何作用。
有谁知道这里发生了什么?
编辑:curl -v http://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
没有看到从服务器的实际输出是有点难以猜测,我build议使用curl -v http://example2.com/看看实际返回的,但我猜你的浏览器caching了redirect。 我以前见过。
使用不同的浏览器是否显示相同的redirect?
更新 :根据您的诊断输出,似乎清楚这是由WordPress发布的redirect,而不是nginx 。