nginx简介及功能介绍

目录

niginx与apache

niginx特点

nginx模块介绍

nginx的编译安装

nginx的平滑升级及版本回滚

niginx的常用参数

nginx独立文件编写

location匹配用法

自定义日志

文件检测

nginx中的长链接管理

nginx下载服务器设置

nginx的状态页面

nginx的数据压缩功能

nginx的变量详解

nginx源码编译php

高速缓存


niginx与apache

Nginx(发音为"engine X")是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。Nginx以其稳定性、丰富的功能集、简单的配置和低资源消耗而闻名。它最初由俄罗斯人Igor Sysoev编写,并于2004年首次公开发布。Nginx的设计初衷是为了解决C10k问题,即单台服务器如何高效地处理成千上万的并发连接。

Apache‌是一个模型化的服务器,可以运行在几乎所有的服务器上,以其模块多、性能稳定而著称。Apache支持多种模块和扩展,包括对动态页面的支持,如PHP等,这使得它在处理动态请求时比较擅长。Apache采用同步多进程模型,每个连接对应一个进程,这种模型在处理复杂的应用逻辑时表现良好。然而,与Nginx相比,Apache在处理静态文件时的性能可能较低,尤其是在高并发环境下。尽管如此,Apache在模块化和.htaccess文件支持方面更有优势,提供了更多的配置选项和灵活性,适合于需要复杂配置和较多自定义功能的网站‌。

niginx特点

高性能:Nginx采用异步非阻塞的事件驱动模型,这使其能够处理高并发连接,而不会像传统的基于线程的服务器那样遇到线程上下文切换的开销。

稳定性:Nginx被设计为高度模块化,每个模块都执行特定的任务,并且Nginx的源代码非常清晰,易于理解和维护,这有助于减少错误和漏洞。

丰富的功能:Nginx支持HTTP、HTTPS、SMTP、POP3和IMAP协议,并且可以作为反向代理、负载均衡器、HTTP缓存和Web服务器使用。它还支持FastCGI、uWSGI、SCGI和WSGI等协议,可以轻松地与各种后端技术栈集成。

配置简单:Nginx的配置文件结构清晰,易于理解和修改。它使用简单的指令和参数来定义服务器的行为,使得配置过程变得简单快捷。

低资源消耗:Nginx在处理大量并发连接时,能够保持较低的内存和CPU使用率,这使得它成为构建高性能Web应用和服务器的理想选择。

模块化设计:Nginx的模块化设计允许用户根据需要添加或删除功能,而无需重新编译整个服务器。这种灵活性使得Nginx能够适应各种应用场景。

nginx模块介绍

Web服务器:Nginx可以作为静态和动态内容的Web服务器,支持HTTP/2、HTTP/3(QUIC)等现代协议。

反向代理:Nginx可以将客户端的请求转发到后端服务器,并根据需要处理负载均衡、缓存和SSL加密等任务。

负载均衡器:Nginx可以根据配置的规则将请求分发到多个后端服务器,以实现负载均衡和故障转移。

HTTP缓存:Nginx可以缓存静态内容,以减少对后端服务器的请求,提高响应速度和降低带宽消耗。

流媒体服务器:Nginx支持HTTP流媒体服务,可以传输音频、视频等多媒体内容。

核心模块:是 Nginx 服务器正常运行必不可少的模块,提供错误日志记录 、配置文件解析 、事件 驱动机制 、进程管理等核心功能

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

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

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

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

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

nginx的编译安装

官方源码包下载地址: https://nginx.org/en/download.html

root@nginx \~# wget -c https://nginx.org/download/nginx-1.24.0.tar.gz

root@nginx \~# tar zxf nginx-1.24.0.tar.gz ------解压

创建nginx用户

root@nginx nginx-1.24.0# useradd -s /sbin/nologin -M nginx

dnf install gcc pcre-devel zlib-devel openssl-devel -y ------安装依赖环境

root@nginx nginx-1.24.0# ./configure --prefix=/usr/local/nginx \

> --user=nginx \

> --group=nginx \

> --with-http_ssl_module \

> --with-http_v2_module \

> --with-http_realip_module \

> --with-http_stub_status_module \

> --with-http_gzip_static_module \

> --with-pcre \

> --with-stream \

> --with-stream_ssl_module \

> --with-stream_realip_module

root@nginx nginx-1.24.0# make && make install

root@nginx nginx-1.24.0# vim ~/.bash_profile

export PATH=$PATH:/usr/local/nginx/sbin

root@nginx nginx-1.24.0# source ~/.bash_profile

root@nginx nginx-1.24.0# nginx -V

