Nginx常用模块及配置(一)

专门记录一份笔记,来记录Nginx常用的一些模块和功能。

一、安装及规划

1.1 安装

安装不再赘述。

CentOS7安装Nginx

1.2 规划

属性 注释
系统 centos7
版本 nginx 1.26.1
主机名 nginx-server
ip 192.168.10.18
虚拟主机001 nginx001.bravexist.cn
虚拟主机002 nginx002.bravexist.cn
虚拟主机003 nginx003.bravexist.cn
虚拟主机005 nginx005.bravexist.cn
虚拟主机006 nginx006.bravexist.cn
虚拟主机007 nginx007.bravexist.cn
1
2
3
4
5
6
7
8
cat >> /etc/hosts <<EOF
127.0.0.1 nginx001.bravexist.cn
127.0.0.1 nginx002.bravexist.cn
127.0.0.1 nginx003.bravexist.cn
127.0.0.1 nginx005.bravexist.cn
127.0.0.1 nginx006.bravexist.cn
127.0.0.1 nginx007.bravexist.cn
EOF
1
2
3
4
5
6
192.168.10.18    nginx001.bravexist.cn
192.168.10.18 nginx002.bravexist.cn
192.168.10.18 nginx003.bravexist.cn
192.168.10.18 nginx005.bravexist.cn
192.168.10.18 nginx006.bravexist.cn
192.168.10.18 nginx007.bravexist.cn

二、配置解析

配置分为不同的模块,模块开源嵌套,主要模块是,httpserverlocation,依次嵌套。

