nginx服务目录结构
一、整体查看nginx服务的命令
nginx -V
systemctl status nginx
---------------------------------------------
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments:
--prefix=/etc/nginx # 指定安装路径
--sbin-path=/usr/sbin/nginx # 程序文件位置
--modules-path=/usr/lib64/nginx/modules # 模块路径的位置
--conf-path=/etc/nginx/nginx.conf # 主配置文件的位置
--error-log-path=/var/log/nginx/error.log # 错误日志位置
--http-log-path=/var/log/nginx/access.log # 访问日志位置
--pid-path=/var/run/nginx.pid # 程序PID
--lock-path=/var/run/nginx.lock # 锁路径,防止重复启动nginx
--http-client-body-temp-path=/var/cache/nginx/client_temp # 缓存
--http-proxy-temp-path=/var/cache/nginx/proxy_temp # 代理缓存
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp # php缓存
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp # python缓存位置
--http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx # 用户
--group=nginx # 组
--with-compat # 启动动态模块兼容
--with-file-aio # 提高性能
--with-threads # 多线程模块
--with-http_addition_module # 响应之前或者之后追加文本内容
--with-http_auth_request_module # 认证模块,比如登录密码
--with-http_dav_module # 增加上传PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)默认情况下为关闭
--with-http_flv_module # NGINX添加MP4、FLV视频支持模块
--with-http_gunzip_module # 压缩模块
--with-http_gzip_static_module # 压缩模块
--with-http_mp4_module # 支持多媒体
--with-http_random_index_module # 随机主页
--with-http_realip_module # nginx获取真实ip模块
--with-http_secure_link_module # nginx安全下载模块
--with-http_slice_module # nginx中文文档
--with-http_ssl_module # 网站加密
--with-http_stub_status_module # 访问状态
--with-http_sub_module # nginx替换响应内容
--with-http_v2_module # web2.0技术
# 邮局
--with-mail # 邮件
--with-mail_ssl_module
# 负载均衡反向代理模块
--with-stream
--with-stream_realip_module
--with-stream_ssl_module
--with-stream_ssl_preread_module
# CPU优化参数等
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
二、定位nginx配置文件位置
whereis nginx.conf
三、配置文件介绍
主配置文件:nginx.conf
------------------------------------------------------
1、全局/核心块。配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,元许生成workerprocess数等。
user nginx; # 指定Nginx的启动用户
worker_processes auto; # 开启nginx的数量,可以自定义,建议和CPu一样多,2核就写2个···
error_log /var/log/nginx/error.log notice; # 错误日志
pid /var/run/nginx.pid; # 进程号存放路径
2、events块,配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
events {
worker_connections 1024; # 进程最大连接数
}
3、http模块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
http {
include /etc/nginx/mime.types; # 加载外部的配置项,降低了文件的复杂度
default_type application/octet-stream; # 字节流处理方式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; # 日志格式,可以修改为json
access_log /var/log/nginx/access.log main; # 访问日志
sendfile on; # 加速访问、高效读取文件
#tcp_nopush on; # 优化
keepalive_timeout 65; # 长连接,timeout不能太低,不然和短链接一样
#gzip on; # 压缩
include /etc/nginx/conf.d/*.conf; # 配置文件
}
4、server块:配置虚拟主机的相关参数,一个http中可以有多个server
5、location块:配置请求的路由,以及各种页面的处理情况
http模块:nginx配置结构分为三层 http > server > location
http 包含一到多个server, server包含一到多个location
配置项的优先级分别是location, server, http
----------------------------------------------------------
http {
...
access_log /var/logs/nginx/nginx.log;
server {
server_name A;
...
access_log /var/logs/nginx/serverA/nginx.log;
location / {
...
access_log /var/logs/nginx/serverA/localtion/nginx.log;
}
}
}
location模块:使用Nginx Location可以控制访问网站的路径, 但一个server可以有多个location配置, 多个location的优先级该如何区分,就用到了location配置的优先级~
三者之间的关系:
http:主要用来解决用户的请求和响应。
server:主要用来响应具体的某一个网站。
location:用于匹配网站的uri路径。