- 功能
- 静态资源web服务器
- 结合FastCGI/SCGI等进行反向代理请求资源
- http/https的反向代理
- 邮件协议的反向代理
- tcp/udp的请求转发
- 组织架构
- nginx由一个master主进程和worker工作进程组成
- 主进程
- 读取nginx配置文件验证其有效性和正确性
- 建立,绑定,关闭socket连接
- 按照配置生成,管理,结束工作进程
- 接受外界指令,重启,升级及退出服务器等指令
- 不中断服务升级,重新加载配置,失败进行回滚处理
- 开启日志服务
- 编译及处理perl脚本
- 工作进程
- 接受客户的请求
- 将请求送入各个功能模块进行处理
- IO调用,获取数据
- 与后端服务器通信,接受后端服务器的处理结果
- 缓存数据,访问缓存索引,查询,调用缓存数据
- 发送请求结果,响应客户的请求
- 主进程
- nginx由一个master主进程和worker工作进程组成
- 模块
- 核心:core module
- 标准:ngxhttp*
- HTTP core modules 默认功能
- HTTP Optional modules 编译指定
- Mail:ngxmail*
- Stream:ngxstream*
- 第三方模块
安装
yum 安装
#安装epel源yum install epel-release -y#安装nginxyum install nginx -y#检查包安装信息rpm -ql nginx#验证nginxnginx -v / nginx -V
编译安装 ```shell
准备编译基础环境
yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib-devel bash-completion nfsutils automake libxml2 libxml2-devel libxslt libxslt-devel perl perlExtUtils-Embed
下载软件包并解压
cd /usr/local/src/ wget https://nginx.org/download/nginx-1.18.0.tar.gz tar xvf nginx-1.18.0.tar.gz
开始编译
cd nginx-1.18.0.tar.gz ./configure —prefix=/apps/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 —with-file-aio —add-module=/usr/local/src/echo-nginx-module —with-openssl=/usr/local/src/openssl-1.1.1d —with-threads make && make install
添加用户
useradd nginx -s /sbin/nologin -u 2000
修改目录属组
chown -R nginx.nginx /apps/nginx
验证
/apps/nginx/sbin/nginx -v
创建软连接
ln -s /apps/nginx/sbin/nginx /usr/sbin/
创建自启动脚本
vim /usr/lib/systemd/system/nginx.service [Unit] Description=The nginx HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong# SELinux context. This might happen when running `nginx -t` from the cmdline.# https://bugzilla.redhat.com/show_bug.cgi?id=1268621ExecStartPre=/usr/bin/rm -f /run/nginx.pidExecStartPre=/apps/nginx/sbin/nginx -tExecStart=/apps/nginx/sbin/nginxExecReload=/bin/kill -s HUP $MAINPIDKillSignal=SIGQUITTimeoutStopSec=5KillMode=processPrivateTmp=true[Install]WantedBy=multi-user.target
重新加载
systemctl daemon-reload
- **核心配置**- **新建web站点**- ** **```shell#进入目录cd /apps/nginx/conf/mkdir testcd test#编写配置文件cat test001.confserver {listen 80;server_name www.nginx123.com;location / {root /data/nginx/html;}}#创建首页文件mkdir -p /data/nginx/htmlecho "test001" > /data/nginx/html/index.html
创建其他站点
server {listen 80;server_name www.nginx.com;location / {root /data/nginx/html;}location /about {root /data/nginx/html;}#此处访问的路径是 /data/nginx/html/about/index.html#也就是root路径+location的路径}
创建别名访问
server {listen 80;server_name www.nginx.com;location / {root /data/nginx/html;}location /about {alias /data/nginx/html;index index.html;}#此处默认会访问alias下的index.html , 即使about目录不存在}
location匹配
= 匹配成功则立即处理请求~ 区分大小写并匹配 !~ 区分大小写不匹配~* 不区分大小写并匹配 !~* 不区分大小写并且不匹配^~ 匹配开头 $ 匹配结尾\ 转义字符 匹配任意长度的任意字符
综合匹配
server {listen 80;server_name www.nginx.com;location / {root /data/nginx/html;}#精确匹配location = /1.jpg {root /data/nginx/html/images;}#不区分大小写location ~* /A.? {root /data/nginx/html/images;}#区分大小写location ~ /A.? {root /data/nginx/html/images;}#不区分大小写location ~* /aa.jpg {root /data/nginx/html/images;}#开头匹配location ^~ /images {root /data/nginx/html/images;}#结尾匹配location ~* \.(jpg|png)$ {root /data/nginx/html/images;}}#匹配优先级= ^~ ~/~* /
四层访问控制
location /about {alias /data/nginx/html;deny 10.0.10.0/24;deny 192.168.10.3;allow all;}
账户认证 ```shell
安装httpd-tools软件工具
yum install httpd-tools -y apt install apache2-utils
生成用户
htpasswd -cbm /apps/nginx/conf/.htpasswd user1 123123 htpasswd -bm /apps/nginx/conf/.htpasswd user2 123123 第一次加c 往后就可以不用加了
location = /login/ { root /data/nginx/html; auth_basic “login password”; auth_basic_user_file /apps/nginx/conf/.htpasswd; }
- **自定义错误页面**```shellserver {listen 80;server_name www.nginx.com;error_page 500 502 503 504 403 /error.html;location = /error.html {root /data/nginx/html;}}
- **自定义访问日志**
server {listen 80;server_name www.nginx.com;access_log /data/nginx/logs/www-nginx-com_access.log;error_log /data/nginx/logs/www-nginx-com_error.log;location / {root /data/nginx/html;}}
- ** 检测文件是否存在**
location /about {root /data/nginx/html;try_files $uri $uri/index.html $uri.html /about/default.html;#try_files $uri $uri/index.html $uri.html =888; 返回状态码}
- ** 长连接配置**
http {#请求最大数量,默认为100keeplive_requests 3;#超时时长,默认为75s 第二个值是返回给客户端看的,并无实际意义keeplive_timeout 65 100;}
- ** 下载服务器**
location /download {autoindex on; #自动索引autoindex_exact_size on; #显示文件精确大小autoindex_localtime on; #显示本机时间,而非格林时间limit_rate 10k; #限制下载速度}
- ** 上传服务器**
location /upload {root /data/nginx/html;#单个文件最大值为10mclient_max_body_size 10m;#超出该值,则将数据缓存到磁盘client_body_buffer_size 16k;#设置存储的路径及目录,16 * 256 * 256 = 1048576client_body_temp_path /apps/temp/ 1 2 2;}
- ** 其他配置**
#限制除GET HEAD 之外的方法仅允许某个网段使用location /upload {root /data/nignx/html;limit_except GET {allow 10.0.10.0/24;deny all;}}#启用AIO功能,需要编译开启 --with-file-aiolocation /video {root /data/nginx;sendfile on;aio on;directio 8m; #大于8m使用磁盘IO , 小于使用sendfiledirectio_alignment 512;}#开启多线程--with-threadsevents{worker_connections 1024;use epoll;accept_mutex on;multi_accept on;}thread_pool pool11 threads=16;thread_pool pool22 threads=32;#调用location /video {root /data/nginx;sendfile on;aio thread=pool11;directio 8m;directio_alignment 4096;}通过/proc/ngxin进程ID/status 查看 threads值#缓存打开的文件open_file_cache on; #开启缓存文件信息open_file_cache max=100 inactive=60s; #最大缓存文件个数,非活动数据超时时间open_file_cache_valid 60s;#每隔60s检查缓存数据有效性open_file_cache_min_uses 5;#60秒内至少被访问5次才被标记open_file_cache_errors on;#缓存错误信息#隐藏nginx版本信息server_tokens off;
- ** 零拷贝**
- **零拷贝,一般有 mmap 和 sendFile 两种**- **传统 IO 执行的话需要 4 次上下文切换(用户态 -> 内核态 -> 用户态 -> 内核态 -> 用户态)和 4 次拷贝(磁盘文件 DMA 拷贝到内核缓冲区,内核缓冲区 CPU 拷贝到用户缓冲区,用户缓冲区 CPU 拷贝到 Socket 缓冲区,Socket 缓冲区 DMA 拷贝到协议引擎)**- **mmap 将磁盘文件映射到内存,支持读和写,对内存的操作会反映在磁盘文件上,适合小数据量读写,需要 4 次上下文切换(用户态 -> 内核态 -> 用户态 -> 内核态 -> 用户态)和3 次拷贝(磁盘文件DMA拷贝到内核缓冲区,内核缓冲区 CPU 拷贝到 Socket 缓冲区,Socket 缓冲区 DMA 拷贝到协议引擎)。**- **sendfile 是将读到内核空间的数据,转到 socket buffer,进行网络发送,适合大文件传输,只需要 2 次上下文切换(用户态 -> 内核态 -> 用户态)和 2 次拷贝(磁盘文件 DMA 拷贝到内核缓冲区,内核缓冲区 DMA 拷贝到协议引擎)。**
