nginx入门一篇足够

本文详细介绍了Nginx服务器的功能,包括其作为高性能HTTP和反向代理服务器的角色,以及如何在Windows和Linux环境下进行安装。此外,还提供了Nginx的编译配置过程,包括如何动态添加模块,以及一系列管理和操作命令,如启动、重启、停止和服务管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

介绍

Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆被广泛使用。

安装

windows

直接下载:http://nginx.org/en/download.html

linux-安装nginx

Ngnix 下载 http://nginx.org/en/download.html

tar -xvf nginx*.tar.gz

如果不指定用户默认的nobody 所以最好指定用户,指定用户前要先创建
【yum安装的会默认创建一个nginx用户】

useradd nginx -s /sbin/nologin -M

【如果系统已经存在可以删除】

userdel –r nginx

依赖
#yum -y install gcc gcc-c++ openssl-devel zlib-devel pcre pcre-devel perl perl-devel perl-ExtUtils-Embed
configure
【见编译配置文件】 尽量多安装的,以后添加和麻烦

./configure \
	--user=nginx \
	--group=nginx \
	--with-http_sub_module \
	--with-http_stub_status_module \
	--with-http_realip_module \
	--with-http_ssl_module \
	--with-http_flv_module \
	--with-http_gzip_static_module \
	--with-pcre \
	--with-http_perl_module 

#make
#make install

Nginx 的默认路径是在/usr/local/nginx
【–prefix= 指定 注意:如果用yum源 会安装在/etc/nginx】

动态添加模块

已经安装好的nginx,需要添加一个未被编译安装的模块

  1. 查看nginx编译安装时安装了哪些模块
#/usr/local/nginx/sbin/nginx -V  【安装的信息】
  1. 加入需要安装的模块,重新编译 [以 with-http_addition_module 为例]
#./configure \
---user=nginx \
--group=nginx \
--with-http_sub_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_flv_module \
--with-http_gzip_static_module \
--with-http_perl_module \
--with-pcre \
--with-http_addition_module 

#make //不要make install,不然就真的覆盖了
3.停止nginx 然后替换nginx二进制文件
4.启动
ok

编译配置文件

./configure --help

–prefix=<path> – Nginx安装路径。如果没有指定,默认为 /usr/local/nginx。 
–sbin-path=<path> – Nginx可执行文件安装路径。只能安装时指定,如果没有指定,默认为<prefix>/sbin/nginx。 
–conf-path=<path> – 在没有给定-c选项下默认的nginx.conf的路径。如果没有指定,默认为<prefix>/conf/nginx.conf。 
–pid-path=<path> – 在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。如果没有指定,默认为 <prefix>/logs/nginx.pid。 
–lock-path=<path> – nginx.lock文件的路径。 
–error-log-path=<path> – 在nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。如果没有指定,默认为 <prefix>/logs/error.log。 
–http-log-path=<path> – 在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为 <prefix>/logs/access.log。 
–user=<user> – 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody。 
–group=<group> – 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的组。如果没有指定,默认为 nobody。 
–builddir=DIR – 指定编译的目录 
–with-rtsig_module – 启用 rtsig 模块 
–with-select_module –without-select_module – 允许或不允许开启SELECT模式,如果 configure 没有找到更合适的模式,比如:kqueue(sun os),epoll (linux kenel 2.6+), rtsig(实时信号)或者/dev/poll(一种类似select的模式,底层实现与SELECT基本相 同,都是采用轮训方法) SELECT模式将是默认安装模式 
–with-poll_module –without-poll_module – Whether or not to enable the poll module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure. 
–with-http_ssl_module – 开启HTTP SSL模块,使NGINX可以支持HTTPS请求。这个模块需要已经安装了OPENSSL,在DEBIAN上是libssl 
–with-http_realip_module – 启用 ngx_http_realip_module 
–with-http_addition_module – 启用 ngx_http_addition_module 
–with-http_sub_module – 启用 ngx_http_sub_module 
–with-http_dav_module – 启用 ngx_http_dav_module 
–with-http_flv_module – 启用 ngx_http_flv_module 
–with-http_stub_status_module – 启用 “server status” 页 
–without-http_charset_module – 禁用 ngx_http_charset_module 
–without-http_gzip_module – 禁用 ngx_http_gzip_module. 如果启用,需要 zlib 。 
–without-http_ssi_module – 禁用 ngx_http_ssi_module 
–without-http_userid_module – 禁用 ngx_http_userid_module 
–without-http_access_module – 禁用 ngx_http_access_module 
–without-http_auth_basic_module – 禁用 ngx_http_auth_basic_module 
–without-http_autoindex_module – 禁用 ngx_http_autoindex_module 
–without-http_geo_module – 禁用 ngx_http_geo_module 
–without-http_map_module – 禁用 ngx_http_map_module 
–without-http_referer_module – 禁用 ngx_http_referer_module 
–without-http_rewrite_module – 禁用 ngx_http_rewrite_module. 如果启用需要 PCRE 。 
–without-http_proxy_module – 禁用 ngx_http_proxy_module 
–without-http_fastcgi_module – 禁用 ngx_http_fastcgi_module 
–without-http_memcached_module – 禁用 ngx_http_memcached_module 
–without-http_limit_zone_module – 禁用 ngx_http_limit_zone_module 
–without-http_empty_gif_module – 禁用 ngx_http_empty_gif_module 
–without-http_browser_module – 禁用 ngx_http_browser_module 
–without-http_upstream_ip_hash_module – 禁用 ngx_http_upstream_ip_hash_module 
–with-http_perl_module – 启用 ngx_http_perl_module 
–with-perl_modules_path=PATH – 指定 perl 模块的路径 
–with-perl=PATH – 指定 perl 执行文件的路径 
–http-log-path=PATH – 指定access log 文件的路径 
–http-client-body-temp-path=PATH – 指定http客户端请求缓存文件存放的目录 
–http-proxy-temp-path=PATH – 指定http反向代理缓存文件存放的目录 
–http-fastcgi-temp-path=PATH – 指定http fastCGI缓存文件存放的目录 
–without-http – 禁用 HTTP server 
–with-mail – 启用 IMAP4/POP3/SMTP 代理模块 
–with-mail_ssl_module – 启用 ngx_mail_ssl_module 
–with-cc=PATH – 指定 C 编译器的路径 
–with-cpp=PATH – 指定 C 预处理器的路径 
–with-cc-opt=OPTIONS – Additional parameters which will be added to the variable CFLAGS. With the use of the system library PCRE in FreeBSD, it is necessary to indicate –with-cc-opt=”-I /usr/local/include”. If we are using select() and it is necessary to increase the number of file descriptors, then this also can be assigned here: –with-cc-opt=”-D FD_SETSIZE=2048″. 
–with-ld-opt=OPTIONS – Additional parameters passed to the linker. With the use of the system library PCRE in FreeBSD, it is necessary to indicate –with-ld-opt=”-L /usr/local/lib”. 
–with-cpu-opt=CPU – 为特定的 CPU 编译,有效的值包括:pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64 
–without-pcre – 禁止 PCRE 库的使用。同时也会禁止 HTTP rewrite 模块。在 “location” 配置指令中的正则表达式也需要 PCRE 。 
–with-pcre=DIR – 指定 PCRE 库的源代码的路径。 
–with-pcre-opt=OPTIONS – 设置pcre库的源代码路径 
–with-md5=DIR – 设置MD5库的源代码 路径 
–with-md5-opt=OPTIONS – MD5库的额外编译选项 
–with-md5-asm – MD5汇编源码 
–with-sha1=DIR – sha1库的 源代码 路径 
–with-sha1-opt=OPTIONS – sha1库的 额外编译选项 
–with-sha1-asm – 使用sha1 汇编源码 
–with-zlib=DIR – zlib库的源代码路径 
–with-zlib-opt=OPTIONS – zlib库的编译选项 
–with-zlib-asm=CPU – zlib库针对CPU优化,值有: pentium, pentiumpro 
–with-openssl=DIR – openssl库的源代码路径 
–with-openssl-opt=OPTIONS – openssl编译选项 
–with-debug – 启用调试日志 
–add-module=PATH – 增加第三方模块所在的路径

