nginx基于域名的虚拟主机配置

网站发布测试

更多干货

1、检查文件
cd /soft/nginx
[root@master nginx]# grep html conf/nginx.conf 
            root   html;
            index  index.html index.htm;
        #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   html;
        #    root           html;
    #        root   html;
    #        index  index.html index.htm;
    #        root   html;
    #        index  index.html index.htm;
2、新建测试文件
[root@master html]# vi index.html
<html>
<head><title>wolf,s Ngnix server web.</title></head>
<body>
Hi,I am wolf. my web address is
<a href="http://blog.csdn.net/yujin2010good">http://blog.csdn.net/yujin2010good</a>
</body>
</html>


======================================================================================


配置基于单个域名的nginx.conf (/etc/nginx/nginx.conf   /usr/local/nginx/html/www)
egrep -v "# | ^$" nginx.conf.default >nginx.conf
vi nginx.conf


worker_processes  1;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.wolf.com;
        location / {
            root   htmli/www;
            index  index.html index.htm;
        }
    }
}
cd objs/
[root@slave objs]# ./nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@slave nginx-1.8.0]# mkdir html/www -p
[root@slave nginx-1.8.0]# echo "http://www.wolf.com" >html/www/index.html
[root@slave nginx-1.8.0]# cat html/www/index.html
http://www.wolf.com
[root@slave nginx-1.8.0]# ./objs/nginx
[root@slave nginx-1.8.0]# ps -ef | grep nginx
root       5019   2440  0 Jun10 pts/1    00:00:00 vi nginx.conf
root       6254      1  0 01:46 ?        00:00:00 nginx: master process ./objs/nginx
nginx      6255   6254  0 01:46 ?        00:00:00 nginx: worker process
root       6257   2440 10 01:47 pts/1    00:00:00 grep nginx
[root@slave nginx-1.8.0]# netstat -lntp | grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      6254/nginx          
[root@slave nginx-1.8.0]# 


客户端测试
echo “192.168.0.203 www.wolf.com” >>/etc/hosts
[root@slave nginx-1.8.0]# tail -f /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
“192.168.0.203 www.wolf.com”


=================================================================================================
配置基于多个域名的虚拟主机(/etc/nginx/nginx.conf   /usr/local/nginx/html/www)
vi nginx.conf
worker_processes  1;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.wolf.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
server {
        listen       80;
        server_name  bbs.wolf.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
server {
        listen       80;
        server_name  blog.wolf.com;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }
}


建立目录
mkdir /usr/local/nginx/html/bbs /usr/local/nginx/html/blog -p
echo "http://bbs.wolf.com" >/usr/local/nginx/html/bbs/index.html
echo "http://blog.wolf.com" >/usr/local/nginx/html/bolg/index.html


cat /usr/local/nginx/html/bbs/index.html
cat /usr/local/nginx/html/bolg/index.html


shell实现以上步骤
for n in www blog bbs;do mkdir -p /usr/local/nginx/html/$n;echo "http://${n}.wolf.com" >/usr/local/nginx/html/$n/index.html;cat /usr/local/nginx/html/$n/index.html;done


http://www.wolf.com
http://blog.wolf.com
http://bbs.wolf.com
[root@slave objs]# tree /usr/local/nginx/html/
/usr/local/nginx/html/
|-- 50x.html
|-- bbs
|   `-- index.html
|-- blog
|   `-- index.html
|-- index.html
`-- www
    `-- index.html
[root@slave objs]# ./nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
修改hosts文件,客户端测试也需要修改
[root@slave objs]# ./nginx -s reload


客户端测试
http://www.wolf.com
http://blog.wolf.com
http://bbs.wolf.com
服务器端测试
curl www.wolf.com
curl bbs.wolf.com
curl blog.wolf.com


此配置文件可以分开写


vi nginx.conf


worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
include extra/www.conf
include extra/bbs.conf
include extra/blog.conf
}
把server部分单独写一个文件
cat extra/www.conf
    server {
        listen       80;
        server_name  www.wolf.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
cat extra/bbs.conf
server {
        listen       80;
        server_name  bbs.wolf.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
cat extra/blog.conf
server {
        listen       80;
        server_name  blog.wolf.com;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_27384769/article/details/81211879