nginx version: nginx/1.24.0

built by gcc 11.3.1 20220421 (Red Hat 11.3.1-2) (GCC)

built with OpenSSL 3.0.1 14 Dec 2021

TLS SNI support enabled

configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-ht tp_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_stat us_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ss l_module --with-stream_realip_module

开启nignx文件

root@nginx nginx-1.24.0# vim /lib/systemd/system/nginx.service

Unit

Description=The NGINX HTTP and reverse proxy server

After=syslog.target network-online.target remote-fs.target nss-lookup.target Wants=network-online.target

Service

Type=forking

PIDFile=/usr/local/nginx/logs/nginx.pid

ExecStartPre=/usr/local/nginx/sbin/nginx -t

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/usr/local/nginx/sbin/nginx -s reload

ExecStop=/bin/kill -s QUIT $MAINPID

PrivateTmp=true

Install WantedBy=multi-user.target

root@nginx nginx-1.24.0# systemctl start nginx---------=启动nginx服务

root@nginx nginx-1.24.0# ps aux | grep nginx------查看进程

root 56017 0.0 0.0 9836 928 ? Ss 11:55 0:00 nginx: master process /usr/local/nginx/sbin/nginx

nginx 56018 0.0 0.1 13724 4804 ? S 11:55 0:00 nginx: worker process

root 57776 0.0 0.0 221664 2232 pts/1 S+ 11:59 0:00 grep --color=auto nginx

nginx的平滑升级及版本回滚

解压新版本

root@nginx# tar zxf nginx-1.26.1.tar.gz

root@Nginx nginx# cd nginx-1.26.1/

开始编译新版本

root@Nginx nginx-1.26.1# ./configure --with-http_ssl_module --withhttp_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module -- with-stream_realip_module

root@nginx-1.26.1# make
把之前的旧版的nginx命令备份

root@nginx nginx-1.26.1# cd /usr/local/nginx/sbin/

root@nginx sbin# cp nginx nginx.24

root@nginx sbin# ls

nginx nginx.24

把新版本的nginx命令复制过去

root@nginx sbin# \cp -f /root/nginx-1.26.1/objs/nginx /usr/local/nginx/sbin

检测

root@nginx sbin# nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

查看进程

root@nginx sbin# ps aux | grep nginx

回收之前版本

root@nginx sbin]# kill -USR2 56017

查看进程

root@nginx sbin# ps aux | grep nginx

生效

root@nginx sbin# curl -I localhost

HTTP/1.1 200 OK

Server: nginx/1.26.2

Date: Thu, 15 Aug 2024 04:00:25 GMT

Content-Type: text/html

Content-Length: 615

Last-Modified: Thu, 15 Aug 2024 03:08:59 GMT

Connection: keep-alive

ETag: "66bd714b-267"

Accept-Ranges: bytes

回滚

root@nginx sbin]# cp nginx nginx.26.2

root@nginx sbin# ls

nginx nginx.24 nginx.26.2

root@nginx sbin# mv nginx.24 nginx

mv: overwrite 'nginx'? y

root@nginx sbin# kill -HUP 56017

root@nginx sbin# ps aux | grep nginx

root 56017 0.0 0.0 9836 2548 ? Ss 11:42 0:00 nginx: master process /usr/local/nginx/sbin/nginx

root 67914 0.0 0.1 9840 6076 ? S 11:59 0:00 nginx: master process /usr/local/nginx/sbin/nginx

nginx 67915 0.0 0.1 13728 5252 ? S 11:59 0:00 nginx: worker process

nginx 72961 0.0 0.1 13724 4804 ? S 12:14 0:00 nginx: worker process

root 73077 0.0 0.0 221664 2228 pts/1 S+ 12:14 0:00 grep --color=auto nginx

root@nginx sbin# kill -WINCH 67914

root@nginx sbin# ps aux | grep nginx

root 56017 0.0 0.0 9836 2548 ? Ss 11:42 0:00 nginx: master process /usr/local/nginx/sbin/nginx

root 67914 0.0 0.1 9840 6076 ? S 11:59 0:00 nginx: master process /usr/local/nginx/sbin/nginx

nginx 72961 0.0 0.1 13724 4804 ? S 12:14 0:00 nginx: worker process

root 73471 0.0 0.0 221664 2288 pts/1 S+ 12:15 0:00 grep --color=auto nginx

[roHTTP/1.1 200 OK

Server: nginx/1.24.0

Date: Thu, 15 Aug 2024 04:15:52 GMT

Content-Type: text/html

Content-Length: 615

Last-Modified: Thu, 15 Aug 2024 03:08:59 GMT

Connection: keep-alive

ETag: "66bd714b-267"

Accept-Ranges: bytesot@nginx sbin]# curl -I localhost

