美文网首页
狂神说-nginx笔记讲解

狂神说-nginx笔记讲解

作者: Leo_23 | 来源:发表于2021-05-10 22:23 被阅读0次

参考原文

4、配置监听

nginx的配置文件是conf目录下的nginx.conf,默认配置的nginx监听的端口为80,如果80端口被占用可以修改为未被占用的端口即可。

图片

当我们修改了nginx的配置文件nginx.conf 时,不需要关闭nginx后重新启动nginx,只需要执行命令 nginx -s reload 即可让改动生效

5、关闭nginx

如果使用cmd命令窗口启动nginx, 关闭cmd窗口是不能结束nginx进程的,可使用两种方法关闭nginx

(1)输入nginx命令 nginx -s stop(快速停止nginx) 或 nginx -s quit(完整有序的停止nginx)

(2)使用taskkill taskkill /f /t /im nginx.exe

<pre data-darkmode-bgcolor-16206563839693="rgb(34, 34, 34)" data-darkmode-original-bgcolor-16206563839693="#fff|rgb(246, 246, 246)" data-darkmode-color-16206563839693="rgb(186, 193, 199)" data-darkmode-original-color-16206563839693="#fff|rgb(33, 37, 41)" data-style="box-sizing: border-box; padding: 10px; margin-bottom: 16px; list-style: none; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; overflow: auto; color: rgb(33, 37, 41); -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow-wrap: break-word; background: rgb(246, 246, 246); border-radius: 3px; border-width: 1px; border-style: solid; border-color: rgb(221, 221, 221); text-align: left;" class="js_darkmode__25" style="margin: 0px 0px 16px; padding: 10px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word; border-color: rgb(221, 221, 221); background: rgb(246, 246, 246); color: rgb(33, 37, 41); font: 400 12px / 1.6 "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; list-style: none; overflow: auto; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; border-radius: 3px; border-width: 1px; border-style: solid; text-align: left;">

  1. taskkill是用来终止进程的,

  2. /f是强制终止 .

  3. /t终止指定的进程和任何由此启动的子进程。

  4. /im示指定的进程名称 .

</pre>

linux下安装

1、安装gcc

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

<pre data-darkmode-bgcolor-16206563839693="rgb(34, 34, 34)" data-darkmode-original-bgcolor-16206563839693="#fff|rgb(246, 246, 246)" data-darkmode-color-16206563839693="rgb(186, 193, 199)" data-darkmode-original-color-16206563839693="#fff|rgb(33, 37, 41)" data-style="box-sizing: border-box; padding: 10px; margin-bottom: 16px; list-style: none; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; overflow: auto; color: rgb(33, 37, 41); -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow-wrap: break-word; background: rgb(246, 246, 246); border-radius: 3px; border-width: 1px; border-style: solid; border-color: rgb(221, 221, 221); text-align: left;" class="js_darkmode__44" style="margin: 0px 0px 16px; padding: 10px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word; border-color: rgb(221, 221, 221); background: rgb(246, 246, 246); color: rgb(33, 37, 41); font: 400 12px / 1.6 "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; list-style: none; overflow: auto; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; border-radius: 3px; border-width: 1px; border-style: solid; text-align: left;">

  1. yum install gcc-c++

</pre>

2、PCRE pcre-devel 安装

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

<pre data-darkmode-bgcolor-16206563839693="rgb(34, 34, 34)" data-darkmode-original-bgcolor-16206563839693="#fff|rgb(246, 246, 246)" data-darkmode-color-16206563839693="rgb(186, 193, 199)" data-darkmode-original-color-16206563839693="#fff|rgb(33, 37, 41)" data-style="box-sizing: border-box; padding: 10px; margin-bottom: 16px; list-style: none; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; overflow: auto; color: rgb(33, 37, 41); -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow-wrap: break-word; background: rgb(246, 246, 246); border-radius: 3px; border-width: 1px; border-style: solid; border-color: rgb(221, 221, 221); text-align: left;" class="js_darkmode__49" style="margin: 0px 0px 16px; padding: 10px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word; border-color: rgb(221, 221, 221); background: rgb(246, 246, 246); color: rgb(33, 37, 41); font: 400 12px / 1.6 "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; list-style: none; overflow: auto; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; border-radius: 3px; border-width: 1px; border-style: solid; text-align: left;">

  1. yum install -y pcre pcre-devel

