1.重定向
- (1)服务端配置
cd /usr/local/nginx/conf/切换到nginx服务器的配置目录下
vim nginx.conf 编辑配置文件
当访问域名 www.westos.org时,实际上访问的是 /web/index.html文件
128 server {
129 listen 80; # 监听80端口
130 server_name www.westos.org; # 定义域名
131
132 location / {
133 root /web;
134 index index.html;
135 }
136 }
mkdir /web建立目录
vim /web/index.html
写入www.westos.org's page
- (2)客户端测试
vim /etc/hosts做本地解析
172.25.78.11 server1 www.westos.org
curl www.westos.org # 测试成功
得到www.westos.org's org
2.添加443端口,实现http到https的加密
需要编译ssl模块
cd nginx-1.17.1/
yum install -y openssl-devel # 编译ssl模块需要的依赖包
./configure --prefix=/usr/local/nginx/ --with-http_ssl_module --with-file-aio
make && make install
cd /etc/pki/tls/certs/
make cert.pem # 制作证书
cp cert.pem /usr/local/nginx/conf/ # 证书在 /usr/local/nginx/conf/才会生效
cd /usr/local/nginx/conf/
vim nginx.conf
server {
listen 443 ssl; # 监听端口为443
server_name www.westos.org;
ssl_certificate cert.pem; # 证书位置
ssl_certificate_key cert.pem; # 私钥位置
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5; # 密码加密方式
ssl_prefer_server_ciphers on;
location / {
root /web; # 根目录位置
index index.html index.htm;
}
}
server {
listen 80;
server_name www.westos.org;
location / {
root /web;
index index.html;
}
}
/usr/local/nginx/sbin/nginx -t # 检测语法
/usr/local/nginx/sbin/nginx -s reload
netstat -antlp | grep 443 # 查看443端口已开启
vim /etc/hosts # 在客户端写解析
172.25.78.11 server1 www.westos.org