nginx介绍和几种安装方法

介绍

Nginx(engine x),2002 年开发,分为社区版和商业版(nginx plus)

nginx是免费的、开源的、高性能的 HTTP 和反向代理服务器、邮件代理服务器、以及 TCP/UDP 代理服务器。
官网地址:https://nginx.org/

功能介绍

  1. 静态的web资源服务器 html、图片、js、css、txt 等静态资源
  2. http/https协议的反向代理
  3. 结合FastCGI/uWSGI/SCGI等协议反向代理动态资源请求
  4. tcp/udp协议的请求转发(反向代理)
  5. imap4/pop4协议的反向代理

架构和进程

架构

进程

nginx是多进程组织模式,而且是一个由 Master 主进程和 Worker 工作进程组成。

主进程(master process)的功能:

  1. 对外接口:接收外部的操作(信号)
  2. 对内转发:根据外部的操作的不同,通过信号管理 Worker
  3. 监控:监控 worker 进程的运行状态,worker 进程异常终止后,自动重启 worker 进程
  4. 读取nginx配置文件并验证其有效性和正确性
  5. 建立、绑定和关闭socket连接
  6. 按照配置生成、管理和结束工作进程
  7. 接受外界指令,比如重启、升级及退出服务器等指令
  8. 不中断服务,实现平滑升级,重启服务并应用新的配置
  9. 开启日志文件,获取文件描述符
  10. 不中断服务,实现平滑升级,升级失败进行回滚处理
  11. 编译和处理perl脚本

工作进程(Worker Process)的功能:

  1. 所有 worker 进程都是平等的
  2. 实际处理:网络请求,由 worker 进程处理
  3. worker 进程数量:一般设置为核心数,充分利用 CPU 资源,同时避免进程数量过多,导致进程竞争
  4. CPU 资源,增加上下文切换的损耗
  5. 接受处理客户的请求
  6. 将请求依次送入各个功能模块进行处理
  7. I/O调用,获取响应数据
  8. 与后端服务器通信,接收后端服务的处理结果
  9. 缓存数据,访问缓存索引,查询和调用缓存数据
  10. 发送请求结果,响应客户的请求
  11. 接收主程序指令,比如重启、升级和退出等

nginx****启动

模块介绍

nginx有多种模块:

核心模块:是Nginx服务器正常运行必不可少的模块,提供错误日志记录、配置文件解析、事件驱

动机制、进程管理等核心功能

标准HTTP模块:提供HTTP协议解析相关的功能,比如:端口配置、网页编码设置、HTTP响应头设置等

可行HTTP模块:主要用于扩展标准的HTTP功能,让Nginx能处理一些特殊的服务,比如:Flash多媒体传输、解析 GeoIP 请求、网络传输压缩、安全协议 SSL 支持等

邮件服务模块:主要用于支持Nginx的邮件服务,包括对 POP3 协议、IMAP 协议和 SMTP 协议的支持

Stream服务模块:实现反向代理功能,包括 TCP 协议代理

第三方模块:是为了扩展 Nginx 服务器应用,完成开发者自定义功能,比如:json 支持、Lua 支持等

Nginx 高度模块化,但其模块早期不支持 DSO 机制;1.9.11 版本支持动态装载和卸载。

安装

本地安装

1、配置本地仓库

bash 复制代码
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# vim bendi.repo
[root@localhost yum.repos.d]# cat bendi.repo
[BaseOS]
name=base
baseurl=/mnt/BaseOS
gpgcheck=0
[APPStream]
name=appstream
baseurl=/mnt/AppStream
gpgcheck=0

2、安装 nginx

bash 复制代码
[root@localhost yum.repos.d]# cd 
[root@localhost ~]# dnf -y install nginx

3、删除本地安装nginx

bash 复制代码
[root@localhost ~]# dnf -y remove nginx
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

Repository os is listed more than once in the configuration
Repository app is listed more than once in the configuration
Dependencies resolved.
=============================================================================
 Package    Arch        Version                   Repository            Size
=============================================================================
Removing:
 nginx      x86_64      2:1.27.4-1.el9.ngx        @nginx-mainline      3.4 M

Transaction Summary
=============================================================================
Remove  1 Package

Freed space: 3.4 M
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                     1/1 
  Running scriptlet: nginx-2:1.27.4-1.el9.ngx.x86_64                     1/1 
  Erasing          : nginx-2:1.27.4-1.el9.ngx.x86_64                     1/1 
  Running scriptlet: nginx-2:1.27.4-1.el9.ngx.x86_64                     1/1 
  Verifying        : nginx-2:1.27.4-1.el9.ngx.x86_64                     1/1 