</pre>

3、zlib 安装

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

<pre data-darkmode-bgcolor-16206563839693="rgb(34, 34, 34)" data-darkmode-original-bgcolor-16206563839693="#fff|rgb(246, 246, 246)" data-darkmode-color-16206563839693="rgb(186, 193, 199)" data-darkmode-original-color-16206563839693="#fff|rgb(33, 37, 41)" data-style="box-sizing: border-box; padding: 10px; margin-bottom: 16px; list-style: none; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; overflow: auto; color: rgb(33, 37, 41); -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow-wrap: break-word; background: rgb(246, 246, 246); border-radius: 3px; border-width: 1px; border-style: solid; border-color: rgb(221, 221, 221); text-align: left;" class="js_darkmode__55" style="margin: 0px 0px 16px; padding: 10px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word; border-color: rgb(221, 221, 221); background: rgb(246, 246, 246); color: rgb(33, 37, 41); font: 400 12px / 1.6 "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; list-style: none; overflow: auto; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; border-radius: 3px; border-width: 1px; border-style: solid; text-align: left;">

  1. yum install -y zlib zlib-devel

</pre>

4、OpenSSL 安装
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

<pre data-darkmode-bgcolor-16206563839693="rgb(34, 34, 34)" data-darkmode-original-bgcolor-16206563839693="#fff|rgb(246, 246, 246)" data-darkmode-color-16206563839693="rgb(186, 193, 199)" data-darkmode-original-color-16206563839693="#fff|rgb(33, 37, 41)" data-style="box-sizing: border-box; padding: 10px; margin-bottom: 16px; list-style: none; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; overflow: auto; color: rgb(33, 37, 41); -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow-wrap: break-word; background: rgb(246, 246, 246); border-radius: 3px; border-width: 1px; border-style: solid; border-color: rgb(221, 221, 221); text-align: left;" class="js_darkmode__61" style="margin: 0px 0px 16px; padding: 10px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word; border-color: rgb(221, 221, 221); background: rgb(246, 246, 246); color: rgb(33, 37, 41); font: 400 12px / 1.6 "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; list-style: none; overflow: auto; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; border-radius: 3px; border-width: 1px; border-style: solid; text-align: left;">

  1. yum install -y openssl openssl-devel

</pre>

5、下载安装包

手动下载.tar.gz安装包,地址:https://nginx.org/en/download.html

图片

下载完毕上传到服务器上 /root

6、解压

<pre data-darkmode-bgcolor-16206563839693="rgb(34, 34, 34)" data-darkmode-original-bgcolor-16206563839693="#fff|rgb(246, 246, 246)" data-darkmode-color-16206563839693="rgb(186, 193, 199)" data-darkmode-original-color-16206563839693="#fff|rgb(33, 37, 41)" data-style="box-sizing: border-box; padding: 10px; margin-bottom: 16px; list-style: none; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; overflow: auto; color: rgb(33, 37, 41); -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow-wrap: break-word; background: rgb(246, 246, 246); border-radius: 3px; border-width: 1px; border-style: solid; border-color: rgb(221, 221, 221); text-align: left;" class="js_darkmode__68" style="margin: 0px 0px 16px; padding: 10px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word; border-color: rgb(221, 221, 221); background: rgb(246, 246, 246); color: rgb(33, 37, 41); font: 400 12px / 1.6 "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; list-style: none; overflow: auto; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; border-radius: 3px; border-width: 1px; border-style: solid; text-align: left;">

  1. tar -zxvf nginx-1.18.0.tar.gz

  2. cd nginx-1.18.0