常用命令

查看

#/usr/local/nginx/sbin/nginx -V 【安装的信息】
#/usr/local/nginx/sbin/nginx -v 【版本】

启动:

#/usr/local/nginx/sbin/nginx

检查nginx配置文件语法有没问题

#/usr/local/nginx/sbin/nginx –t

重启的命令

#/usr/local/nginx/sbin/nginx -s reload

重新加载配置文件 【生产上尽量使用这种方式】
#kill -1 NGINX主线程号
停止:

#/usr/local/nginx/sbin/nginx -s stop
#nginx -s stop :快速停止nginx

		 		 quit  :完整有序的停止nginx

查看nginx进程

#ps -ef|grep nginx

nginx从容停止命令,等所有请求结束后关闭服务

kill -QUIT nginx主进程号

nginx 快速停止命令,立刻关闭nginx进程

kill -TERM nginx主进程号 

如果以上命令不管用,可以强制停止

kill -9 nginx主进程号

架构说明

在这里插入图片描述

1)nginx启动时,生成master进程 不处理网络请求,主要负责调度工作进程,也就是图示的三项:加载配置、启动工作进程、非停升级。所以,nginx启动以后,查看操作系统的进程列表,我们就能看到至少有两个nginx进程。
2)服务器实际处理网络请求及响应的是工作进程(worker)[使用的NIO],在类unix系统上,nginx可以配置多个worker,而每个worker进程都可以同时处理数以千计的网络请求。
3)模块化设计。nginx的worker,包括核心和功能性模块,核心模块负责维持一个运行循环(run-loop),执行网络请求处理的不同阶段的模块功能,如网络读写、存储读写、内容传输、外出过滤,以及将请求发往上游服务器等。而其代码的模块化设计,也使得我们可以根据需要对功能模块进行适当的选择和修改,编译成具有特定功能的服务器。
4)事件驱动、异步及非阻塞,可以说是nginx得以获得高并发、高性能的关键因素,同时也得益于对Linux、Solaris及类BSD等操作系统内核中事件通知及I/O性能增强功能的采用,如kqueue、epoll及event ports。

模块介绍

在这里插入图片描述

关于代理

在这里插入图片描述

正向代理

正向代理是指客户端与目标服务器之间增加一个代理服务器,客户端直接访问代理服务器,在由代理服务器访问目标服务器并返回客户端并返回 。这个过程当中客户端需要知道代理服务器地址,并配置连接。
在这里插入图片描述

反向代理

反向代理是指 客户端访问目标服务器,在目标服务内部有一个统一接入网关将请求转发至后端真正处理的服务器并返回结果。这个过程当中客户端不需要知道代理服务器地址,代理对客户端而言是透明的。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值