Installed products updated.

Removed:
  nginx-2:1.27.4-1.el9.ngx.x86_64                                            

Complete!

官方安装

1、进入官网首页:www.nginx.org

2、单击 install 链接进入以下界面

3、单击 packages 链接

4、单击 RHEL and derivatives 链接

5、按照该页面提示配置仓库

bash 复制代码
[root@localhost ~]# vim /etc/yum.repos.d/nginx.repo 
[root@localhost ~]# cat /etc/yum.repos.d/nginx.repo 
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

6、安装nginx

bash 复制代码
[root@localhost ~]# dnf -y install nginx
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

Repository os is listed more than once in the configuration
Repository app is listed more than once in the configuration
nginx stable repo                            6.7 kB/s | 3.0 kB     00:00    
Dependencies resolved.
=============================================================================
 Package     Architecture Version                   Repository          Size
=============================================================================
Installing:
 nginx       x86_64       2:1.26.3-1.el9.ngx        nginx-stable       997 k

Transaction Summary
=============================================================================
Install  1 Package

Total download size: 997 k
Installed size: 3.3 M
Downloading Packages:
nginx-1.26.3-1.el9.ngx.x86_64.rpm            120 kB/s | 997 kB     00:08    
-----------------------------------------------------------------------------
Total                                        120 kB/s | 997 kB     00:08     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                     1/1 
  Running scriptlet: nginx-2:1.26.3-1.el9.ngx.x86_64                     1/1 
  Installing       : nginx-2:1.26.3-1.el9.ngx.x86_64                     1/1 
  Running scriptlet: nginx-2:1.26.3-1.el9.ngx.x86_64                     1/1 
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* https://nginx.org/en/docs/

Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* https://nginx.org/en/support.html

Commercial subscriptions for nginx are available on:
* https://nginx.com/products/

----------------------------------------------------------------------

  Verifying        : nginx-2:1.26.3-1.el9.ngx.x86_64                     1/1 
Installed products updated.

Installed:
  nginx-2:1.26.3-1.el9.ngx.x86_64                                            

Complete!

a.注意:默认安装的是 stable 版本,接下来是安装 mainline 版本,先删除之前安装的 stable 版本

bash 复制代码
[root@localhost ~]# dnf -y remove install
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

Repository os is listed more than once in the configuration
Repository app is listed more than once in the configuration
No match for argument: install
No packages marked for removal.
Dependencies resolved.
Nothing to do.
Complete!

b.启用 mainline 版本

bash 复制代码
[root@localhost ~]# dnf config-manager --enable nginx-mainline
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

Repository os is listed more than once in the configuration
Repository app is listed more than once in the configuration

c.安装 mainline 版本

bash 复制代码
[root@localhost ~]# dnf install nginx 
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

Repository os is listed more than once in the configuration
Repository app is listed more than once in the configuration
nginx stable repo                            6.4 kB/s | 3.0 kB     00:00    
nginx mainline repo                          4.6 kB/s | 3.0 kB     00:00    
Package nginx-2:1.26.3-1.el9.ngx.x86_64 is already installed.
Dependencies resolved.
=============================================================================
 Package    Arch        Version                    Repository           Size
=============================================================================
Upgrading:
 nginx      x86_64      2:1.27.4-1.el9.ngx         nginx-mainline      1.0 M

Transaction Summary
=============================================================================
Upgrade  1 Package

Total download size: 1.0 M
Is this ok [y/N]: y
Downloading Packages:
nginx-1.27.4-1.el9.ngx.x86_64.rpm            304 kB/s | 1.0 MB     00:03    
-----------------------------------------------------------------------------
Total                                        304 kB/s | 1.0 MB     00:03     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                     1/1 
  Running scriptlet: nginx-2:1.27.4-1.el9.ngx.x86_64                     1/2 
  Upgrading        : nginx-2:1.27.4-1.el9.ngx.x86_64                     1/2 
  Running scriptlet: nginx-2:1.27.4-1.el9.ngx.x86_64                     1/2 
  Running scriptlet: nginx-2:1.26.3-1.el9.ngx.x86_64                     2/2 
  Cleanup          : nginx-2:1.26.3-1.el9.ngx.x86_64                     2/2 
  Running scriptlet: nginx-2:1.26.3-1.el9.ngx.x86_64                     2/2 
  Verifying        : nginx-2:1.27.4-1.el9.ngx.x86_64                     1/2 
  Verifying        : nginx-2:1.26.3-1.el9.ngx.x86_64                     2/2 
Installed products updated.

Upgraded:
  nginx-2:1.27.4-1.el9.ngx.x86_64                                            