</pre>

图片

7、配置

使用默认配置,在nginx根目录下执行

<pre data-darkmode-bgcolor-16206563839693="rgb(34, 34, 34)" data-darkmode-original-bgcolor-16206563839693="#fff|rgb(246, 246, 246)" data-darkmode-color-16206563839693="rgb(186, 193, 199)" data-darkmode-original-color-16206563839693="#fff|rgb(33, 37, 41)" data-style="box-sizing: border-box; padding: 10px; margin-bottom: 16px; list-style: none; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; overflow: auto; color: rgb(33, 37, 41); -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow-wrap: break-word; background: rgb(246, 246, 246); border-radius: 3px; border-width: 1px; border-style: solid; border-color: rgb(221, 221, 221); text-align: left;" class="js_darkmode__85" style="margin: 0px 0px 16px; padding: 10px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word; border-color: rgb(221, 221, 221); background: rgb(246, 246, 246); color: rgb(33, 37, 41); font: 400 12px / 1.6 "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; list-style: none; overflow: auto; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; border-radius: 3px; border-width: 1px; border-style: solid; text-align: left;">

  1. ./configure

  2. make

  3. make install

</pre>

查找安装路径: whereis nginx

图片

Nginx常用命令

cd /usr/local/nginx/sbin/

./nginx  启动

./nginx -s stop  停止

./nginx -s quit  安全退出

./nginx -s reload  重新加载配置文件

ps aux|grep nginx  查看nginx进程

注意:如何连接不上,检查阿里云安全组是否开放端口,或者服务器防火墙是否开放端口!
相关命令:

# 开启

service firewalld start

# 重启

service firewalld restart

# 关闭

service firewalld stop

# 查看防火墙规则

firewall-cmd --list-all

# 查询端口是否开放

firewall-cmd --query-port=8080/tcp

# 开放80端口

firewall-cmd --permanent --add-port=80/tcp

# 移除端口

firewall-cmd --permanent --remove-port=8080/tcp



#重启防火墙(修改配置后要重启防火墙)

firewall-cmd --reload



# 参数解释

1、firwall-cmd:是Linux提供的操作firewall的一个工具;

2、--permanent:表示设置为持久;

3、--add-port:标识添加的端口;

相关文章

  • 狂神说-nginx笔记讲解

    参考原文[https://mp.weixin.qq.com/s/pVopNiFSTHJWeSLCpfq_vw] 4...

  • 注解和反射

    来源于狂神说笔记

  • 狂神说笔记

    https://blog.csdn.net/qq_42025798/article/details/1191920...

  • 狂神Nginx

    反向代理负载均衡动静分离 nginx是一个高性能的http和反向代理web服务器 Linux下安装nginx./n...

  • Spring笔记

    感谢狂神,讲解清晰,以下是原视频【狂神说Java】Spring5最新完整教程IDEA版通俗易懂[https://w...

  • Mybatis笔记

    感谢狂神,讲解清晰,以下是原视频【狂神说Java】Mybatis最新完整教程IDEA版通俗易懂_哔哩哔哩_bili...

  • 2.Docker安装与加速器配置

    笔记整理来源 B站UP主狂神说Javahttps://space.bilibili.com/95256449/ <...

  • MySQL知识_索引

    目录索引: ლ(′◉❥◉`ლ) 点击此链接 MySQL_博客园链接Mysql基础原理知识点总结狂神说 讲解mysql

  • Spring boot-JDBC

    B站 “遇见狂神说” 笔记点击跳转视频 jdbc 链接数据库的时候,添加时区serverTime=UTC。 使用:

  • 狂神说mybaits

    mybatis的核心目标,获得sqlsession sqlsessionfactorybuilder sql...

网友评论

      本文标题:狂神说-nginx笔记讲解

      本文链接:https://www.haomeiwen.com/subject/peswdltx.html