技术架构
nginx启动时,会生成两种类型的进程,一个是主进程(Master),一个或多个工作进程(Worker)。主进程并不处理网络请求,主要负责调度工作进程,也就是:加载配置、启动工作进程及非停升级。所以,nginx启动以后,查看操作系统的进程列表,我们就能看到至少有两个nginx进程。
服务器实际处理网络请求及响应的是工作进程(worker),在类unix系统上,nginx可以配置多个worker,而每个worker进程都可以同时处理数以千计的网络请求。
模块化设计。nginx的worker,包括核心和功能性模块,核心模块负责维持一个运行循环(run-loop),执行网络请求处理的不同阶段的模块功能,如网络读写、存储读写、内容传输、外出过滤,以及将请求发往上游服务器等。而其代码的模块化设计,也使得我们可以根据需要对功能模块进行适当的选择和修改,编译成具有特定功能的服务器。
事件驱动、异步及非阻塞,可以说是nginx得以获得高并发、高性能的关键因素,同时也得益于对Linux、Solaris及类BSD等操作系统内核中事件通知及I/O性能增强功能的采用,如kqueue、epoll及event
ports。代理(proxy)设计,可以说是nginx深入骨髓的设计,无论是对于HTTP,还是对于FastCGI、memcache、Redis等的网络请求或响应,本质上都采用了代理机制。所以,nginx天生就是高性能的代理服务器
安装方式
yum安装
-
配置yum源文件
[root@localhost yum.repos.d]# vim /etc/yum.repos.d/nginx.repo ``` [nginx_yum] name=nginx baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
-
加载yum源
[root@localhost yum.repos.d]# yum makecache 已加载插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com base | 3.6 kB 00:00:00 extras | 2.9 kB 00:00:00 mysql-connectors-community | 2.6 kB 00:00:00 mysql-tools-community | 2.6 kB 00:00:00 mysql57-community | 2.6 kB 00:00:00 nginx_yum | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/3): nginx_yum/7/x86_64/filelists_db | 314 kB 00:00:03 (2/3): nginx_yum/7/x86_64/other_db | 143 kB 00:00:01 (3/3): nginx_yum/7/x86_64/primary_db | 235 kB 00:00:08 元数据缓存已建立
-
yum安装nginx
[root@localhost yum.repos.d]# yum -y install nginx 已加载插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com 正在解决依赖关系 --> 正在检查事务 ---> 软件包 nginx.x86_64.1.1.21.6-1.el7.ngx 将被 安装 --> 正在处理依赖关系 libpcre2-8.so.0()(64bit),它被软件包 1:nginx-1.21.6-1.el7.ngx.x86_64 需要 --> 正在检查事务 ---> 软件包 pcre2.x86_64.0.10.23-2.el7 将被 安装 --> 解决依赖关系完成 依赖关系解决 ================================================================================== Package 架构 版本 源 大小 ================================================================================== 正在安装: nginx x86_64 1:1.21.6-1.el7.ngx nginx_yum 796 k 为依赖而安装: pcre2 x86_64 10.23-2.el7 base 201 k 事务概要 ================================================================================== 安装 1 软件包 (+1 依赖软件包) 总下载量:998 k 安装大小:3.3 M Downloading packages: (1/2): pcre2-10.23-2.el7.x86_64.rpm | 201 kB 00:00:01 warning: /var/cache/yum/x86_64/7/nginx_yum/packages/nginx-1.21.6-1.el7.ngx.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY nginx-1.21.6-1.el7.ngx.x86_64.rpm 的公钥尚未安装 (2/2): nginx-1.21.6-1.el7.ngx.x86_64.rpm | 796 kB 00:00:14 ---------------------------------------------------------------------------------- 总计 68 kB/s | 998 kB 00:14 从 https://nginx.org/keys/nginx_signing.key 检索密钥 导入 GPG key 0x7BD9BF62: 用户ID : "nginx signing key <signing-key@nginx.com>" 指纹 : 573b fd6b 3d8f bc64 1079 a6ab abf5 bd82 7bd9 bf62 来自 : https://nginx.org/keys/nginx_signing.key Running transaction check Running transaction test Transaction test succeeded Running transaction 正在安装 : pcre2-10.23-2.el7.x86_64 1/2 正在安装 : 1:nginx-1.21.6-1.el7.ngx.x86_64 2/2 ---------------------------------------------------------------------- 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/ ---------------------------------------------------------------------- 验证中 : pcre2-10.23-2.el7.x86_64 1/2 验证中 : 1:nginx-1.21.6-1.el7.ngx.x86_64 2/2 已安装: nginx.x86_64 1:1.21.6-1.el7.ngx 作为依赖被安装: pcre2.x86_64 0:10.23-2.el7 完毕!
-
开启服务
[root@localhost yum.repos.d]# systemctl start nginx Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
出现错误
-
排错
输入systemctl status nginx
观察发现端口绑定失败6月 09 14:07:29 localhost.localdomain nginx[21265]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 6月 09 14:07:30 localhost.localdomain nginx[21265]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 6月 09 14:07:30 localhost.localdomain nginx[21265]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 6月 09 14:07:31 localhost.localdomain nginx[21265]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 6月 09 14:07:31 localhost.localdomain nginx[21265]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 6月 09 14:07:32 localhost.localdomain nginx[21265]: nginx: [emerg] still could not bind()
-
解决问题
查看端口被谁占用[root@localhost yum.repos.d]# netstat -nultp | grep 80 tcp6 0 0 :::80 :::* LISTEN 8069/httpd tcp6 0 0 :::443 :::* LISTEN 8069/httpd
发现被httpd服务占用
关闭httpd服务再次开启nginx服务
查看状态[root@localhost yum.repos.d]# systemctl start nginx [root@localhost yum.repos.d]# systemctl status nginx ● nginx.service - nginx - high performance web server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: active (running) since 四 2022-06-09 14:14:07 CST; 7s ago Docs: http://nginx.org/en/docs/ Process: 28915 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS) Main PID: 28916 (nginx) CGroup: /system.slice/nginx.service ├─28916 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf ├─28917 nginx: worker process ├─28918 nginx: worker process ├─28919 nginx: worker process └─28920 nginx: worker process 6月 09 14:14:07 localhost.localdomain systemd[1]: Starting nginx - high performance web server... 6月 09 14:14:07 localhost.localdomain systemd[1]: Can't open PID file /var/run/nginx.pid (yet?) after start: No such file or directory 6月 09 14:14:07 localhost.localdomain systemd[1]: Started nginx - high performance web server.
编译安装
-
安装编译环境
[root@localhost nginx]# yum -y install gcc gcc-c++ make ncurses ncurses-devel
-
安装pcre、openssl、zlib
yum install -y pcre pcre-devel openssl openssl-devel zlib zlib-devel
-
创建用户、组
useradd nginx
groupadd nginx -
下载 解压nginx安装包
[root@localhost ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
5.编译安装
```bash
[root@localhost ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@localhost ~]# tar xf nginx-1.18.0.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/nginx-1.18.0/
```
[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream
make && make install
6.启动
[root@localhost sbin]# pwd
/usr/local/nginx/sbin
[root@localhost sbin]# ./nginx
如果启动出现下列问题:
nginx: [emerg] mkdir() "/tmp/nginx/client_body" failed (2: No such file or directory)
解决方案:
[root@localhost sbin]# mkdir -pv /tmp/nginx/client_body
然后再次启动
[root@localhost sbin]# ./nginx