目 录CONTENT

文章目录

Nginx入门操作

phyger
2022-03-26 / 0 评论 / 0 点赞 / 605 阅读 / 1,765 字 / 正在检测是否收录...

前言

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

实践

安装

yum -y install nginx

HTTP

访问http://

nginx默认主页

从响应头中我们可以看到服务端的类型是: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}

无法以https访问

修改配置

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}

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

我们成功进入到了主页。

拓展:实现 URL 跳转

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

nginx 配置

添加如下配置:

location /me{
        rewrite .+ https://phygerr.github.io;
        }
``

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

配置完后,重启 nginxsystemctl restart nginx

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

跳转请求

跳转成功

感谢您的阅读,别忘了关注,点赞,评论,转发四连哟!

0

评论区