Nginx 配置

目录

/etc/nginx nginx配置文件目录

/etc/nginx/nginx.conf nginx的主配置文件

/etc/nginx/conf.d/ nginx的子配置文件的目录

/etc/ logrotate.d/nginx 日志切割

/usr/lib/systemd/system/nginx.service nginx的systemctl配置文件

/usr/sbin/nginx nginx管理命令

/usr/share/nginx/html nginx默认的站点目录,网站的根目录

/var/log/nginx nginx日志文件的目录

配置文件

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#================================ 核心配置区 ===============================
user nginx; # 指定nginx所属于用户的通知用户
worker_processes auto; # 用于设置进程数量,处理用户请求
error_log /var/log/nginx/error.log; # Nginx模块日志及位置
pid /run/nginx.pid; # pid文件
# worker_rlimit_nofile number # 子进程可以打开的最大句柄数
# worker_rlimit_core size # 设置异常文件大小
# working_directory # 存储core文件的路径
# worker_cpu_affinity # 将子进程与CPU核心绑定
# worker_cpu_affinity 0001 0010 0100 1000 # 将子进程与核心绑定
# worker_priority number # 调整子进程优先级
# worker_shutdown_timeout time # 调整优雅退出超时时间
# timer_resolution interval # 调整向内核获取时间戳的
周期,周期越小,系统调用开销越大,周期越大,日志时间精度越低
# daemon on | off # 设置守护进程
#===========================================================================
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf; # 额外的配置文件
#================================= EVENTS区 ================================
events {
worker_connections 1024; # worker进程的最大连接数
# use # 设置nginx的IO模型
# accept_mutex # 互斥锁,开启后会先尝试请求锁,才能接收请求
# accept_mutex_delay time # 子进程等待时间
# lock_file file # 锁文件存储路径
# multi_accept on|off # 一个子进程可以接收多个请求
}
#===========================================================================
#================================== HTTP 区 ================================
http {
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; # 直接把文件从磁盘发送到套接字
tcp_nopush on; # 等待TCP报文到一定大小后发出。
tcp_nodelay on; # 不等待最大TCP报文长,直接发送
keepalive_timeout 65; # 长连接超时时间
types_hash_max_size 4096;

include /etc/nginx/mime.types; # 文件包含,把另一个文件中的内容添加过来 / 媒体类型
default_type application/octet-stream; #

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See <http://nginx.org/en/docs/ngx_core_module.html#include>
# for more information.
include /etc/nginx/conf.d/*.conf; # 把conf.d 中的子配置文件包含进来

server { # 网站配置
listen 80; # 监听端口
listen [::]:80; # 监听端口(IPv6)
server_name localhost; # 域名
# 域名匹配优先级 精确匹配>左侧匹配>右侧匹配>正则匹配
client_max_body_size 1024M; # 限制客户端请求体(request body)最大大小

root /usr/share/nginx/html; # 网站根目录
index index.html

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf; #
location / {
# location 匹配规则
# = 精确匹配,必须和声明路径完全一致
# ~ 区分大小写的正则表达式匹配
# ~* 不区分大小写的正则表达式匹配
# ^~ 匹配到即停止搜索
# 匹配优先级 = > ^~ > ~ > ~* > 未配置
# 路径声明为 /something 会先查询是否存在something文件,再查询
# 是否存在/something 目录中存在index.html 作为返回
# 声明为 /something/ 只会作为目录进行查询是否存在index.html文件
index index.html ;
# root 定义了访问的根目录,查询从根目录开始
# alias 定义为/的别名,即访问的内容都是包含alias的路径
# 比如 /something alias /opt/something 请求/something/1.jpg
# 会直接替换为/opt/something/1.jpg 意思为声明的“/”的别名是alias声明的路径
# stub_status ;
# 数据统计
# Active Connections 活动的连接数
# accepts 接受的客户端总量
# handled 处理的客户端总量
# requests 客户端的总请求数量
# Reading 读取客户端请求的数量
# Writing 响应数据到客户端的总数量
# Waiting 空闲客户端请求连接数量
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
===============================================================================
==================================== HTTPS 区 =================================
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
=================================================================================
}

Nginx 虚拟主机

虚拟主机类型

基于域名的虚拟主机 不同的域名访问不同的虚拟主机 常用

基于端口的虚拟主机 不同端口访问不同的虚拟主机 给网站增加特殊端口,限制用户访问

基于ip的虚拟主机 不同的ip地址访问不同的虚拟主机 用来增强限制,限制用户只能通过内网访问,或vpn访问

基于域名的虚拟主机

配置子系统配置 game.codfish.cn.conf

1
2
3
4
5
6
7
8
server {
listen 80 ;
server_name game.codfish.cn;
root /app/code/game;
location / {
index index.html;
}
}
1
2
3
4
5
6
7
8
mkdir -p /app/code/{game,live,www}

for name in game live www
do
echo $name.codfish.cn > /app/code/$name/index.html
done

head /app/code/{game,live,www}/index.html

基于端口的虚拟主机

1
2
3
4
5
6
7
8
server {
listen 81 ; # 定义特殊端口
server_name game.codfish.cn;
root /app/code/game;
location / {
index index.html;
}
}

基于IP的虚拟主机

1
2
3
4
5
6
7
8
server {
listen localhost:81 ; # 定义特殊端口
server_name game.codfish.cn;
root /app/code/game;
location / {
index index.html;
}
}

异常访问

当用户访问不存在的域名或者使用IP访问网络时,Nginx 会按字母顺序选择 最匹配的server 进行响应

所以 当域名不存在/或IP访问时 匹配字母靠前的端口一致的 server 进行响应。

或者可以在server定义中 定义一个 默认的server来进行处理

1
2
3
4
5
6
7
8
server {
listen 80 default_server ;
server_name some_url ;
root /app/code/codfish ;
location / {
index index.html;
}
}

或者定义一个不存在的域名来处于异常访问

1
2
3
4
5
6
7
server {
listen 80 default_server ;
server_name _ ;
charset utf8 ;
default_type text/plain;
return 200 "message";
}

Nginx 日志

log_format main “$remote_addr - $remote_user [$time_local] “ $request” ‘

1
2
3
‘$status  $body_bytes_sent “$http_referer” ’

‘”http_user_agent” “$http_x_forwarded_for” ’;

$remote_addr 客户端ip地址

$remote_user 用户名

$time_local 时间

$request 请求报文的起始行

$status 状态码

$body_bytes_sent 响应消息体长度

$http_referer 间接进行本次请求的域名

$http_user_agent 用户使用的访问代理

$http_x_forwarded_for 使用代理用于记录用户真实的ip地址

access_log

语法 access_log 地址/文件名 格式

access_log path [format [buffer=”size”]] [gzip[=”level”]][flush=”time”][if=”condition”]】‘

buffer 缓存用户访问日志

gzip 日志是否要压缩

flush 多久向磁盘中写入

error_log

语法 error_log 地址/文件名 级别

error_log path level

日志切割

1 系统定时任务 + logroate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# cat /etc/logrotate.d/nginx
/var/log/nginx/*.log{
create 0640 nginx root # 切割后的文件所有者权限信息
daily # 每天切割
rotate 10 # 保留10次
missingok # 日志不存在不切割
notifempty # 日志为空 不切割
compress # 切割后的日志要进行压缩
delaycompress # 延迟压缩
sharedscripts # 开启切割后或切割前 执行脚本/命令功能
postrotate # 切割后执行的命令
/bin/kill -USR1 'cat /run/nginx.pid 2>/dev/null' 2>/dev/null || true
endscript
}

2 自己实现日志切割

实现上述配置文件,使用logratate (-f) 进行文件切割


Nginx 配置
http://gadoid.io/2025/05/18/Nginx-配置/
作者
Codfish
发布于
2025年5月18日
许可协议