Complete!

d.安装完成之后查看

bash 复制代码
[root@localhost ~]# rpm -qa | grep nginx
nginx-1.27.4-1.el9.ngx.x86_64

源码安装

bash 复制代码
[root@localhost ~]# git clone https://github.com/nginx/nginx.git
Cloning into 'nginx'...
remote: Enumerating objects: 70209, done.
remote: Counting objects: 100% (135/135), done.
remote: Compressing objects: 100% (86/86), done.


[root@localhost ~]# cd nginx/
[root@localhost nginx]# ll
total 40
drwxr-xr-x. 6 root root 4096 Oct 9 17:23 auto
-rw-r--r--. 1 root root 5215 Oct 9 17:23 CODE_OF_CONDUCT.md
drwxr-xr-x. 2 root root 168 Oct 9 17:23 conf
drwxr-xr-x. 4 root root 72 Oct 9 17:23 contrib
-rw-r--r--. 1 root root 4049 Oct 9 17:23 CONTRIBUTING.md
drwxr-xr-x. 8 root root 94 Oct 9 17:23 docs
-rw-r--r--. 1 root root 1319 Oct 9 17:23 LICENSE
drwxr-xr-x. 2 root root 39 Oct 9 17:23 misc
-rw-r--r--. 1 root root 14220 Oct 9 17:23 README.md
-rw-r--r--. 1 root root 787 Oct 9 17:23 SECURITY.md
drwxr-xr-x. 9 root root 91 Oct 9 17:23 src

#如果出现以下提示:
auto/configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE
library
statically from the source with nginx by using --with-pcre=<path> option.

#安装:
[root@localhost nginx]# dnf install pcre-devel
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered with an entitlement server. You can use
subscription-manager to register.
Last metadata expiration check: 0:26:49 ago on Wed 09 Oct 2024 05:11:09 PM
CST.
Dependencies resolved.
===========================================================================
=============================
Package Architecture Version
Repository Size
===========================================================================
=============================
Installing:
pcre-devel x86_64 8.44-3.el9.3
baseos2 511 k
Installing dependencies:
pcre-cpp x86_64 8.44-3.el9.3
baseos2 28 kpcre-utf16 x86_64 8.44-3.el9.3
baseos2 188 kpcre-utf32 x86_64 8.44-3.el9.3
baseos2 178 kTransaction Summary
===========================================================================
=============================
Install 4 Packages
Total size: 905 k
Installed size: 2.6 M
Is this ok [y/N]: y
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing :
1/1
Installing : pcre-utf32-8.44-3.el9.3.x86_64
1/4
Installing : pcre-utf16-8.44-3.el9.3.x86_64
2/4
Installing : pcre-cpp-8.44-3.el9.3.x86_64
3/4
Installing : pcre-devel-8.44-3.el9.3.x86_64
4/4
Running scriptlet: pcre-devel-8.44-3.el9.3.x86_64
4/4
Verifying : pcre-cpp-8.44-3.el9.3.x86_64
1/4
Verifying : pcre-devel-8.44-3.el9.3.x86_64
2/4
Verifying : pcre-utf16-8.44-3.el9.3.x86_64
3/4
Verifying : pcre-utf32-8.44-3.el9.3.x86_64
4/4
Installed products updated.
Installed:
pcre-cpp-8.44-3.el9.3.x86_64 pcre-devel-8.44-3.el9.3.x86_64 pcreutf16-8.44-3.el9.3.x86_64
pcre-utf32-8.44-3.el9.3.x86_64
Complete!


#如果出现以下提示:
auto/configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib
library
statically from the source with nginx by using --with-zlib=<path> option.

#安装:
[root@localhost nginx]# dnf install zlib-devel
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered with an entitlement server. You can use
subscription-manager to register.
Last metadata expiration check: 0:29:09 ago on Wed 09 Oct 2024 05:11:09 PM
CST.
Dependencies resolved.
===========================================================================
=============================
Package Architecture Version
Repository Size
===========================================================================
=============================
Installing:
zlib-devel x86_64 1.2.11-40.el9
baseos2 47 k
Transaction Summary
===========================================================================
=============================
Install 1 Package
Total size: 47 k
Installed size: 138 k
Is this ok [y/N]: y
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing :
1/1
Installing : zlib-devel-1.2.11-40.el9.x86_64
1/1
Running scriptlet: zlib-devel-1.2.11-40.el9.x86_64
1/1
Verifying : zlib-devel-1.2.11-40.el9.x86_64
1/1
Installed products updated.
Installed:
zlib-devel-1.2.11-40.el9.x86_64
Complete!


