1、更改nginx的网站主页
[root@localhost data]# vim /apps/nginx/conf/nginx.conf — 在nginx的配置文件中修改
加入下面ailun字段的内容
location / {
root html;
index index.html index.htm;
}
location /ailun {
root /data/;
index index.html index.htm;
}

在根目录下创建一个ailun的目录,往里面重定向一个“yegeyu”
然后nginx -s reload
访问网页
http://192.168.107.190/ailun/

特别注意:此处访问格式只能是192.168.107.190**/ailun/**
原因 :192.168.107.190/ailun 和 192.168.107.190/ailun/ 的区别
语义层面
192.168.107.190/ailun:通常表示服务器根目录下名为 ailun 的一个文件或资源。客户端期望获取这个特定名称的资源内容。
192.168.107.190/ailun/:明确表示服务器根目录下的 ailun 目录。客户端期望访问该目录下的默认资源(例如 index.html 或 index.htm)。
服务器处理行为层面
192.168.107.190/ailun:
若 ailun 是一个文件,服务器会直接返回该文件的内容。
若 ailun 是一个目录,在默认配置下,服务器会将该请求重定向到 192.168.107.190/ailun/,返回一个 301 或 302 状态码。
192.168.107.190/ailun/:服务器会将其当作一个目录请求来处理,接着查找该目录下的默认索引文件(由 index 指令指定,如 index.html 或 index.htm),并返回该索引文件的内容。若未找到索引文件,服务器可能会返回一个目录列表(取决于 autoindex 指令的配置),或者返回 403 禁止访问错误。
我们在data的目录下创建一个ailun的**文件**
[root@localhost data]# cat ailun
ailun
[root@localhost data]#
#现在直接访问就没有问题了
[root@localhost data]# curl 192.168.107.190/ailun
ailun
[root@localhost data]# curl 192.168.107.190/ailun
ailun
[root@localhost data]# curl 192.168.107.190/ailun
ailun
[root@localhost data]# curl 192.168.107.190/ailun
ailun
同时,我们有没有办法不写/ailun/ 写/ailun 的同时又能访问到网页呢?
#此时,我们需要在curl 之后加一个-L的选项 表示从/ailun 跳转到/ailun/
[root@localhost data]# mkdir ailun
[root@localhost data]# ls
ailun
[root@localhost data]# echo "ailunyegeyu" > ailun/index.html
[root@localhost data]# tree
.
└── ailun
└── index.html
1 directory, 1 file
[root@localhost data]# curl -L 192.168.107.190/ailun
ailunyegeyu
[root@localhost data]#
2、构建虚拟主机 (在一台主机上有2个网页)
我们有三种方法可以实现构建虚拟主机,如下
2.1 基于域名
[root@localhost data]# vim /apps/nginx/conf/nginx.conf
加入这一行
include /apps/nginx/conf.d/*.conf

在/apps/nginx/下创建conf.d文件
[root@localhost conf.d]# pwd
/apps/nginx/conf.d
[root@localhost conf.d]#
创建我们所需要的域名文件:例如 “ailun” “yegeyu”
[root@localhost conf.d]# vim ailun.conf
[root@localhost conf.d]# vim yegeyu.conf
写入下面的字段
server{
listen 80;
server_name www.ailun.com;
root /data/ailun/;
}
~
server{
listen 80;
server_name www.yegeyu.com;
root /data/yegeyu/;
}
检查是否书写正确
[root@localhost conf.d]# nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@localhost conf.d]#
#刷新配置文件
[root@localhost conf.d]# nginx -s reload
[root@localhost conf.d]#
修改本机的hosts文件
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 www.ailun.com
127.0.0.1 www.yegeyu.com
在/data的目录下创建相应的文件
[root@localhost data]# mkdir yegeyu
[root@localhost data]# ls
ailun yegeyu
[root@localhost data]# echo "yegeyu" > yegeyu/index.html
ailun与yegeyu的操作是一样的
测试访问
[root@localhost data]# curl http://www.yegeyu.com
yegeyu
[root@localhost data]# curl http://www.ailun.com
ailunyegeyu
[root@localhost data]# curl http://www.yegeyu.com
yegeyu
[root@localhost data]# curl http://www.yegeyu.com
yegeyu
[root@localhost data]# curl http://www.ailun.com
ailunyegeyu
[root@localhost data]# curl http://www.ailun.com
ailunyegeyu
[root@localhost data]# curl http://www.ailun.com
ailunyegeyu
[root@localhost data]#
2.2基于端口号
有了上面的基础,我们的操作就比较简单了
我们只需要修改这两个配置文件



测试访问
[root@localhost conf.d]# curl 192.168.107.190:8080
ailunyegeyu
[root@localhost conf.d]# curl 192.168.107.190:5050
yegeyu
[root@localhost conf.d]# curl 192.168.107.190:5050
yegeyu
[root@localhost conf.d]# curl 192.168.107.190:5050
yegeyu
[root@localhost conf.d]# curl 192.168.107.190:5050
yegeyu
[root@localhost conf.d]# curl 192.168.107.190:5050
yegeyu
[root@localhost conf.d]# curl 192.168.107.190:5050
yegeyu
[root@localhost conf.d]# curl 192.168.107.190:8080
ailunyegeyu
[root@localhost conf.d]#
2.3 基于IP地址
我们新添加一个IP地址为ens33:0 并查看是否应用成功
[root@localhost conf.d]# ifconfig ens33:0 192.168.107.110 netmask 255.255.255.0
[root@localhost conf.d]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.107.190 netmask 255.255.255.0 broadcast 192.168.107.255
inet6 fe80::77f1:d253:9067:dd77 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1a:76:fb txqueuelen 1000 (Ethernet)
RX packets 760204 bytes 1032504360 (984.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 297946 bytes 18488002 (17.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.107.110 netmask 255.255.255.0 broadcast 192.168.107.255
ether 00:0c:29:1a:76:fb txqueuelen 1000 (Ethernet)
到相应的ailun.conf yegeyu.conf 中更改相关的配置文件
[root@localhost conf.d]# ls
ailun.conf yegeyu.conf
[root@localhost conf.d]# vim ailun.conf
[root@localhost conf.d]# vim yegeyu.conf
server{
listen 192.168.107.190:80;
server_name www.ailun.com;
root /data/ailun/;
}
server{
listen 192.168.107.110:80;
server_name www.yegeyu.com;
root /data/yegeyu/;
}
~
检测语法是否正确,刷新nginx的配置文件,检测访问
[root@localhost conf.d]# nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@localhost conf.d]# nginx -s reload
[root@localhost conf.d]# curl 192.168.107.190
ailunyegeyu
[root@localhost conf.d]# curl 192.168.107.110
yegeyu
[root@localhost conf.d]#
6892




被折叠的 条评论
为什么被折叠?