niginx的常用参数

root@nginx \~# vim /usr/local/nginx/conf/nginx.conf

启动nginx工作进程的用户和组

启动nginx工作进程的数量

错误日志记录配置

pid文件保存路径

重启服务

root@nginx \~# nginx -s reload

nginx独立文件编写

root@nginx \~# vim /lib/systemd/system/nginx.service

重启服务

root@nginx \~# nginx -s reload

root@nginx \~# systemctl start nginx

location匹配用法

root@nginx \~# mkdir -p /data/web{1..5}/test

[root@nginx ~# echo web1 > /data/web1/test/index.html

root@nginx \~# echo web2 > /data/web2/test/index.html

root@nginx \~# echo web3 > /data/web3/test/index.html

root@nginx \~# echo web4 > /data/web4/test/index.html

root@nginx \~# echo web5 > /data/web5/test/index.html

root@nginx \~# vim /usr/local/nginx/conf.d/vhost.conf

root@nginx \~# nginx -s reload

自定义日志

自定义错误页

server {

listen 80;

server_name www.timingwxq.org;

root /data/web/html;

index index.html;

error_page 404 /40x.html;

location /yu {

root /data/web;

auth_basic "login password !!";

auth_basic_user_file "/usr/local/nginx/.htpasswd";

}

location = /40x.html {

root /data/web/errorpage;

}

}

root@nginx \~# mkdir -p /data/web/errorpage

root@nginx \~# echo error page > /data/web/errorpage/40x.html

root@nginx \~# nginx -s reload

自定义错误日志

root@nginx \~# mkdir /var/log/jieyu.org/

root@nginx \~# nginx -s reload

root@nginx \~# cat /var/log/timingwxq.org/access.log

root@nginx \~# cat /var/log/timingwxq.org/error.log

文件检测

root@nginx \~# vim /usr/local/nginx/conf.d/vhost.conf

root@nginx \~# mkdir /data/web/html/error

root@nginx \~# echo error default > /data/web/html/error/default.html

nginx中的长链接管理

vim /usr/local/nginx/conf/nginx.conf------进入nginx的主配置文件进行配置

下载测试工具

root@nginx \~# dnf install telnet -y

root@nginx \~# echo www.timingwxq.org > /data/web/html/error/default.html

nginx下载服务器设置

root@nginx \~# mkdir /data/web/download

root@nginx \~# dd if=/dev/zero of=/data/web/download/leefile bs=1M count=100

root@nginx \~# vim /usr/local/nginx/conf.d/vhost.conf

root@nginx \~# nginx -s reload

nginx的状态页面

root@nginx \~# vim /usr/local/nginx/conf.d/status.conf

root@nginx \~# nginx -s reload

由于只允许172.25.254.1通过,所以本地访问会报错

nginx的数据压缩功能

root@nginx \~# cd /usr/local/nginx/conf/nginx.conf

root@nginx conf]# nginx -s reload

root@nginx conf# echo Tomorrow will be fine > /data/web/html/small.html

root@nginx conf# du -sh /usr/local/nginx/logs/access.log

16K /usr/local/nginx/logs/access.log

root@nginx conf# cat /usr/local/nginx/logs/access.log > /data/web/html/big.html

nginx的变量详解

nginx配置代码

root@nginx \~# vim /usr/local/nginx/conf.d/vars.conf

nginx自定义变量

server {

listen 80;

server_name var.timinglee.org;

root /data/web/html;

index index.html;

location /var {

default_type text/html;

set $timinglee lee;

echo $timinglee;

}

}

测试

root@nginx \~# curl -b "key1=wxq,key2=wxq1" -u wxq:wxq var.timingwxq.org/var?name=wxq&&id=6666

nginx源码编译php

root@nginx-node1 \~# tar zxf nginx-1.26.1.tar.gz

root@nginx-node1 \~# cd nginx-1.26.1/

root@nginx-node1 nginx-1.26.1# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --add-module=/root/echo-nginx-module-0.63 --add-module=/root/memc-nginx-module-0.20 --add-module=/root/srcache-nginx-module-0.33 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

root@nginx-node1 nginx-1.26.1 make&&make install
#利用yum解决php依赖

root@nginx-node1 \~# yum install -y bzip2 systemd-devel libxml2-devel sqlite-devel

libpng-devel libcurl-devel

#下载oniguruma-devel包

root@nginx-node1 \~# cd /mnt

root@nginx-node1 \~# wget https://mirrors.aliyun.com/rockylinux/9.4/devel/x86_64/kickstart/Packages/o/oniguruma-devel-6.9.6-1.el9.5.x86_64.rpm

