配置文件
配置文件
开局一张图:
st=>start: nginx -t
en=>end: 启动完成
1=>operation: 默认读取/etc/nginx.conf
2=>operation: 读取 include /etc/nginx/conf.d/*.conf
st->1->2
2->en
默认配置
/etc/nginx/nginx.conf
默认配置文件/etc/nginx/nginx.conf.default
模板文件
路由匹配优先级
- 最高级
"="
完全匹配 - 次高级
"^~"
开头匹配 - 中等级
"~"
/xxx/ 正则 - 最低级
"/"
匹配任何
完整模板 (nginx/1.20.1)
- yum安装的默认 nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx; # 设置运行用户和运行组,用空格分割
worker_processes auto; #允许生成的进程数,默认为1
error_log /var/log/nginx/error.log; # 错误日志的存放路径
pid /run/nginx.pid; # 存储进程pid文件的路径
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf; # 需要引入的动态配置文件
# events块 配置影响服务器与用户的网络连接
# 每个进程的最大链接
# 选取哪种事件驱动模型处理连接请求
# 是否允许同时接受多个网路连接
# 开启多个网络连接序列化
events {
worker_connections 1024;
}
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; #sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载.注意:如果图片显示不正常把这个改成off.
tcp_nopush on; # 防止网络阻塞
tcp_nodelay on; # 防止网络阻塞
keepalive_timeout 65; # (单位s)设置客户端连接保持活动的超时时间,在超过这个时间后服务器会关闭该链接
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;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# 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 HIGH:!aNULL:!MD5;
# 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 {
# }
# }
}
关键字详解
全局
关键字 | 说明 |
---|---|
user | |
worker_processes | nginx的工作进程数,默认auto 根据服务器的cpu来定 |
error_log | 错误日志 |
pid |
events {../}
更多操作关键字 | 说明 | 参数 |
---|---|---|
worker_connections 1024 | 最大连接数 | |
client_header_buffer_size | ||
accept_mutex on/off | 惊群现象 | |
multi_accept on/off | ||
use epoll | 事件驱动模型 |
http {../}
关键字 | 说明 | 用例 |
---|---|---|
log_format | 设置日志的记录格式 | 语法 |
access_log {path} {level} | 访问日志 |
- log_format语法
server {../}
关键字 | 说明 | 用例 |
---|---|---|
sendfile | ||
tcp_nopush | ||
local |
其他配置
gzip
gzip on;
# 开启, 真实压缩
gzip_static on;
gzip_min_length 1k;
gzip_buffers 4 16k;
#gzip_http_version 1.0;
gzip_comp_level 5;
# application/javascript - 必须添加 压缩js
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
负载均衡
http {
upstream xxx { //负载均衡 默认随机分配
server 192.168.0.1:11 //设置变量
}
server {
location * {//转发规则
proxy_pass http://xx/; //反向代理 xx 可以使用变量
}
}
}