#出现以下提示,表示 configure 成功
creating objs/Makefile
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

#执行 make 指令

[root@localhost nginx]# make

#出现以下提示,表示 make 完成
objs/src/http/modules/ngx_http_upstream_zone_module.o \
objs/ngx_modules.o \
-lcrypt -lpcre -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
-e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
-e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
-e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
< docs/man/nginx.8 > objs/nginx.8
make[1]: Leaving directory '/root/nginx'

开始安装:

bash 复制代码
[root@localhost nginx]# make install

出现以下提示,说明安装成功
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
|| cp -R docs/html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory '/root/nginx'

#启动服务
[root@localhost nginx]# /usr/local/nginx/sbin/nginx

#查看服务
[root@localhost nginx]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
PID/Program name
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
1196/cupsd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
1207/sshd: /usr/sbi
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
42424/nginx: master
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
1/systemd
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN
2241/sshd: root@pts
tcp6 0 0 ::1:6010 :::* LISTEN
2241/sshd: root@pts
tcp6 0 0 :::22 :::* LISTEN
1207/sshd: /usr/sbi
tcp6 0 0 ::1:631 :::* LISTEN
1196/cupsd
tcp6 0 0 :::111 :::* LISTEN
1/systemd
udp 0 0 0.0.0.0:5353 0.0.0.0:*
986/avahi-daemon: r
udp 0 0 0.0.0.0:111 0.0.0.0:*
1/systemd
udp 0 0 127.0.0.1:323 0.0.0.0:*
1011/chronyd
udp 0 0 0.0.0.0:41410 0.0.0.0:*
986/avahi-daemon: r
udp6 0 0 :::5353 :::*
986/avahi-daemon: r
udp6 0 0 :::111 :::*
1/systemd
udp6 0 0 ::1:323 :::*
1011/chronyd
udp6 0 0 :::33739 :::*
986/avahi-daemon: r

[root@localhost nginx]# ps -ef | grep nginx
root 42401 1 0 17:47 ? 00:00:00 gpg-agent --homedir
/var/cache/PackageKit/9.3/metadata/nginx-stable-9-x86_64.tmp/gpgdir --usestandard-socket --daemon
root 42403 42401 0 17:47 ? 00:00:00 scdaemon --multi-server
--homedir /var/cache/PackageKit/9.3/metadata/nginx-stable-9-
x86_64.tmp/gpgdir
root 42415 1 0 17:47 ? 00:00:00 gpg-agent --homedir
/var/cache/PackageKit/9.3/metadata/nginx-mainline-9-x86_64.tmp/gpgdir --
use-standard-socket --daemon
root 42417 42415 0 17:47 ? 00:00:00 scdaemon --multi-server
--homedir /var/cache/PackageKit/9.3/metadata/nginx-mainline-9-
x86_64.tmp/gpgdir
root 42424 1 0 17:47 ? 00:00:00 nginx: master process
/usr/local/nginx/sbin/nginx
nobody 42425 42424 0 17:47 ? 00:00:00 nginx: worker process
root 42429 2252 0 17:47 pts/0 00:00:00 grep --color=auto nginx

注意:源码安装适合对 Nginx 功能有定制需求的用户。通过源码编译,可以选择特定的版本和模块进行安装,但过程相对复杂,需要手动处理依赖关系。如果选择源码编译安装,需要确保系统中安装了必要的编译工具和依赖库,如 gccmakeopensslpcrezlib等。

相关推荐
还是鼠鼠18 分钟前
Node.js 跨域 CORS 简单请求与预检请求的介绍
运维·服务器·vscode·中间件·node.js·express
old_iron4 小时前
vim定位有问题的脚本/插件的一般方法
linux·编辑器·vim
爱知菜6 小时前
Windows安装Docker Desktop(WSL2模式)和Docker Pull网络问题解决
运维·docker·容器
做测试的小薄6 小时前
Nginx 命令大全:Linux 与 Windows 系统的全面解析
linux·自动化测试·windows·nginx·环境部署
影龙帝皖7 小时前
Linux网络之局域网yum仓库与apt的实现
linux·服务器·网络
月下雨(Moonlit Rain)7 小时前
Docker
运维·docker·容器
A尘埃8 小时前
用户注册(阿里云手机验证码)
阿里云·智能手机·云计算·短信验证码
碎忆8 小时前
在VMware中安装虚拟机Ubuntu
linux·ubuntu
农民小飞侠8 小时前
ubuntu 安装pyllama教程
linux·python·ubuntu
技术小甜甜8 小时前
[Dify] 使用 Docker 本地部署 Dify 并集成 Ollama 模型的详细指南
运维·docker·容器·dify