要求:
1.web服务器的主机ip:172.25.254.100
2.web服务器的默认访问目录为/var/www/html
默认发布内容为default‘s page
3.站点news.timinglee.org默认发布目录为/var/www/virtual/timinglee.org/news
默认发布内容为 news.timinglee.org
4.站点login.timinglee.org在被访问时必须强制走加密协议,其默认发布目录为/var/www/virtual/timinglee.org/login
默认发布内容为login.timinglee.org
2.web服务器的默认访问目录为/var/www/html
默认发布内容为default‘s page
下载nginx服务并启用:
[root@localhost ~]# dnf install nginx
[root@localhost ~]# systemctl enable --now nginx.service
修改默认发布路径:
[root@localhost ~]# vim /etc/nginx/nginx.conf
server {
listen 80;
listen [::]:80;
server_name _;
#root /usr/share/nginx/html;
root /var/www/html;
index index.html;
[root@localhost ~]# echo "defult's page" > /var/www/html/index.html
[root@localhost ~]# curl 172.25.254.100
defult's page
3.站点news.timinglee.org默认发布目录为
/var/www/virtual/timinglee.org/news
默认发布内容为 news.timinglee.org
[root@localhost ~]# cd /etc/nginx/conf.d/
编写文件:
[root@localhost conf.d]# vim vhosts.conf
server{
listen 80;
server_name news.timinglee.org;
root /var/www/virtual/timinglee.org/news;
index index.html;
}
创建目录:
[root@localhost conf.d]# mkdir -p /var/www/virtual/timinglee.org/news
输出重定向:
[root@localhost conf.d]# echo "news.timinglee.org" > /var/www/virtual/timinglee.org/news/index.html
重启:
[root@localhost conf.d]# systemctl restart nginx.service
解析:
[root@localhost conf.d]# vim /etc/hosts
172.25.254.100 server100.timinglee.org news.timinglee.org
测试:
[root@localhost conf.d]# curl news.timinglee.org
news.timinglee.org
4.站点login.timinglee.org在被访问时必须强制走加密协议,其默认发布目录为/var/www/virtual/timinglee.org/login
默认发布内容为login.timinglee.org
创建目录:
[root@localhost nginx]# mkdir -p /etc/nginx/certs
认证:
[root@localhost nginx]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /etc/nginx/certs/timinglee.org.key -x509 -days 356 -out /etc/nginx/certs/timinglee.org.crt
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:ShanXi
Locality Name (eg, city) [Default City]:Xi'an
Organization Name (eg, company) [Default Company Ltd]:timinglee
Organizational Unit Name (eg, section) []:web
Common Name (eg, your name or your server's hostname) []:www.easylee.org
Email Address []:lee@easylee.org
强制加密
[root@localhost conf.d]# vim vhosts.conf
server{
listen 80;
server_name login.timinglee.org;
rewrite ^/(.*)$ https://login.timinglee.org/$1 permanent;
}
server{
listen 443 ssl;
server_name login.timinglee.org;
ssl_certificate "/etc/nginx/certs/timinglee.org.crt";
ssl_certificate_key "/etc/nginx/certs/timinglee.org.key";
root /var/www/virtual/timinglee.org/login;
index index.html;
}
创建目录:
[root@localhost conf.d]# mkdir -p /var/www/virtual/timinglee.org/login
[root@localhost conf.d]# echo "login.timinglee.org"> /var/www/virtual/timinglee.org/login/index.html
添加解析:
[root@localhost nginx]# vim /etc/hosts
172.25.254.100 server100.timinglee.org news.timinglee.org www.timinglee.org login.timinglee.org
[root@localhost nginx]# curl login.timinglee.org
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
测试可在浏览器中进行