继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

关于Nginx,你需要知道的!

慕田峪4524236
关注TA
已关注
手记 204
粉丝 19
获赞 50

前言

NginxApache HTTP Server 都是业内流行的 web 服务器软件,但是相比 Apache HTTP ServerNginx 更加轻量和高性能,所以在了解完 Apache HTTP Server 后,今天我们一起来进入 Nginx 的世界。

实践

安装

yum -y install nginx

HTTP

访问http://{server_ip}

http://img4.mukewang.com/6123c5de000158df17810947.jpg

从响应头中我们可以看到服务端的类型是:nginx/1.20.1

修改默认主页

nginx 的默认站点路径在:/usr/share/nginx/html

我们修改此路径下的 index.html 的内容为:

<!DOCTYPE html>

<html>

<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>


再次访问查看效果:

http://img2.mukewang.com/6123c60b0001684204290773.jpg

HTTPS

默认配置下的 nginx 是不支持 https 协议的。默认的 nginx 配置路径为:/etc/nginx/nginx.conf

尝试访问:https://{server_ip}

http://img1.mukewang.com/6123c60b00016d1001840147.jpg

修改配置

vim /etc/nginx/nginx.conf

默认如下配置是被注释掉的,我们放开注释即可。

http://img1.mukewang.com/6123c60b0001e8b905860411.jpg

根据上图中的配置,我们拷贝自签证书到指定路径下。生成证书的方法,请看上篇文章,里面有介绍。

# 创建证书目录

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}

http://img1.mukewang.com/6123c6290001cab412810554.jpg

我们看到,nginxhttps 已经搞定。点击高级,继续前往:

http://img1.mukewang.com/6123c6290001267203280225.jpg

我们成功进入到了主页。

拓展:实现 URL 跳转

目标:当访问https://{server_ip}/me时,自动跳转到:https://phygerr.github.io

nginx 配置

添加如下配置:

http://img1.mukewang.com/6123c6290001273d05270288.jpg

location /me{

        rewrite .+ https://phygerr.github.io;

        }

``


> http跳转在http的server部分配置,https的跳转就在https的server部分配置。

配置完后,重启 nginxsystemctl restart nginx

访问https://{server_ip}/me查看效果:

http://img2.mukewang.com/6123c64000018edc01560053.jpg

http://img.mukewang.com/6123c640000105fc03060091.jpg


作者:Python测试和开发
链接:https://juejin.cn/post/6999507189710716964
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP