下面以本站为例,做了LNMP环境下的nginx域名301重定向教程。
直接用WinSCP编辑器打开 /usr/local/nginx/conf/vhost/www.net8000.com.conf 这个配置文件,代码如下:
server
{
listen 80;
server_name net8000.com www.net8000.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/net8000.com;
include typecho.conf;
location ~ .*\.(php|php5)?$
{
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 net8000.com www.net8000.com; 这行的net8000.com 删掉。
再在后面加上下面代码:
server {
server_name net8000.com;
rewrite ^(.*) http://www.net8000.com$1 permanent;
}
最终效果见下图:
改完后重启nginx就生效了。虽然做了301重定向,但是DNS处还是要做A记录解析的。完成后,试试效果,打开net8000.com就会自动跳转到www.net8000.com。