nginx部署与主配置文件
软件包部署方式
安装方式 | 优点 | 缺点 |
---|
rpm安装 | 简单、较快 | 需要找rpm包,解决依赖较困难,无法定制化 |
yum安装 | 简单、易用、高效;自动解决依赖关系 | 不能选择软件版本或软件存放路径,无法定制化 |
编译安装 | 可自定义安装 | 较慢,安装复杂,需要gcc编译器环境 |
综合安装 | 将源码制作成rpm包,放入本地yum仓库,实现yum自动安装 | 需要提前制作,制作复杂 |
二进制安装 | 一般用于mysql的部署 | |
nginx编译安装
# 环境准备
# gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel
yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel
# 下载nginx
wget https://nginx.org/download/nginx-1.22.1.tar.gz
# 解压缩
tar -zxvf nginx-1.22.1.tar.gz
# 进入目录配置参数、编译、安装
cd nginx-1.22.1/
./configure --prefix=/opt/nginx
make && make install
# 启动、停止
nginx -t
nginx
nginx -s stop
nginx -s quit #正常关闭
nginx -s reload
## 卸载nginx -- yum源安装的卸载方式
rpm -e nginx
## 卸载nginx -- 编译安装的卸载方式
find / -iname "nginx*" -exec rm -rf {} \;
## 主要编译参数
# ./configure --help #查看模块参数
./configure \
--prefix=/opt/nginx/ \ # nginx安装路径
--sbin-path=/usr/sbin/nginx \ # 可执行文件路径
--modules-path=/opt/nginx/modules \ # 模块存放路径
--conf-path=/etc/nginx/nginx.conf \ # 配置文件路径
--error-log-path=/opt/nginx/log/error.log \ # 错误日志路径
--pid-path=/opt/nginx/nginx.pid \ # PID文件路径
--lock-path=/opt/nginx/nginx.lock \ # lock文件路径
--user=nginx \ # nginx用户
--group=nginx \ # nginx组
--with-http_ssl_module \ # ssl模块
--with_http_flv_module \ # flv模块
--with_http_gzip_static_module \ # gzip模块
--http-log-path=/opt/nginx/access.log \ #访问日志路径
--http-client-body-temp-path=/opt/nginx/tmp/client/ \ #客户端请求缓存文件路径
--http-proxy-temp-path=/opt/nginx/proxy/ \ # 反向代理缓存文件路径
--http-fastcgi-temp-path=/opt/nginx/fcgi/ \ # fastcgi缓存文件路径
--http-uwsgi-temp-path=/opt/nginx/uwcgi/ \ # uwcgi缓存文件路径
--with-debug # 启用debug日志
nginx yum源安装
## 配置yum源
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
# 清除yum缓存,重新建立yum缓存
yum clean all
yum makecache
# 安装nginx
yum -y install nginx
systemctl start nginx
systemctl enable nginx
systemctl reload nginx
systemctl status nginx
#移除yum包 建议使用 rpm -e 不适用yum -r 该命令会删除系统相应依赖包
## 调整yum优先级
yum -y install yum-plugin-priorities
# 配置文件 /etc/yum/pluginconf.d/priorities.conf
cat /etc/yum/pluginconf.d/priorities.conf
[main]
enabled = 1
# 在nginx.repo中的每个节点加入 优先级越小越高
priority = 1
nginx目录结构
[root@localhost ~]# rpm -ql nginx
/etc/logrotate.d/nginx # nginx日志轮询配置文件
/etc/nginx # 配置文件所在目录
/etc/nginx/conf.d # 导入的配置文件所在目录
/etc/nginx/conf.d/default.conf # 默认配置文件
/etc/nginx/fastcgi_params # fastcgi参数
/etc/nginx/mime.types # 文件类型描述文件
/etc/nginx/modules # nginx模块目录
/etc/nginx/nginx.conf # 主配置文件
/etc/nginx/scgi_params # scgi参数
/etc/nginx/uwsgi_params # uwsgi参数
/usr/lib/systemd/system/nginx-debug.service # systemctl服务管理定义文件
/usr/lib/systemd/system/nginx.service # systemctl服务管理定义文件
/usr/lib64/nginx # nginx库文件目录
/usr/lib64/nginx/modules # nginx模块目录
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx # nginx二进制可执行程序
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.22.1 # 帮助文档目录
/usr/share/doc/nginx-1.22.1/COPYRIGHT
/usr/share/man/man8/nginx.8.gz # 帮助文档压缩文件
/usr/share/nginx # nginx共享目录
/usr/share/nginx/html # nginx站点目录
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx # nginx缓存文件
/var/log/nginx # nginx日志文件
# nginx日志配置文件说明
/etc/logrotate.d/nginx # nginx日志轮询配置文件
[root@localhost ~]# more /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily # 每天轮询一次
missingok # 在日志轮循期间,任何错误将被忽略
rotate 52 # 日志保存52周,即一年
compress # gzip压缩
delaycompress # 保存的日志文件到下一次保存时压缩
# dateext # 保存的日志加上日期
notifempty # 如果是空文件,则不保存
create 640 nginx adm # 创建日志文件时,设置文件的权限与用户
sharedscripts # 共享脚本
postrotate # 所有指令执行后,执行该内容
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
# /etc/nginx 配置文件所在目录
[root@localhost ~]# tree /etc/nginx
/etc/nginx
├── conf.d # 配置文件所在目录
│ └── default.conf # 默认配置文件
├── fastcgi_params # fastcgi参数文件
├── mime.types # 描述文件类型
├── modules -> ../../usr/lib64/nginx/modules # nginx模块目录
├── nginx.conf # 主配置文件
├── scgi_params # scgi参数
└── uwsgi_params # uwcgi参数
# nginx服务配置文件
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# more nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"
[Install]
WantedBy=multi-user.target
# nginx日志文件
[root@localhost html]# cd /var/log/nginx/
[root@localhost nginx]# ls
access.log error.log
[root@localhost nginx]# more access.log
192.168.44.155 - - [08/Jan/2023:17:49:19 +0800] "GET / HTTP/1.1" 200 615 "-" "curl/7.29
.0" "-"
[root@localhost nginx]# more error.log
2023/01/08 17:02:00 [notice] 1872#1872: using the "epoll" event method
2023/01/08 17:02:00 [notice] 1872#1872: nginx/1.22.1
2023/01/08 17:02:00 [notice] 1872#1872: built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44)
(GCC)
2023/01/08 17:02:00 [notice] 1872#1872: OS: Linux 3.10.0-862.el7.x86_64
2023/01/08 17:02:00 [notice] 1872#1872: getrlimit(RLIMIT_NOFILE): 1024:4096
2023/01/08 17:02:00 [notice] 1873#1873: start worker processes
2023/01/08 17:02:00 [notice] 1873#1873: start worker process 1874
nginx主配置文件
[root@localhost nginx]# rpm -qc nginx #查看nginx相关的配置文件
/etc/logrotate.d/nginx
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/mime.types
/etc/nginx/nginx.conf # 主配置文件
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
# nginx主配置文件
[root@localhost nginx]# more nginx.conf
# 核心模块区域 Core functionality
user nginx; # nginx worker进程用户
worker_processes auto; # worker进程数量配置--自动
error_log /var/log/nginx/error.log notice; # 错误日志,日志等级为notice
pid /var/run/nginx.pid; # 指定pid存储文件
# pcre_jit on; # 提升正则表达式处理速度
# 事件模块区域 性能配置
events {
worker_connections 1024; # 指定单个个worker进程的最大连接数
# worker_connections x worker_processes 最大连接数
# accept_mutex on; # 网络连接序列化,防止惊群现象,worker进程依次接受新连接,默认为on
# multi_accept on; # 设置一个进程是否同时接收多个连接,默认为off,一次接收一个新连接
# use epoll; # 设置网络IO模型,默认为epoll
}
# http模块区域 ngx_http_core_module 解决用户的请求与响应以及相应优化参数
http {
include /etc/nginx/mime.types; # 导入文件描述文件
default_type application/octet-stream; # 默认文件类型
# 定义日志格式main
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; # 访问日志设置,应用日志格式main
sendfile on; # 零拷贝,提升文件传输性能,默认为off
# sendfile_max_chunk 100k; # 允许传输文件大小,默认为0,不设置上限
#tcp_nopush on;
keepalive_timeout 65; # 设置超时时间
#gzip on;
include /etc/nginx/conf.d/*.conf; # 导入配置文件目录
}