nginx 虚拟主机

基于域名的虚拟主机

创建站点目录

[root@nginx conf]# cd /usr/local/nginx/html/
[root@nginx html]# pwd
/usr/local/nginx/html
[root@nginx html]# mkdir www
[root@nginx html]# mkdir blog
[root@nginx html]# mkdir bbs
[root@nginx html]# ls
50x.html bbs blog index.html www

修改nginx.conf

[root@nginx html]# cd /usr/local/nginx/conf/
server {
listen ;
server_name www.httpd.com; location /{
root html/www;
index index.html index.htm;
}
}
server {
listen ;
server_name blog.httpd.com; location /{
root html/blog;
index index.html index.htm;
}
}
server {
listen ;
server_name bbs.httpd.com; location /{
root html/bbs;
index index.html index.htm;
}
}

创建站点测试页、重启服务

[root@nginx conf]# echo "<h1>www.http.com</h1>" > /usr/local/nginx/html/www/index.html
<h1>www.http.com</h1> /usr/local/nginx/html/www/index.html
[root@nginx conf]# echo "<h1>bbs.http.com</h1>" > /usr/local/nginx/html/bbs/index.html
<h1>bbs.http.com</h1> /usr/local/nginx/html/bbs/index.html
[root@nginx conf]# echo "<h1>blog.http.com</h1>" > /usr/local/nginx/html/blog/index.html
<h1>blog.http.com</h1> /usr/local/nginx/html/blog/index.html
[root@nginx conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx conf]# /usr/local/nginx/sbin/nginx -s reload

添加本机hosts

[root@nginx bbs]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
:: localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.161.134 www.httpd.com
192.168.161.134 bbs.httpd.com
192.168.161.134 blog.httpd.com

测试

[root@nginx bbs]# curl www.httpd.com
<h1>www.http.com</h1>
[root@nginx bbs]# curl bbs.httpd.com
<h1>bbs.http.com</h1>
[root@nginx bbs]# curl blog.httpd.com
<h1>blog.http.com</h1>
上一篇:2014年度辛星解读css第四节


下一篇:Codeforces Round #324 (Div. 2)解题报告