nginx学习笔记(3)
nginx学习笔记(3)
一、yum安装nginx
访问nginx的官方网站:http://www.nginx.org/
配置Yum源的官网:http://nginx.org/en/linux_packages.html
# 关闭防火请和selinux
[root@nginx ~]# yum install yum-utils -y
[root@nginx ~]# vim /etc/yum.repo.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
[root@nginx ~]# yum makecache
[root@nginx ~]# yum install nginx -y
[root@nginx ~]# nginx -V
[root@nginx ~]# systemctl start nginx
[root@nginx ~]# systemctl enable nginx
[root@nginx ~]# repoquery -ql nginx
二、源码安装
[root@nginx ~]# yum -y install gcc gcc-c++ pcre pcre-devel gd-devel openssl openssl-devel openssl openssl-devel
[root@nginx ~]# useradd nginx
[root@nginx ~]# passwd nginx
[root@nginx ~]# yum -y install wget
[root@nginx ~]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
[root@nginx ~]# tar xzf nginx-1.20.1.tar.gz -C /usr/local/
[root@nginx ~]# cd /usr/local/nginx-1.20.1/
[root@nginx ~]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream
**# 查看 nginx 安装的模块**
[root@nginx ~]# /usr/local/nginx/sbin/nginx -V
--prefix=/usr/local/nginx //指向安装目录
--conf-path=/etc/nginx/nginx.conf //指定配置文件
--http-log-path=/var/log/nginx/access.log //指定访问日志
--error-log-path=/var/log/nginx/error.log //指定错误日志
--lock-path=/var/lock/nginx.lock //指定lock文件
--pid-path=/run/nginx.pid //指定pid文件
--http-client-body-temp-path=/var/lib/nginx/body //设定http客户端请求临时文件路径
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi //设定http fastcgi临时文件路径
--http-proxy-temp-path=/var/lib/nginx/proxy //设定http代理临时文件路径
--http-scgi-temp-path=/var/lib/nginx/scgi //设定http scgi临时文件路径
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi //设定http uwsgi临时文件路径
--with-debug //启用debug日志
--with-ipv6 //启用ipv6支持
--with-http_ssl_module //启用ssl支持
--with-http_stub_status_module //获取nginx自上次启动以来的状态
--with-http_realip_module //允许从请求标头更改客户端的IP地址值,默认为关
--with-http_auth_request_module //实现基于一个子请求的结果的客户端授权。如果该子请求返回的2xx响应代码,所述接入是允许的。如果它返回401或403中,访问被拒绝与相应的错误代码。由子请求返回的任何其他响应代码被认为是一个错误。
--with-http_addition_module //作为一个输出过滤器,支持不完全缓冲,分部分响应请求
--with-http_dav_module //增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法 默认关闭,需编译开启
--with-http_geoip_module //使用预编译的MaxMind数据库解析客户端IP地址,得到变量值
--with-http_gunzip_module //它为不支持“gzip”编码方法的客户端解压具有“Content-Encoding: gzip”头的响应。
--with-http_gzip_static_module //在线实时压缩输出数据流
--with-http_spdy_module //SPDY可以缩短网页的加载时间
--with-http_sub_module //允许用一些其他文本替换nginx响应中的一些文本
--with-http_xslt_module //过滤转换XML请求
--with-mail //启用POP3/IMAP4/SMTP代理模块支持
--with-mail_ssl_module //启用ngx_mail_ssl_module支持启用外部模块支持
--with 表示在编译过程中需要给nginx添加的模块
--without 表示编译nginx时默认该模块是添加进去的当使用这个参数时表示将默认编译的模块移除
[root@nginx ~]# make && make install
[root@nginx ~]# /usr/local/nginx/sbin/nginx -V
# 更改配置文件后检测nginx配置文件是否正确
[root@nginx ~]# /usr/local/nginx/sbin/nginx -t
[root@nginx ~]# mkdir -p /tmp/nginx
# 绝对路径起服务
[root@nginx ~]# /usr/local/nginx/sbin/nginx
# 热载
[root@nginx ~]# /usr/local/nginx/sbin/nginx -s reload
[root@nginx ~]#
[root@nginx ~]#
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
配置文件:
# 全局参数设置
user nginx; #设置nginx使用的用户
worker_processes 4; #设置nginx启动进程的数量,一般设置成与逻辑cpu数量相同
error_log logs/error.log; #指定错误日志
pid /var/run/nginx.pid;
events {
worker_connections 1024; #设置一个进程的最大并发连接数
}
# http 服务相关设置
http {
include mime.types; #关联mime类型,关联资源的媒体类型
default_type application/octet-stream; #根据文件的后缀来匹配相应的MIME类型
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; #用于开启文件高效传输模式,一般设置为on,若nginx是用来进行磁盘IO负载应用时,可以设置为off,降低系统负载
tcp_nopush on; # 减少网络报文段数量,当有数据时,先别着急发送, 确保数据包已经装满数据, 避免了网络拥塞
tcp_nodelay on; # 提高I/O性能,确保数据尽快发送, 提高可数据传输效率
gzip on; #是否开启gzip压缩,将注释去掉开启
keepalive_timeout 65; #设置长连接的超时时间,请求完成之后还要保持连接多久,
# 虚拟服务器的相关设置
server {
listen 80; #设置监听的端口
server_name localhost; #设置绑定的主机名、域名或ip地址
charset koi8-r; # 设置编码字符
location / { # 匹配级别
root /var/www/nginx; #设置服务器默认网站的根目录位置,需要手动创建
index index.html index.htm; #设置默认打开的文档
}
error_page 500 502 503 504 /50x.html; #设置错误信息返回页面
location = /50x.html {
root html; #这里的绝对位置是/usr/local/nginx/html
}
}
}
2.1.nginx.conf的组成:nginx.conf一共由三部分组成,分别为:全局块、events(模型)块、http块。在http块中又包含http全局块、多个server块。每个server块中又包含server全局块以及多个location块。在统一配置块中嵌套的配置快,各个之间不存在次序关系。
2.2.Nginx的组成:
nginx的二进制可执行文件----启动关闭,有各个模块编译出来的
nginx.conf配置文件----控制nginx
access.log访问日志--记录每条http请求
error.log 错误日志--定位问题
通过 nginx 命令控制 nginx 服务
nginx -c /path/nginx.conf # 以特定目录下的配置文件启动nginx:
nginx -s reload # 修改配置后重新加载生效
nginx -s stop # 快速停止nginx
nginx -s quit # 正常停止nginx
nginx -t # 测试当前配置文件是否正确
nginx -t -c /path/to/nginx.conf # 测试特定的nginx配置文件是否正确
注意:
nginx -s reload 命令加载修改后的配置文件,命令下达后发生如下事件
2.1.1. Nginx的master进程检查配置文件的正确性,若是错误则返回错误信息,nginx继续采用原配置文件进行工作(因为worker未受到影响)
2.1.2. Nginx启动新的worker进程,采用新的配置文件
2.1.3. Nginx将新的请求分配新的worker进程
2.1.4. Nginx等待以前的worker进程的全部请求已经都返回后,关闭相关worker进程
2.1.5. 重复上面过程,知道全部旧的worker进程都被关闭掉
nginx源码编译安装,我写了一个脚本,适用于只安装了gcc-c++ 编译工具,其余依赖也是源码安装,使用的场景就是在内网环境服务器在刚装了系统,安装了deployment-tools的情况下,这里就不复制粘贴了,有需要交流的私聊吧。
本文作者:Tanuki_11
本文链接:https://www.cnblogs.com/tanukisama/p/15809092.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构