nginx 将无www转换为有www失败,为什么?
server {
listen 80;
server_name www.testsite.com;
index index.html index.htm index.php;
root /alidata/www/testsite;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
#α¾²Ì¬¹æÔò
include /alidata/server/nginx/conf/rewrite/phpwind.conf;
access_log /alidata/log/nginx/access/http.log;
}
server {
server_name testsite.com;
return 301 http://www.testsite.com$request_uri;
}
带www,访问成功,不带访问失败,浏览器提示
414 Request-URI Too Large
发现浏览器实际地址为
http://testsite.com/www.testsite.com/www.testsite.com/.....(不断重复)
应该怎么解决
server {
listen 80;
server_name testsite.com;
rewrite ^/(.*) http://www.testsite.com/$1 permanent;
}
server {
listen 80;
server_name 127.0.0.1;
server {
listen 80;
server_name www.testsite.com;
location / {
root html;
index index.html index.htm;
proxy_pass http://127.0.0.1:服务端口号;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
我推荐你用上面这个配置,至于www访问问题,你需要到域名控制台去进行域名解析,加入一个@解析,也称为泛解析,之后就额可以正常访问。
server {
listen 80;
server_name testsite.com;
location / {
proxy_pass www.testsite.com;
}
}
试试这个吧,改完了记得nginx -s reload