前言
Nginx
和 Apache HTTP Server
都是业内流行的 web
服务器软件,但是相比 Apache HTTP Server
,Nginx
更加轻量和高性能,所以在了解完 Apache HTTP Server
后,今天我们一起来进入 Nginx
的世界。
实践
安装
yum -y install nginx 复制代码
HTTP
访问http://{server_ip}
网络异常,图片无法展示
|
从响应头中我们可以看到服务端的类型是:nginx/1.20.1
修改默认主页
nginx 的默认站点路径在:/usr/share/nginx/html
我们修改此路径下的 index.html
的内容为:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script> <body> <div id="appv"> 请输入内容:<br><br> <textarea rows="" cols="" v-model="info"></textarea> <!-- <input v-model="info"> --> <p style="white-space: pre-line;">你输入的内容是:<br><br>{{ info }}</p> </div> <script> app = new Vue({ el: "#appv", data: { info: "placeholder", } }) </script> </body> </html> 复制代码
再次访问查看效果:
网络异常,图片无法展示
|
HTTPS
默认配置下的 nginx
是不支持 https
协议的。默认的 nginx
配置路径为:/etc/nginx/nginx.conf
尝试访问:https://{server_ip}
网络异常,图片无法展示
|
修改配置
vim /etc/nginx/nginx.conf
默认如下配置是被注释掉的,我们放开注释即可。
网络异常,图片无法展示
|
根据上图中的配置,我们拷贝自签证书到指定路径下。生成证书的方法,请看上篇文章,里面有介绍。
# 创建证书目录 mkdir /etc/pki/nginx/ mkdir /etc/pki/nginx/private/ # 拷贝证书到指定目录下 cp server.crt /etc/pki/nginx/ cp server.key /etc/pki/nginx/private/ 复制代码
HTTPS 方式访问
再次尝试访问:https://{server_ip}
网络异常,图片无法展示
|
我们看到,nginx
的 https
已经搞定。点击高级,继续前往:
网络异常,图片无法展示
|
我们成功进入到了主页。
拓展:实现 URL 跳转
目标:当访问https://{server_ip}/me
时,自动跳转到:https://phygerr.github.io
。
nginx 配置
添加如下配置:
网络异常,图片无法展示
|
location /me{ rewrite .+ https://phygerr.github.io; } `` > http跳转在http的server部分配置,https的跳转就在https的server部分配置。 复制代码
配置完后,重启 nginx
:systemctl restart nginx
。
访问https://{server_ip}/me
查看效果:
网络异常,图片无法展示
|
网络异常,图片无法展示
|
感谢您的阅读,别忘了关注,点赞,评论,转发四连哟!