Linux之web服务器
1.web服务器简介
http协议请求的工作流程:
1.终端客户在web浏览器地址栏输入访问地址;2.web浏览器请求DNS服务器把域名解析成web服务器的IP地址 ;3.web浏览器将端口号(默认是80)从访问地址(URL)中解析出来; 4.web浏览器通过解析后的ip地址及端口号与web服务器之间建立一条TCP连接 ;5.建立TCP连接后,web浏览器向web服务器发送一条HTTP请求报文 ;6.web服务器响应并读取浏览器的请求信息,然后返回一条HTTP响应报文;7.web服务器关闭HTTP连接,关闭TCP连接,web浏览器显示访问的网站内容到屏幕上。
https协议相对于http的加密:
对称加密 我的理解是: 首先我们需要协商使用什么算法(AES), 接着我们通过相同的密钥对数据进行加密即可实现:明文 + 密钥 ==> 密文 密文 + 密钥 ==> 明文。由此我们可以看出, 都使用相同的密钥,一旦密钥泄露, 那么数据相当于公开。 优点: 效率高。 改进:能不能提前把密钥加密,只能由服务端才能解密。
非对称加密 是网络通信的基石, 保证了非对称加密 密钥的安全。 首先, 非对称加密需要 私钥 和 公钥, 公钥加密的数据只能由私钥解开, 而私钥加密的数据只能由公钥解开。 公钥存储在客服端, 私钥存储在服务端, 永远不对外暴漏。 流程:首先客服端请求公钥, 服务端响应公钥, 之后客服端通过公钥加密数据传输, 服务端通过私钥解密获取信息。 存在的安全隐患: 我们的公钥是直接进行传输的, 那么黑客就可以得到我们的公钥从而获取信息。
非对称加密 + 对称加密
客服端请求公钥响应后, 客服端通过公钥可以加密一串随机数,作为以后信息传输对称加密的密钥, 而这串随机数也只能由服务端的私钥才能解析
2.基于http的静态网站
在配置前使用nmtui进入配置IP界面,添加要使用的IP
实验1.搭建一个显示“hello jio!”的服务器
[root@localhost ~]# echo "hello jio!" > /usr/share/nginx/html/index.html
[root@localhost ~]# curl localhost
hello jio!
[root@localhost ~]# curl 192.168.168.50
hello jio!
实验2.搭建两个基于不同IP地址访问的网站
(主机位为100和200,首页目录为/www/ip/100,/www/ip/200,首页内容分别为this is100,this is 200)
[root@localhost ~]# nmcli connection up ens160 #激活
[root@localhost ~]# mkdir -pv /www/ip/{100,200}
[root@localhost ~]# echo this is 100 > /www/ip/100/index.html
[root@localhost ~]# echo this is 200 > /www/ip/200/index.html
[root@server html]# setenforce 0 #selinux
[root@localhost ~]# vim /etc/nginx/conf.d/ip.conf
server {
listen 192.168.168.100:80;
root /www/ip/100;
location / {
}
}
server {
listen 192.168.168.200:80;
root /www/ip/200;
location / {
}
}
[root@localhost ~]# systemctl restart nginx #如果出错了按照提示复制命令检查问题
[root@localhost ~]# curl 192.168.168.100
this is 100
[root@localhost ~]# curl 192.168.168.200
this is 200
实验3.搭建两个基于不同端口访问的网站
(端口分别为80和10000,首页目录为/www/port/80,/www/port/10000,首页内容分别为the port is 80,the port is 10000)
[root@localhost ~]# mkdir -pv /www/port/{80,10000}
[root@localhost ~]# echo the port is 80 > /www/port/80/index.html
[root@localhost ~]# echo the port is 10000 > /www/port/10000/index.html
[root@localhost ~]# vim /etc/nginx/conf.d/test_port.conf
server {
listen 192.168.168.153:80;
root /www/port/80;
location / {
}
}
server {
listen 192.168.168.153:10000;
root /www/port/10000;
location / {
}
}
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# curl 192.168.168.153:10000
the port is 10000
[root@localhost ~]# curl 192.168.168.153:80
the port is 80
实验4.搭建两个基于不同域名访问的网站
(域名分别为www.jio.com和zj.de.wang,首页目录为/www/jio,/www/jioz,首页内容分别为my name is jio,my name is jioz)
[root@localhost ~]# mkdir /www/{jio,jioz}
[root@localhost ~]# echo my name is jio > /www/jio/index.html
[root@localhost ~]# echo my name is jioz > /www/jioz/index.html
[root@localhost ~]# vim /etc/nginx/conf.d/name.conf
server {
listen 192.168.168.154:80;
server_name www.jio.com;
root /www/jio;
location / {
}
}
server {
listen 192.168.168.154:80;
server_name jio.de.wang jiojio.de.wang;
root /www/jioz;
location / {
}
}
[root@localhost ~]# vim /etc/hosts
192.168.168.154 www.jio.com jio.de.wang jiojio.de.wang
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# curl www.jio.com
my name is jio
[root@localhost ~]# curl jio.de.wang
my name is jioz
[root@localhost ~]# curl jiojio.de.wang
my name is jioz
实验5.基于虚拟目录和用户控制的web网站
[root@localhost ~]# mkdir /www/real/
[root@localhost ~]# echo real-virtual > /www/real/index.html
[root@localhost ~]# vim /etc/nginx/conf.d/virtual.conf
server {
listen 192.168.168.155:80;
root /usr/share/nginx/html;
location /real {
alias /www/real;
}
}
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# curl 192.168.168.155/real/
real-virtual
[root@localhost ~]# vim /etc/nginx/conf.d/virtual1.conf
server {
listen 192.168.168.155:80;
root /usr/share/nginx/html;
location /real {
alias /www/real;
auth_basic on;
auth_basic_user_file /etc/nginx/conf.d/auth-password;
}
}
[root@localhost ~]# dnf install httpd-tools -y
[root@localhost ~]# htpasswd -cb /etc/nginx/conf.d/auth-password user1
123456
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# curl 192.168.168.155/real/ -u user1
Enter host password for user 'user1':
real-virtual
[root@localhost ~]# curl user1:123456@192.168.168.155/real/
real-virtual
3.基于https的静态网站
实验.https的网站配置
[root@localhost ~]# echo 我是更加安全的https > /www/https/index.html
[root@localhost ~]# cd /etc/pki/tls/certs/
[root@localhost certs]# openssl genrsa -out https.key #私钥文件
[root@localhost certs]# openssl req -utf8 -new -key https.key -x509 -days 100 -out https.crt #公钥文件
[root@localhost ~]# vim /etc/nginx/conf.d/test_https.conf
server {
listen 192.168.168.156:443 ssl;
root /www/https;
ssl_certificate /etc/pki/tls/certs/https.crt;
ssl_certificate_key /etc/pki/tls/certs/https.key;
location / {
}
}
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# curl -k https://192.168.168.156
https
我是更加安全的https