主要包括两部分配置,

  • 主配置文件 /etc/nginx/nginx.conf
  • 子配置文件 /etc/nginx/nginx/conf.d/*.conf

主配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
user  nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
  • user :定义工作进程的启动用户
  • worker_processes:定义工作进程数
  • error_log:全局错误日志文件的路径及记录级别
  • pid:存储 Nginx 主进程 ID (PID) 的文件路径,用于进程管理(如重载、停止)
  • events:事件模块块,用于设定网络连接的处理机制
  • worker_connections:每个 worker 进程允许的最大并发连接数。理论最大并发 = 进程数 × 连接数
  • http:HTTP 核心模块块,包含所有处理 Web 流量的配置
  • include:引入外部配置文件。这里引入了 MIME 类型定义文件,告诉浏览器如何处理不同后缀的文件
  • default_type:当文件类型未定义时,默认使用的 MIME 类型
  • log_format:定义日志的格式,可以定义多个,变量可通过官方文档查询。
  • access_log:访问日志的路径及使用的格式(main 格式在 log_format 中定义)
  • sendfile: 开启高效文件传输模式(零拷贝),直接在内核空间传输数据,极大提高静态文件性能
  • keepalive_timeout:长连接超时时间(秒)。在时间内复用 TCP 连接,减少握手开销

子配置文件

1
2
3
4
5
6
7
8
9
10
11
12
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
  • server :定义一个虚拟主机块。一个 Nginx 实例可以配置多个 server
  • listen:指定监听的端口(如 80, 443)和 IP 地址
  • server_name:域名匹配规则。当请求头中的 Host 与此匹配时,使用该 server 块处理
  • root:站点的根目录路径。请求的文件将从这个目录下查找
  • index:默认首页文件。当访问目录时(如 /),按顺序查找这些文件并返回
  • location:URL 路由匹配块。根据请求的 URI 路径进行不同的处理(如代理、返回静态文件等)

三、常用模块及功能

3.1 location

3.1.1 基础功能

URI 进行匹配,进入不同的处理逻辑。

URI 是路径,从 / 开始,不包括域名。

新增一个 admin 的匹配规则

1
vim /etc/nginx/conf.d/nginx001.conf
1
2
3
4
5
6
7
8
9
10
11
server {
listen 80;
server_name nginx001.bravexist.cn;
root /www/wwwroot/nginx001;
location /{
index index.html;
}
location /admin/ {
index index.html;
}
}
1
2
3
4
5
6
DIRNAME=/www/wwwroot/nginx001
SITENAME="nginx001.bravexist.cn"
mkdir -pv ${DIRNAME}/admin

echo "${SITENAME} index" > ${DIRNAME}/index.html
echo "${SITENAME} admin" > ${DIRNAME}/admin/index.html
1
curl http://127.0.0.1  -H Host:nginx001.bravexist.cn
1
curl http://127.0.0.1/admin/  -H Host:nginx001.bravexist.cn
1
curl http://127.0.0.1/admin  -H Host:nginx001.bravexist.cn -v
1
curl http://127.0.0.1/admin  -H Host:nginx001.bravexist.cn -Lv
  • -L 跟随重定向
  • -v 显示详细日志

3.1.2 正则匹配

~* 不区分大小写的正则匹配

~ 区分大小写的正则匹配

3.1.3 其它匹配规则

= 精确匹配

@自定义名字

^~ 非正则前缀匹配

3.2 allow、deny

allow 模块

deny 模块

1
vim /etc/nginx/conf.d/nginx002.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 80;
server_name nginx002.bravexist.cn;
root /www/wwwroot/nginx002;
location /{
index index.html;
}
location /admin/ {
allow 127.0.0.1;
deny 192.168.10.1;
index index.html;
}
}
1
2
3
4
5
6
DIRNAME=/www/wwwroot/nginx002
SITENAME="nginx002.bravexist.cn"
mkdir -pv ${DIRNAME}/admin

echo "${SITENAME} index" > ${DIRNAME}/index.html
echo "${SITENAME} admin" > ${DIRNAME}/admin/index.html
1
curl http://127.0.0.1/admin/  -H Host:nginx002.bravexist.cn
1
curl http://192.168.10.18/admin/  -H Host:nginx002.bravexist.cn

3.3 缓存 expires

expires 模块

1
vi /etc/nginx/conf.d/nginx003.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 80;
server_name nginx003.bravexist.cn;
root /www/wwwroot/nginx003;
location /{
index index.html;
}
location /admin/ {
index index.html;
}
#location ~* \.html$ {
# expires 2h;
#}
#location ~ \.HTML$ {
# expires 1d;
#}
}
1
2
3
4
5
6
7
DIRNAME=/www/wwwroot/nginx003
SITENAME="nginx003.bravexist.cn"
mkdir -pv ${DIRNAME}/admin

echo "${SITENAME} index" > ${DIRNAME}/index.html
echo "${SITENAME} admin" > ${DIRNAME}/admin/index.html
echo "${SITENAME} admin HTML" > ${DIRNAME}/admin/index.HTML

添加 hosts 解析后,使用浏览器F12,开启响应头,缓存控制,验证。

  • 都不开,测一次

  • 打开 ~ 测一次

  • 打开 ~* 测一此

1
http://nginx003.bravexist.cn/admin/index.HTML
1
http://nginx003.bravexist.cn/admin/index.html

3.4 日志模块

只需要分为两种:

3.5 自动索引

1
vi /etc/nginx/conf.d/nginx005.conf
1
2
3
4
5
6
7
8
server {
listen 80;
server_name nginx005.bravexist.cn;
root /www/wwwroot/nginx005;
autoindex on;
# autoindex_localtime on;
# autoindex_exact_size off;
}
1
2
3
4
5
6
7
8
DIRNAME=/www/wwwroot/nginx005
SITENAME="nginx005.bravexist.cn"
mkdir -pv ${DIRNAME}/{2020..2026}/{01..12}

touch ${DIRNAME}/{2020..2026}/{01..12}/0kb.txt
seq 10000 > ${DIRNAME}/10000.txt
seq 100000 > ${DIRNAME}/100000.txt
seq 1000000 > ${DIRNAME}/1000000.txt
1
nginx -t && nginx -s reload

添加 hosts 解析后,使用浏览器访问

1
http://nginx005.bravexist.cn

3.6 基础认证模块

auth_basic 模块

1
vi /etc/nginx/conf.d/nginx006.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 80;
server_name nginx006.bravexist.cn;
root /www/wwwroot/nginx006;
location /{
index index.html;
}
location /admin/ {
auth_basic "测试语句,不会生效";
auth_basic_user_file /etc/nginx/user;
index index.html;
}
}
1
2
3
4
5
6
DIRNAME=/www/wwwroot/nginx006
SITENAME="nginx006.bravexist.cn"
mkdir -pv ${DIRNAME}/admin

echo "${SITENAME} index" > ${DIRNAME}/index.html
echo "${SITENAME} admin" > ${DIRNAME}/admin/index.html
1
yum install httpd-tools

创建用户,覆盖写入文件

1
htpasswd -bc /etc/nginx/user admin admin

创建用户,追加写入文件

1
htpasswd -b /etc/nginx/user admin1 admin1

修改权限(可选)

1
chmod 600 /etc/nginx/user
1
nginx -t && nginx -s reload

添加 hosts 解析后,使用浏览器访问

1
http://nginx006.bravexist.cn
1
http://nginx006.bravexist.cn/admin

3.7 统计功能

stub_status 模块

1
vi /etc/nginx/conf.d/nginx007.conf
1
2
3
4
5
6
7
8
9
10
11
server {
listen 80;
server_name nginx007.bravexist.cn;
root /www/wwwroot/nginx007;
location /{
index index.html;
}
location /status {
stub_status;
}
}
1
2
3
4
5
DIRNAME=/www/wwwroot/nginx007
SITENAME="nginx007.bravexist.cn"
mkdir -pv ${DIRNAME}/admin

echo "${SITENAME} index" > ${DIRNAME}/index.html
1
nginx -t && nginx -s reload

添加 hosts 解析后,使用浏览器访问

1
http://nginx007.bravexist.cn/status
1
2
3
4
Active connections: 1 
server accepts handled requests
39 39 108
Reading: 0 Writing: 1 Waiting: 0
  • Active connections:当前 Nginx 正在处理 的活动连接数
  • accepts:总共接收的连接数
  • handled:总共处理的连接数
  • requests:总共处理的请求数
  • Reading:正在读取客户端的请求头
  • Writing:正在处理请求,或者正在把响应数据写回给客户端
  • Waiting:已经处理完请求,正在等待客户端发送下一个请求

四、压测

abApache 的一款压测工具。

1
ab -n 999999 -c 3 -H nginx007:video.bravexist.cn http://127.0.0.1/
  • -n 总请求数量
  • -c 并发数

五、动态网站架构LNMP

有些网站需要靠服务器端计算每次响应的页面,这就是动态网站。一般由后端的 PHP、Java、GoLang、Python等编写。不过实际情况会是混合网站,既有动态资源,也有静态资源。

中文wordpress

主要使用 LNMP 部署一个 WordPress

  • Nginx 1.26.1
  • WordPress 6.9
  • MariaDB 10.11(LTS)
  • PHP8.3

5.1 常见的几种架构

名称 组件 开发语言
LNMP(LEMP) Linux + Nginx + MySQL + PHP PHP
LAMP Linux + Apache + MySQL + PHP PHP
WNMP Windows + Nginx + MySQL + PHP PHP
WAMP Windows + Apache + MySQL + PHP PHP
LNMT Linux + Nginx + MySQL + Tomcat Java
LNMP Linux + Nginx + MySQL + Python Python
LNMG Linux + Nginx + MySQL + GoLang GoLang
  • LNMP 又叫 LEMP,因为 Nginx 之前叫 Engine X
  • M 有时候也代表 MySQL 的开源版 MariaDB

LNMP(LEMP)主要原理是 Nginx 收到动态请求后,通过 fastcgi 协议转发给后端的 php-fpm 来处理,之后获取响应后发送给客户端。

5.2 下载

使用 MariaDB 是因为没用过,对 MySQL8.0 的安装已经足够熟悉了。

WordPress下载Wordpress中文版下载

5.2.1 MariaDB

官方yum源生成

  1. 新建 MariaDB的yum源
1
vi /etc/yum.repos.d/MariaDB.repo
1
2
3
4
5
6
7
8
9
10
11
# MariaDB 10.11 RedHatEnterpriseLinux repository list - created 2026-01-08 01:55 UTC
# https://mariadb.org/download/
[mariadb]
name = MariaDB
# rpm.mariadb.org is a dynamic mirror if your preferred mirror goes offline. See https://mariadb.org/mirrorbits/ for details.
# baseurl = https://rpm.mariadb.org/10.11/rhel/$releasever/$basearch
baseurl = https://mirrors.xtom.com/mariadb/yum/10.11/rhel/$releasever/$basearch
module_hotfixes = 1
# gpgkey = https://rpm.mariadb.org/RPM-GPG-KEY-MariaDB
gpgkey = https://mirrors.xtom.com/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck = 1
  1. 替换URL
1
http://yum.mariadb.org
1
2
https://mirrors.aliyun.com/mariadb/yum
https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum
  1. 安装
1
sudo yum install MariaDB-server MariaDB-client
  1. 启动并开机自启
1
systemctl enable --now mariadb
  1. 运行安全脚本,都默认选项即可。
1
mariadb-secure-installation
  • Enter current password for root (enter for none): (直接回车,因为刚装好没密码)
  • Switch to unix_socket authentication [Y/n] (输入 Y)
  • Change the root password? [Y/n] (输入 Y,然后设置你的新 Root 密码)
  • Remove anonymous users? [Y/n] (输入 Y,移除匿名用户)
  • Disallow root login remotely? [Y/n] (输入 Y,建议禁止 Root 远程登录,后面我们会创建单独用户)
  • Remove test database and access to it? [Y/n] (输入 Y,移除测试库)
  • Reload privilege tables now? [Y/n] (输入 Y,刷新权限)
  1. 修改编码集
1
vi /etc/my.cnf.d/server.cnf
1
2
3
4
5
6
7
8
9
10
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

# 让新建库、新建表默认继承
init_connect='SET NAMES utf8mb4'
skip-character-set-client-handshake

# 可选,但强烈建议
max_connections = 500
1
vi /etc/my.cnf.d/client.cnf
1
2
[client]
default-character-set = utf8mb4
1
vi /etc/my.cnf.d/mysql-clients.cnf
1
2
[mysql]
default-character-set = utf8mb4
1
systemctl restart mysqld
1
mysql -uroot -p'Root21..'
1
2
SHOW VARIABLES LIKE 'character_set%';
SHOW VARIABLES LIKE 'collation%';

5.2.2 PHP

  1. 安装Remi 仓库
1
yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
  1. 确认仓库存在,查看php可用版本
1
ls /etc/yum.repos.d/ | grep remi
  1. 启用 php8.3
1
yum install -y yum-utils
1
yum-config-manager --enable remi-php83
  1. 安装PHP8.3及常用扩展
1
2
3
4
yum install -y \
php php-fpm php-cli php-common \
php-mysqlnd php-opcache \
php-gd php-mbstring php-xml php-json php-bcmath php-intl php-zip php-curl
  1. 验证
1
php -v
  1. 禁用无关的库
1
2
3
4
5
6
7
8
9
10
11
yum-config-manager --disable remi-php54
yum-config-manager --disable remi-php55
yum-config-manager --disable remi-php56
yum-config-manager --disable remi-php70
yum-config-manager --disable remi-php71
yum-config-manager --disable remi-php72
yum-config-manager --disable remi-php73
yum-config-manager --disable remi-php74
yum-config-manager --disable remi-php80
yum-config-manager --disable remi-php81
yum-config-manager --disable remi-php82

5.3 初始化

5.3.1 MariaDB

  1. 登录
1
mysql -uroot -p'Root21..'
  1. 创建数据库
1
2
3
CREATE DATABASE wordpress
DEFAULT CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
  1. 创建普通用户
1
2
CREATE USER 'wpuser'@'localhost'
IDENTIFIED BY 'Wpuser21..';
  1. 授权
1
2
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
  1. 验证授权
1
SHOW GRANTS FOR 'wpuser'@'localhost';

5.3.2 Nginx

1
vim /etc/nginx/conf.d/wordpress.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
server {
listen 80;
server_name wp.bravexist.cn;
root /www/wwwroot/wordpress;
index index.php index.html;

access_log /var/log/nginx/wordpress.access.log;
error_log /var/log/nginx/wordpress.error.log;

location / {
try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

location ~* \.(jpg|jpeg|gif|png|css|js|ico|svg|woff|woff2|ttf|eot)$ {
expires 30d;
access_log off;
}

location ~ /\. {
deny all;
}
}

5.3.3 PHP-FPM

  1. PHP-FPM 运行用户和 Nginx 一致
1
vi /etc/php-fpm.d/www.conf

​ 关键配置

1
2
3
4
5
6
7
user = nginx
group = nginx

listen = /run/php-fpm/www.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
  1. PHP 基础参数
1
vi /etc/php.ini
1
2
3
4
5
memory_limit = 256M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
date.timezone = Asia/Shanghai
  1. 重启 php-fpm
1
2
systemctl restart php-fpm
systemctl status php-fpm

5.3.4 Wordpress

1
wget https://wordpress.org/wordpress-6.9.tar.gz

或 中文包

1
wget https://cn.wordpress.org/wordpress-6.9-zh_CN.tar.gz
1
mkdir -pv /www/wwwroot
1
tar xf wordpress-6.9.tar.gz -C /www/wwwroot
1
chown -R nginx.nginx /www/wwwroot/wordpress

5.4 启动

1
systemctl start nginx
1
nginx -t && nginx -s reload

设置 hosts 解析后访问

1
http://wp.bravexist.cn