模块与工作原理
nginx由内核和模块组成。其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block(location是nginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。
模块分类
nginx的模块从结构上 分为核心模块、基础模块和第三方模块
- HTTP模块、EVENT模块和MAIL模块等属于核心模块
- HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块和HTTP Rewrite模块属于基本模块
- HTTP Upstream模块、Request Hash模块、Notice模块和HTTP Access Key模块属于第三方模块
用户根据自己的需要开发的模块都属于第三方模块。 nginx模块从功能上 分为三类,分别是:
- Handlers(处理器模块)。此类模块直接处理请求,并进行输出内容和修改headers信息等操作。handlers处理器模块一般只能有一个
- Filters(过滤器模块)。此类模块主要对其他处理器模块输出的内容进行修改操作,最后由nginx输出
- Proxies(代理器模块)。就是nginx的HTTP Upstream之类的模块,这些模块主要与后端一些服务比如fastcgi等操作交互,实现服务代理和负载均衡等功能
nginx模块分为:核心模块、事件模块、标准Http模块、可选Http模块、邮件模块、第三方模块和补丁等
nginx基本模块:所谓基本模块,指的是nginx默认的功能模块,它们提供的指令,允许你使用定义nginx基本功能的变量,在编译时不能被禁用,包括:
- 核心模块:基本功能和指令,如进程管理和安全。常见的核心模块指令,大部分是放置在配置文件的顶部
- 事件模块:在Nginx内配置网络使用的能力。常见的events(事件)模块指令,大部分是放置在配置文件的顶部
- 配置模块:提供包含机制
更多的指令,请参考nginx官方文档
nginx的工作原理
nginx的模块直接被编译进nginx,因此属于静态编译方式。
启动nginx后,nginx的模块被自动加载 ,与Apache不一样,首先将模块编译为一个so文件,然后在配置文件中指定是否进行加载。
在解析配置文件时,nginx的每个模块都有可能去处理某个请求,但是同一个处理请求只能由一个模块来完成。
nginx的进程架构: 启动nginx时,会启动一个Master进程,这个进程不处理任何客户端的请求,主要用来产生worker线程,一个worker线程用来处理n个request。
worker 进程中,ngx_worker_process_cycle()函数就是这个无限循环的处理函数。在这个函数中,一个请求的简单处理流程如下:
- 操作系统提供的机制(例如 epoll, kqueue 等)产生相关的事件。
- 接收和处理这些事件,如是接收到数据,则产生更高层的 request 对象。
- 处理 request 的 header 和 body。
- 产生响应,并发送回客户端。
- 完成 request 的处理。
- 重新初始化定时器及其他事件。
多进程模型的处理方式:
- 首先,master进程一开始就会根据我们的配置,来建立需要listen的网络socket fd,然后fork出多个worker进程。
- 其次,根据进程的特性,新建立的worker进程,也会和master进程一样,具有相同的设置。因此,其也会去监听相同ip端口的套接字socket fd。
然后,这个时候有多个worker进程都在监听同样设置的socket fd,意味着当有一个请求进来的时候,所有的worker都会感知到。这样就会产生所谓的“惊群现象”。为了保证只会有一个进程成功注册到listenfd的读事件,nginx中实现了一个“accept_mutex”类似互斥锁,只有获取到这个锁的进程,才可以去注册读事件。其他进程全部accept 失败。 - 最后,监听成功的worker进程,读取请求,解析处理,响应数据返回给客户端,断开连接,结束。因此,一个request请求,只需要worker进程就可以完成。
nginx模块一次常规的HTTP请求和响应的过程 一个典型的HTTP处理周期: 7. 客户端发送HTTP请求 8. Nginx基于配置文件中的位置选择一个合适的处理模块 9. (如果有)负载均衡模块选择一台后端服务器 10. 处理模块进行处理并把输出缓冲放到第一个过滤模块上 11. 第一个过滤模块处理后输出给第二个过滤模块 12. 然后第二个过滤模块又到第三个 13. 依此类推 –> 最后把响应发给客户端。
Nginx本身做的工作实际很少,当它接到一个HTTP请求时,它仅仅是通过查找配置文件将此次请求映射到一个location block,而此location中所配置的各个指令则会启动不同的模块去完成工作,因此模块可以看做Nginx真正的劳动工作者。
基本的WEB服务请求步骤
- 建立连接 — 接受一个客户端连接,或者如果不希望与这个客户端建立连接,就将其关闭。
- 接收请求 — 从网络中读取一条 HTTP 请求报文。
- 处理请求 — 对请求报文进行解释,并采取行动。
- 访问资源 — 访问报文中指定的资源。
- 构建响应 — 创建带有正确首部的 HTTP 响应报文。
- 发送响应 — 将响应回送给客户端。
- 记录事务处理过程 — 将与已完成事务有关的内容记录在一个日志文件中。
nginx部署
nginx的安装
# 关闭防火墙和selinux
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@localhost ~]# reboot
[root@localhost ~]# getenforce 0
Disabled
# 创建系统用户nginx
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
# 安装依赖环境
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ vim wget make
[root@localhost ~]# yum -y groups mark install 'Development Tools'
# 创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
# 下载nginx
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz
# 编译安装
[root@localhost src]# ls
debug kernels nginx-1.12.0.tar.gz
[root@localhost src]# tar xf nginx-1.12.0.tar.gz
[root@localhost src]# cd nginx-1.12.0
[root@localhost nginx-1.12.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@localhost nginx-1.12.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
# 配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# . /etc/profile.d/nginx.sh
# 启动nginx
[root@localhost ~]# nginx
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
# 写service文件让nginx开机自启
[root@localhost ~]# cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.
nginx的配置文件详解
主配置文件:/usr/local/nginx/conf/nginx.conf
- 默认启动nginx时,使用的配置文件是:安装路径/conf/nginx.conf文件
- 可以在启动nginx时通过-c选项来指定要读取的配置文件
nginx常见的配置文件及其作用
配置文件 | 作用 |
---|
nginx.conf | nginx的基本配置文件 | mime.types | MIME类型关联的扩展文件 | fastcgi.conf | 与fastcgi相关的配置 | proxy.conf | 与proxy相关的配置 | sites.conf | 配置nginx提供的网站,包括虚拟主机 |
nginx.conf的内容分为以下几段:
- main配置段:全局配置段。其中main配置段中可能包含event配置段
- event {}:定义event模型工作特性
- http {}:定义http协议相关的配置
配置指令:要以分号结尾,语法格式如下:
derective value1 [value2 ...];
支持使用变量:
- 内置变量:模块会提供内建变量定义
- 自定义变量:set var_name value
用于调试、定位问题的配置参数 是否以守护进程方式运行Nginx 守护进程(daemon)是脱离终端并且在后台运行的进程。它脱离终端是为了避免进程执行过程中的信息在任何终端上显示,这样一来,进程也不会被任何终端所产生的信息所打断。Nginx毫无疑问是一个需要以守护进程方式运行的服务,因此,默认都是以这种方式运行的。
daemon {on|off}; //是否以守护进程方式运行nginx,调试时应设置为off
master_process {on|off}; //是否以master/worker模型来运行nginx,调试时可以设置为off
error_log 位置 级别; //配置错误日志
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
daemon off;
[root@localhost ~]# nginx -s stop;nginx
正常运行必备的配置参数
不过Nginx还是提供了关闭守护进程的模式,之所以提供这种模式,是为了方便跟踪调试Nginx,毕竟用gdb调试进程时最烦琐的就是如何继续跟进fork出的子进程了。
# user USERNAME [GROUPNAME] //指定允许worker进程的用户和组
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;
# pid /path/to/pid_file //指定nginx守护进程的pid文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
pid logs/nginx.pid;
[root@localhost ~]# ls /usr/local/nginx/logs/
nginx.pid
[root@localhost ~]# systemctl stop nginx.service
[root@localhost ~]# ls /usr/local/nginx/logs/
# worker_rlimit_nofile number //设置所有woker进程最大可以打开的文件数,默认1024
# worker_rlimit_core size //指明所有worker进程所能够使用的总体的最大核心文件大小,保持默认即可
优化性能的配置参数
worker_processes n; //启动n个worker进程,这里的n为了避免上下文切换,通常设置为cpu总核心数-1或等于总核心数
worker_cpu_affinity cpumask ...; //将进程绑定到某cpu中,避免频繁刷新缓存
//cpumask:使用8位二进制表示cpu核心,如:
0000 0001 //第一颗cpu核心
0000 0010 //第二颗cpu核心
0000 0100 //第三颗cpu核心
0000 1000 //第四颗cpu核心
0001 0000 //第五颗cpu核心
0010 0000 //第六颗cpu核心
0100 0000 //第七颗cpu核心
1000 0000 //第八颗cpu核心
#查看cpu
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 3;
worker_cpu_affinity 0001 0010 0100;
[root@localhost ~]# systemctl restart nginx.service
[root@localhost ~]# ps -ef | grep nginx
root 1571 1 0 23:13 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 1572 1571 0 23:13 ? 00:00:00 nginx: worker process
nginx 1573 1571 0 23:13 ? 00:00:00 nginx: worker process
nginx 1574 1571 0 23:13 ? 00:00:00 nginx: worker process
root 1576 1485 0 23:13 pts/0 00:00:00 grep --color=auto nginx
[root@localhost ~]# top
top - 23:31:29 up 22 min, 1 user, load average: 0.00, 0.00, 0.00
Tasks: 223 total, 1 running, 222 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 3752.0 total, 3300.8 free, 216.8 used, 234.4 buff/cache
MiB Swap: 4044.0 total, 4044.0 free, 0.0 used. 3305.5 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 242256 10628 8180 S 0.0 0.3 0:00.92 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
#按shift+f ,输入nginx,回车
Locate string nginx
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1485 root 20 0 26516 4928 3452 S 0.0 0.1 0:00.03 bash
1571 root 20 0 80256 1092 56 S 0.0 0.0 0:00.00 nginx
1572 nginx 20 0 111708 6276 4700 S 0.0 0.2 0:00.00 nginx
1573 nginx 20 0 111708 6276 4700 S 0.0 0.2 0:00.00 nginx
1574 nginx 20 0 111708 6276 4700 S 0.0 0.2 0:00.00 nginx
# 按f, 将光标移到 P = Last Used Cpu (SMP)
Fields Management for window 1:Def, whose current sort field is %CPU
Navigate with Up/Dn, Right selects for move then <Enter> or Left commits,
'd' or <Space> toggles display, 's' sets sort. Use 'q' or <Esc> to end!
* PID = Process Id WCHAN = Sleeping in Function
* USER = Effective User Name Flags = Task Flags <sched.h>
* PR = Priority CGROUPS = Control Groups
* NI = Nice Value SUPGIDS = Supp Groups IDs
* VIRT = Virtual Image (KiB) SUPGRPS = Supp Groups Names
* RES = Resident Size (KiB) TGID = Thread Group Id
* SHR = Shared Memory (KiB) OOMa = OOMEM Adjustment
* S = Process Status OOMs = OOMEM Score current
* %CPU = CPU Usage ENVIRON = Environment vars
* %MEM = Memory Usage (RES) vMj = Major Faults delta
* TIME+ = CPU Time, hundredths vMn = Minor Faults delta
* COMMAND = Command Name/Line USED = Res+Swap Size (KiB)
PPID = Parent Process pid nsIPC = IPC namespace Inode
UID = Effective User Id nsMNT = MNT namespace Inode
RUID = Real User Id nsNET = NET namespace Inode
RUSER = Real User Name nsPID = PID namespace Inode
SUID = Saved User Id nsUSER = USER namespace Inode
SUSER = Saved User Name nsUTS = UTS namespace Inode
GID = Group Id LXC = LXC container name
GROUP = Group Name RSan = RES Anonymous (KiB)
PGRP = Process Group Id RSfd = RES File-based (KiB)
TTY = Controlling Tty RSlk = RES Locked (KiB)
TPGID = Tty Process Grp Id RSsh = RES Shared (KiB)
SID = Session Id CGNAME = Control Group name
nTH = Number of Threads NU = Last Used NUMA node
P = Last Used Cpu (SMP)
TIME = CPU Time
SWAP = Swapped Size (KiB)
CODE = Code Size (KiB)
DATA = Data+Stack (KiB)
nMaj = Major Page Faults
nMin = Minor Page Faults
nDRT = Dirty Pages Count
# 空格选择,按q
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND P
1571 root 20 0 80256 1092 56 S 0.0 0.0 0:00.00 nginx 2
1572 nginx 20 0 111708 6276 4700 S 0.0 0.2 0:00.00 nginx 0
1573 nginx 20 0 111708 6276 4700 S 0.0 0.2 0:00.00 nginx 1
1574 nginx 20 0 111708 6276 4700 S 0.0 0.2 0:00.00 nginx 2
timer_resolution interval; //计时器解析度。降低此值,可减少gettimeofday()系统调用的次数
worker_priority number; //指明worker进程的nice值 number取19~-20,数值越低优先级越高
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
worker_priority -20;
[root@localhost ~]# systemctl restart nginx.service
[root@localhost ~]# ps -elf |grep nginx
1 S root 1750 1 0 80 0 - 20064 - 00:21 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
5 S nginx 1751 1750 0 60 -20 - 27927 do_epo 00:21 ? 00:00:00 nginx: worker process
5 S nginx 1752 1750 0 60 -20 - 27927 do_epo 00:21 ? 00:00:00 nginx: worker process
5 S nginx 1753 1750 0 60 -20 - 27927 do_epo 00:21 ? 00:00:00 nginx: worker process
0 S root 1756 1485 0 80 0 - 3086 - 00:22 pts/0 00:00:00 grep --color=auto nginx
事件相关的配置:event{}段中的配置参数
accept_mutex {off|on}; //master调度用户请求至各worker进程时使用的负载均衡锁;on表示能让多个worker轮流地、序列化地去响应新请求
lock_file file; //accept_mutex用到的互斥锁锁文件路径
use [epoll | rtsig | select | poll]; //指明使用的事件模型,建议让nginx自行选择
worker_connections #; //每个进程能够接受的最大连接数
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 10240;
}
[root@localhost ~]# dnf -y install httpd-tools
[root@localhost ~]# ab -n 3000 http://192.168.8.132/index.html
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.8.132 (be patient)
Completed 300 requests
Completed 600 requests
Completed 900 requests
Completed 1200 requests
Completed 1500 requests
Completed 1800 requests
Completed 2100 requests
Completed 2400 requests
Completed 2700 requests
Completed 3000 requests
Finished 3000 requests
Server Software: nginx/1.20.1
Server Hostname: 192.168.111.141
Server Port: 80
Document Path: /index.html
Document Length: 612 bytes
Concurrency Level: 1
Time taken for tests: 0.376 seconds
Complete requests: 3000
Failed requests: 0
Total transferred: 2535000 bytes
HTML transferred: 1836000 bytes
Requests per second: 7974.21 [#/sec] (mean)
Time per request: 0.125 [ms] (mean)
Time per request: 0.125 [ms] (mean, across all concurrent requests)
Transfer rate: 6580.28 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 0 0 0.0 0 1
Waiting: 0 0 0.0 0 1
Total: 0 0 0.0 0 1
Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 0
90% 0
95% 0
98% 0
99% 0
100% 1 (longest request)
网络连接相关的配置参数
keepalive_timeout number; //长连接的超时时长,默认为65s
keepalive_requests number; //在一个长连接上所能够允许请求的最大资源数
keepalive_disable [msie6|safari|none]; //为指定类型的UserAgent禁用长连接
tcp_nodelay on|off; //是否对长连接使用TCP_NODELAY选项,为了提升用户体验,通常设为on
client_header_timeout number; //读取http请求报文首部的超时时长
client_body_timeout number; //读取http请求报文body部分的超时时长
send_timeout number; //发送响应报文的超时时长
fastcgi的相关配置参数
# LNMP: php需启用fpm模型
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000; //定义反向代理
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; //.php文件路径
include fastcgi_params;
}
nginx.conf配置文件案例 更改默认端口号以及进程数和指定特定配置文件
[root@localhost conf]# head nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
// 使用80端口
server {
listen 80;
server_name localhost;
// 使用源文件运行进程数如下
[root@localhost conf]# ps -ef | grep nginx
root 257815 1 0 16:46 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 257816 257815 0 16:46 ? 00:00:00 nginx: worker process
root 258199 102060 0 16:46 pts/0 00:00:00 grep --color=auto nginx
// 将源文件以及mime.types文件copy一份到/opt目录中
[root@localhost conf]# cp nginx.conf /opt/
[root@localhost conf]# cp mime.types /opt/
[root@localhost conf]# cd /opt/
[root@localhost opt]# ls
mime.types nginx.conf
[root@localhost opt]# nginx -t -c /opt/nginx.conf
nginx: the configuration file /opt/nginx.conf syntax is ok
nginx: configuration file /opt/nginx.conf test is successful
// 修改 worker_rlimit_nofile number; 参数为4
#user nobody;
worker_processes 4;
server {
listen 8070;
server_name localhost;
使用nginx服务控制命令重启并指定配置文件路径
[root@localhost opt]# nginx -s stop;nginx -c /opt/nginx.conf
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8070 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]# ps -ef | grep nginx
root 276931 1 0 16:56 ? 00:00:00 nginx: master process nginx -c /opt/nginx.conf
nginx 276932 276931 0 16:56 ? 00:00:00 nginx: worker process
nginx 276933 276931 0 16:56 ? 00:00:00 nginx: worker process
nginx 276934 276931 0 16:56 ? 00:00:00 nginx: worker process
nginx 276935 276931 0 16:56 ? 00:00:00 nginx: worker process
root 283466 276242 0 16:58 pts/2 00:00:00 grep --color=auto nginx
报错 解决方法
[root@localhost nginx-1.12.0]# vim objs/Makefile
CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werrori -g
// 找到 -Werrori 后将其去掉
[root@localhost nginx-1.12.0]# vim src/os/unix/ngx_user.c
#ifdef __GLIBC__
/* work around the glibc bug */
cd.current_salt[0] = ~salt[0];
#endif
// 把cd.current_salt[0] = ~salt[0];前后加上/* cd.current_salt[0] = ~salt[0];*/
错误页面配置
[root@localhost conf]# vim nginx.conf
error_page 404 /404.html;
// 把这一行的注释取消
[root@localhost html]# vim 404.html
<html>
<head>
<title>test</title>
</head>
<body>
<a href="http://www.baidu.com">baidu</a>
</body>
</html>
[root@localhost html]# systemctl restart nginx
平滑升级加echo功能获取现有的程序编译的参数 -V
- 获取新版本的软件包或功能包
- 将新功能或新版本进行编译
- 备份原程序
- 替换原程序
#功能包下载
https://github.com/openresty/echo-nginx-module.git
[root@localhost src]# unzip echo-nginx-module-master.zip
[root@localhost src]# ls
debug echo-nginx-module-master kernels nginx-1.20.1 nginx-1.20.1.tar.gz echo-nginx-module-master.zip
#解压nginx
[root@localhost src]# ls
debug kernels nginx-1.20.1 nginx-1.20.1.tar.gz
[root@localhost src]# rm -rf nginx-1.20.1
[root@localhost src]# tar xf nginx-1.20.1.tar.gz
[root@localhost nginx-1.20.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=../echo-nginx-module-master
[root@localhost nginx-1.20.1]# make
[root@localhost nginx-1.20.1]# ls objs/
addon Makefile nginx.8 ngx_auto_headers.h ngx_modules.o
autoconf.err nginx ngx_auto_config.h ngx_modules.c src
#备份nginx
[root@localhost nginx-1.20.1]# cp /usr/local/nginx/sbin/nginx /opt/
[root@localhost nginx-1.20.1]# nginx -s stop;objs/nginx -c /usr/local/nginx/conf/nginx.conf
[root@localhost nginx-1.20.1]# ps -ef |grep nginx
root 5652 1 0 22:24 ? 00:00:00 nginx: master process objs/nginx -c /usr/local/nginx/conf/nginx.conf
nginx 5653 5652 0 22:24 ? 00:00:00 nginx: worker process
nginx 5654 5652 0 22:24 ? 00:00:00 nginx: worker process
nginx 5655 5652 0 22:24 ? 00:00:00 nginx: worker process
root 5657 1623 0 22:24 pts/1 00:00:00 grep --color=auto nginx
[root@localhost conf]# vim nginx.conf
location /test {
echo "test";
}
[root@localhost nginx-1.20.1]# objs/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@localhost nginx-1.20.1]# objs/nginx -s reload
#重启后在命令行查看
[root@localhost src]# cp nginx-1.20.1/objs/nginx /usr/local/nginx/sbin/
cp:是否覆盖'/usr/local/nginx/sbin/nginx'? y
localtion配置
通过指定模式来与客户端请求的URI相匹配 功能:允许根据用户请求的URI来匹配定义的各location,匹配到时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能 语法:location [ 修饰符 ] pattern {…} 常用修饰符:
修饰符 | 功能 |
---|
= | 精确匹配 | ~ | 正则表达式模式匹配,区分大小写 | ~* | 正则表达式模式匹配,不区分大小写 | ^~ | 前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式 | @ | 定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等 |
location /test {
echo "test";
}
[root@localhost ~]# curl 192.168.111.141/test
test
[root@localhost ~]# curl 192.168.111.141/test/
test
[root@localhost ~]# curl 192.168.111.141/testabc
test
= : 表示必须与指定模式精确匹配
location = /testa {
echo "test2";
}
[root@localhost ~]# curl 192.168.111.141/testa
test2
[root@localhost ~]# curl 192.168.111.141/testa?abc
test2
~:表示指定的正则表达式要区分大小写,如:
abc
location ~ ^/abc$ {
echo "abc";
}
[root@localhost ~]# curl 192.168.111.141/abc
abc
[root@localhost ~]# curl 192.168.111.141/abc?abc
abc
~*:表示指定的正则表达式不区分大小写,如:
location ~* ^/abc$ {
echo "abc";
}
[root@localhost ~]# curl 192.168.8.137/abc
abc
[root@localhost ~]# curl 192.168.8.137/ABC
abc
[root@localhost ~]# curl 192.168.8.137/abc?ABC
abc
查找顺序和优先级:
- 带有=的精确匹配优先
- 正则表达式按照他们在配置文件中定义的顺序
- 带有^~修饰符的,开头匹配
- 带有或*修饰符的,如果正则表达式与URI匹配
- 没有修饰符的精确匹配
优先级次序如下:
( location = 路径 ) --> ( location ^~ 路径 ) --> ( location ~ 正则 ) --> ( location ~* 正则 ) --> ( location 路径 )
访问配置
用于location段
allow:设定允许哪台或哪些主机访问,多个参数间用多个allow deny:设定禁止哪台或哪些主机访问,多个参数间用多个deny
#拒绝本机访问
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
deny 192.168.111.1;
root html;
index index.html index.htm;
}
#允许本机访问,其他全部拒绝
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
allow 192.168.111.1;
deny all;
root html;
index index.html index.htm;
}
[root@localhost ~]# curl 192.168.111.141
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
用户认证
auth_basic "欢迎信息";
auth_basic_user_file "/path/to/user_auth_file"
user_auth_file内容格式为:
username:password
这里的密码为加密后的密码串,建议用htpasswd来创建此文件:
[root@localhost ~]# yum -y install httpd-tools
[root@localhost ~]# htpasswd -c -m /usr/local/nginx/conf/.pass admin
New password:
Re-type new password:
Adding password for user admin
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
auth_basic "Welcome";
auth_basic_user_file "conf/.pass";
root html;
index index.html index.htm;
}
//命令行访问 -u 指定用户名和密码
[root@localhost html]# curl -u admin:admin http://192.168.111.141
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost html]#
https配置
生成证书
#生成一对密钥
[root@localhost ~]# mkdir -p /etc/pki/CA
[root@localhost ~]# cd /etc/pki/CA/
[root@localhost CA]# mkdir private
[root@localhost CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus (2 primes)
........................+++++
........................................................................+++++
e is 65537 (0x010001)
#生成自签署证书
[root@localhost CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HB
Locality Name (eg, city) [Default City]:WH
Organization Name (eg, company) [Default Company Ltd]:runtime
Organizational Unit Name (eg, section) []:runtime
Common Name (eg, your name or your server's hostname) []:test.neawalke.com
Email Address []:1@2.com
[root@localhost CA]# mkdir certs newcerts crl
[root@localhost CA]# touch index.txt && echo 01 > serial
#创建证书存放位置
[root@localhost CA]# mkdir /usr/local/nginx/conf/ssl
#生成密钥
[root@localhost ~]# cd /usr/local/nginx/conf/ssl/
[root@localhost ssl]# (umask 077;openssl genrsa -out nginx.key 2048)
Generating RSA private key, 2048 bit long modulus (2 primes)
......+++++
..............+++++
e is 65537 (0x010001)
#生成证书签署请求
[root@localhost ssl]# openssl req -new -key nginx.key -days 365 -out nginx.csr
Ignoring -days; not generating a certificate
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HB
Locality Name (eg, city) [Default City]:WH
Organization Name (eg, company) [Default Company Ltd]:runtime
Organizational Unit Name (eg, section) []:runtime
Common Name (eg, your name or your server's hostname) []:test.neawalke.com
Email Address []:1@2.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
#修改nginx配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 443 ssl;
server_name test.neawalke.com;
ssl_certificate ssl/nginx.crt;
ssl_certificate_key ssl/nginx.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
#CA签署客户端提交上来的证书
[root@localhost ssl]# openssl ca -in nginx.csr -out nginx.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Oct 27 15:46:45 2021 GMT
Not After : Oct 27 15:46:45 2022 GMT
Subject:
countryName = CN
stateOrProvinceName = HB
organizationName = runtime
organizationalUnitName = runtime
commonName = test.neawalke.com
emailAddress = 1@2.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
DB:C0:67:A9:96:4F:D8:67:60:8D:C0:6E:E7:B9:96:A9:70:7A:0E:62
X509v3 Authority Key Identifier:
keyid:E6:F5:AE:F8:57:F4:37:2F:EE:29:36:75:E9:CB:0E:45:FE:80:8A:72
Certificate is to be certified until Oct 27 15:46:45 2022 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
#当需两台服务器实现https,需做如下操作
//客户端把证书签署请求文件发送给CA
scp httpd.csr root@CA端IP:/root
//CA把签署好的证书httpd.crt发给客户端
scp httpd.crt root@客户端IP:/etc/httpd/ssl/
|