LNMP下Nginx如何实现域名301重定向
时间:2014-05-20 11:27 来源:linux.it.net.cn 作者:IT网
将zhangjun.sh.cn直接转向www.zhangjun.sh.cn
直接用VI编辑器打开 /usr/local/nginx/conf/vhost/www.zhangjun.sh.cn.conf 这个配置文件,代码如下
server
{
listen 80;
server_name www.zhangjun.sh.cn zhangjun.sh.cn blog.zhangjun.sh.cn;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/www.zhangjun.sh.cn;
include typecho.conf;
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
access_log off;
}
现在把 server_name www.zhangjun.sh.cn zhangjun.sh.cn blog.zhangjun.sh.cn; 这行的 zhangjun.sh.cn blog.zhangjun.sh.cn 删掉。再在后面加上下面代码:
server {
server_name zhangjun.sh.cn blog.zhangjun.sh.cn;
rewrite ^(.*) http://www.zhangjun.sh.cn$1 permanent;
}
改完后reload或重启nginx就生效了。虽然做了301重定向,但是DNS处还是要做A记录解析的。
(责任编辑:IT)
将zhangjun.sh.cn直接转向www.zhangjun.sh.cn 直接用VI编辑器打开 /usr/local/nginx/conf/vhost/www.zhangjun.sh.cn.conf 这个配置文件,代码如下
server { listen 80; server_name www.zhangjun.sh.cn zhangjun.sh.cn blog.zhangjun.sh.cn; index index.html index.htm index.php default.html default.htm default.php; root /home/wwwroot/www.zhangjun.sh.cn; include typecho.conf; location ~ .*\.(php|php5)?$ { try_files $uri =404; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fcgi.conf; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } access_log off; } 现在把 server_name www.zhangjun.sh.cn zhangjun.sh.cn blog.zhangjun.sh.cn; 这行的 zhangjun.sh.cn blog.zhangjun.sh.cn 删掉。再在后面加上下面代码: server { server_name zhangjun.sh.cn blog.zhangjun.sh.cn; rewrite ^(.*) http://www.zhangjun.sh.cn$1 permanent; } 改完后reload或重启nginx就生效了。虽然做了301重定向,但是DNS处还是要做A记录解析的。 (责任编辑:IT) |