root@nginx-node1 \~# yum localinstall oniguruma-devel-6.9.6-1.el9.5.x86_64.rpm

#解压源码并安装

root@nginx-node1 \~# tar zxf php-8.3.9.tar.gz

root@nginx-node1 \~# cd php-8.3.9/

root@nginx-node1 php-8.3.9# ./configure \

--prefix=/usr/local/php \

--with-config-file-path=/usr/local/php/etc \

--enable-fpm \

--with-fpm-user=nginx \

--with-fpm-group=nginx \

--with-curl \

--with-iconv \

--with-mhash \

--with-zlib \

--with-openssl \

--enable-mysqlnd \

--with-mysqli \

--with-pdo-mysql \

--disable-debug \

--enable-sockets \

--enable-soap \

--enable-xml \

--enable-ftp \

--enable-gd \

--enable-exif \

--enable-mbstring \

--enable-bcmath \

--with-fpm-systemd\

root@nginx-node1 php-8.3.9# make && make install

php配置优化

root@nginx php-8.3.9# cd /usr/local/php/etc/

root@nginx etc# cp -p php-fpm.conf.default php-fpm.conf

root@nginx etc# vim php-fpm.conf

root@nginx php-fpm.d# vim www.conf


root@nginx-node1 php-fpm.d# cd /root/php-8.3.9/

root@nginx-node1 php-8.3.9# cp php.ini-production /usr/local/php/etc/php.ini

root@nginx-node1 php-8.3.9# vim /usr/local/php/etc/php.ini------时区更改

服务启动

root@nginx system# systemctl start php-fpm.service

root@nginx system# netstat -antlupe | grep php

root@nginx fpm# cd /usr/local/php/bin/

添加php环境变量

root@nginx bin# vim ~/.bash_profile

root@nginx bin# source ~/.bash_profile

测试网页

root@nginx-node1 fpm# mkdir /data/web/php -p

root@nginx-node1 fpm# vim /data/web/php/index.php

测试

高速缓存

root@Nginx \~# rm -fr /apps/nginx/

root@Nginx \~# tar zxf srcache-nginx-module-0.33.tar.gz

root@Nginx \~# tar zxf memc-nginx-module-0.20.tar.gz

root@Nginx \~# cd nginx-1.26.1/

root@Nginx nginx-1.26.1# ./configure --prefix=/apps/nginx --user=nginx --

group=nginx --with-http_ssl_module --with-http_v2_module --withhttp_realip_module --with-http_stub_status_module --with-http_gzip_static_module

--with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --

add-module=/root/memc-nginx-module-0.20 --add-module=/root/srcache-nginx-module-

0.33

root@Nginx nginx-1.26.1# make && make install

root@Nginx \~# vim /apps/nginx/conf.d/php.conf

upstream memcache {

server 127.0.0.1:11211;

keepalive 512;

}

server {

listen 80;

server_name php.timinglee.org;

root /data/php;

location /memc {

internal;

memc_connect_timeout 100ms;

memc_send_timeout 100ms;

memc_read_timeout 100ms;

set memc_key query_string; #使用内置变量$query_string来作为key

set $memc_exptime 300; #缓存失效时间300秒

memc_pass memcache;

}

location ~ \.php$ {

set key uri$args; #设定key的值

srcache_fetch GET /memc $key; #检测mem中是否有要访问的php

srcache_store PUT /memc $key; #缓存为加载的php数据

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi.conf;

}

}

root@Nginx \~# systemctl start nginx.service

相关推荐
zyl8372120 小时前
Docker 使用手册
运维·docker·容器
古月方枘Fry20 小时前
MGRE实验
运维·服务器
博客-小覃20 小时前
Zabbix之华为交换机的日志记录信息操作详细教程
服务器·网络·华为·zabbix
stolentime21 小时前
FreeDomain 本地开发环境快速搭建指南
运维·服务器·网络
向量引擎21 小时前
从零起步,如何打造专属向量引擎 API 中转工作流?
java·服务器·前端
z200509301 天前
【Linux学习】Linux中的进程程序替换
linux·服务器·学习
bush41 天前
嵌入式linux学习记录四
linux·运维·学习
lihao lihao1 天前
软硬链接
linux·运维·服务器
TOWE technology1 天前
智能安防监控系统如何做好防雷?——视频信号SPD综合应用方案解析
运维·服务器·防雷产品·信号保护·信号防雷·spd
楼田莉子1 天前
Docker学习:Docker介绍及其架构介绍
运维·后端·学习·docker·容器·架构