背景:
在 www 虚拟主机站点基础上新增一个 bbs 虚拟主机站点。
1 备份配置文件
- [[email protected] conf]# pwd / application / nginx / conf[[email protected] conf]# cp nginx.conf {,
- .oldboy.2017.0819
- }
2 编辑 nginx.conf 配置文件,新增 bbs 虚拟主机站点
- [[email protected]/* */ conf]# vim nginx.conf.oldboy.20170819
- 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.etiantian.org;
- location / {
- root html;
- index index.html index.htm;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
- }
新增 bbs 站点后的 nginx.conf 配置文件为:
- [[email protected]/* */ conf]# cat 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.etiantian.org;
- location / {
- root html/www;
- index index.html index.htm;
- }
- }
- server {
- listen 80;
- server_name bbs.etiantian.org;
- location / {
- root html/bbs;
- index index.html index.htm;
- }
- }
- }
修改配置文件后,需要建立站点相关文件
- [[email protected]/* */ conf]# mkdir ../html/{www,bbs} -p #相当于在/application/nginx/html/目录下建立www和bbs两个目录
- [[email protected]/* */ conf]# echo "www" >../html/www/index.html #在www首页index.html中写入www字母
- [[email protected]/* */ conf]# echo "bbs" >../html/bbs/index.html #在bbs首页index.html中写入bbs字母
3 nginx 配置文件检查语法查看是否有错误:/application/nginx/sbin/nginx -t
- [[email protected] conf]# / application / nginx / sbin / nginx - t #自检查看是否有错误nginx: the configuration file / application / nginx - 1.6.3 //conf/nginx.conf syntax is ok
- nginx: configuration file / application / nginx - 1.6.3 //conf/nginx.conf test is successful
根据提示 nginx 配置文件检查语法结果成功
4 nginx 服务平滑重启
- [[email protected] conf]# / application / nginx / sbin / nginx - s reload
apche,nginx 等如果平滑重启 reload 失败那么就用 stop 和 start,禁止用 restart
- [[email protected]/* */ conf]# /application/nginx/sbin/nginx -s stop #stop是带-s stop
- [[email protected]/* */ conf]# /application/nginx/sbin/nginx #start是直接nginx就是start
5 检查 nginx 服务 80 端口是否启动
- [[email protected]/* */ conf]# lsof -i :80
- COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
- nginx 5784 root 6u IPv4 23562 0t0 TCP *:http (LISTEN)
- nginx 5785 www 6u IPv4 23562 0t0 TCP *:http (LISTEN)
6 在 linux 中验证 nginx 服务是否可用(当然也可以再 windows 中验证)
6.1 在管理机 m01 机器上面本机 hosts 文件中添加 172.16.1.8 的 dns 解析 172.16.1.8 web01 www.etiantian.org bbs.etiantian.org
- [[email protected]/* */ ~]# cat /etc/hosts
- 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
- ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
- 172.16.1.5 lb01
- 172.16.1.6 lb01
- 172.16.1.7 web02
- 172.16.1.8 web01 www.etiantian.org bbs.etiantian.org
- 172.16.1.51 de01 db01.etiantian.org
- 172.16.1.31 nfs01
- 172.16.1.41 backup
- 172.16.1.61 m01
6.2 然后 ping www.etiantian.org 和 bbs.etiantian.org 看是否可用解析到 172.16.1.8 这个 ip 地址。
- [[email protected]/* */ ~]# ping -c4 www.etiantian.org
- PING web01 (172.16.1.8) 56(84) bytes of data.
- 64 bytes from web01 (172.16.1.8): icmp_seq=1 ttl=64 time=0.629 ms
- 64 bytes from web01 (172.16.1.8): icmp_seq=2 ttl=64 time=0.432 ms
- 64 bytes from web01 (172.16.1.8): icmp_seq=3 ttl=64 time=0.439 ms
- 64 bytes from web01 (172.16.1.8): icmp_seq=4 ttl=64 time=0.654 ms
- --- web01 ping statistics ---
- 4 packets transmitted, 4 received, 0% packet loss, time 3003ms
- rtt min/avg/max/mdev = 0.432/0.538/0.654/0.105 ms
- [[email protected]/* */ ~]# ping -c4 bbs.etiantian.org
- PING web01 (172.16.1.8) 56(84) bytes of data.
- 64 bytes from web01 (172.16.1.8): icmp_seq=1 ttl=64 time=0.896 ms
- 64 bytes from web01 (172.16.1.8): icmp_seq=2 ttl=64 time=0.459 ms
- 64 bytes from web01 (172.16.1.8): icmp_seq=3 ttl=64 time=0.782 ms
- 64 bytes from web01 (172.16.1.8): icmp_seq=4 ttl=64 time=0.445 ms
- --- web01 ping statistics ---
- 4 packets transmitted, 4 received, 0% packet loss, time 3005ms
- rtt min/avg/max/mdev = 0.445/0.645/0.896/0.199 ms
6.3 curl 两个虚拟主机站点名字,看是否可用返回对应的 www 和 bbs 内容(首页 index.html 里面的内容)
- [[email protected]/* */ ~]# curl www.etiantian.org
- www
- [[email protected]/* */ ~]# curl bbs.etiantian.org
- bbs
7 windows 浏览器中验证
7.1 在 windows 主机 hosts 文件中添加 dns 解析
在 C:\Windows\System32\drivers\etc\hosts 中添加一条主机解析:
10.0.0.8 www.etiantian.org bbs.etiantian.org
7.2 在浏览器中分别输入 www.etiantian.org 和 bbs.etiantian.org 可以返回对应的首页内容 www 和 bbs,说明新增域名虚拟主机成功。
来源: http://www.bubuko.com/infodetail-2269808.html