nginx.conf文件详解
1. nginx.conf文件结构
1、全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。
5、location块:配置请求的路由,以及各种页面的处理情况
2. 各模块解析
2.1 main 全局配置
[root@nginx-kafka01 nginx]# vim nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
# 设置worker进程的用户,指定linux中的用户,会涉及到linux操作目录和
# 文件的一些权限,默认为nobody
user nginx;
# worker进程工作数设置,一般来说CPU有几个,就设置几个
worker_processes 4;
# nginx日志级别debug | info | notice | warn | error | crit | alert | emerg,错误级别从左到右越来越大
error_log /var/log/nginx/error.log;
# 设置nginx进程pid
pid /run/nginx.pid;
# include引入外部配置,提高可读性 避免单个配置文件过大
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
2.2 event 配置工作模式及最大连接数
# 设置工作模式
events {
# 每个worker允许连接的最大客户端连接数
worker_connections 1024;
}
2.3 http模块内解析
# http指令快,针对http网络传输的一些指令设置
# 比如:日志格式、日志存放位置等
http {
# 设置日志格式,main为定义的格式名称,access_log可以直接使用这个变量
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使用高效文件传输,提升传输性能。启用后才能使用tcp_nopush,
# 是指当数据表累积一定大小后才发送,提高了效率。
sendfile on;
tcp_nopush on;
tcp_nodelay on;
# keepalive_timeout设置客户端与服务端请求的超时时间,
# 保证客户端多次请求的时候不会重复建立新的连接,节约资源损耗。
keepalive_timeout 65;
types_hash_max_size 4096;
# include引入外部配置,提高可读性 避免单个配置文件过大
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引入外部配置,提高可读性 避免单个配置文件过大
include /etc/nginx/conf.d/*.conf;
# server可以在http指令块中设置多个虚拟主机
# listen 监听端口
# server_name localhost、ip、域名
# location 请求路由映射 匹配拦截
# root 请求位置
# index 首页设置
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.
#
# 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 {
# }
# }
}
3. /etc/nginx/conf.d/*.conf和/etc/nginx/nginx.conf
有时候我们安装了nginx后发现配置文件只有一个/etc/nginx/nginx.conf,
所有的配置包括虚拟目录也在此文件中配置, 这样当虚拟主机多了管理就有些不方便了,这时候就需要我们把配置文件拆分开来,在/etc/nginx/conf.d/ 文件夹下建立对应的域名配置文件,比如 /etc/nginx/conf.d/sc.conf文件
3.1 如何配置
# 1、在原文件下的http模块添加:
include /etc/nginx/conf.d/*.conf;
# 2、 创建/etc/nginx/conf.d/sc.com文件
内容:
[root@nginx-kafka01 conf.d]# cat sc.conf
# 虚拟主机配置文件
server {
# 默认监听端口
listen 80 default_server;
# 服务域名
server_name www.sc.com;
# 服务访问的页面内容
root /usr/share/nginx/html;
# 服务日治文件存放位置及存放格式为main
access_log /var/log/nginx/sc/access.log main;
location / {
}
}
#################
server {
listen 8080;
server_name www.sc2.com;
root /usr/share/nginx/html;
access_log /var/log/nginx/sc/access.log main;
location / {
}
}
[root@nginx-kafka01 conf.d]#
3.2 一键安装nginx脚本
#!/bin/bash
#install nginx
#version:1.21.6
#!/bin/bash
#install nginx
#version:1.21.6
#author: cali
#mail: 2744708078@qq.com
#company: aaaa
#time: 2022-06-10
#create user aaa
useradd -s /sbin/nologin xieshan
#download nginx
mkdir -p /lianxi/xieshan
cd /lianxi/xieshan
curl -O https://nginx.org/download/nginx-1.21.6.tar.gz
#uncompress nginx source
tar xf nginx-1.21.6.tar.gz
cd nginx-1.21.6
#resolution dependency
yum install pcre2 pcre2-devel zlib zlib-devel openssl openssl-devel -y
#configure
./configure --prefix=/usr/local/xieshan --user=xieshan --with-threads --with-http_ssl_module --with-http_v2_module
#make
make -j 2
#install
make install
#path variable
PATH=/usr/local/xieshan/sbin/:$PATH
echo 'PATH=/usr/local/xieshan/sbin/:$PATH' >>/root/.bashrc
#start nginx
if pidof nginx &>/dev/null ;then
echo "nginx is running"
#kill old nginx process
killall -9 nginx
#start new nginx process
nginx
else
nginx
fi