Nginx笔记
本篇博客是自己学习Nginx过程中记录的笔记,短期内会持续进行更新。
Nginx版本为1.14.0,操作系统为CentOS 7.4_x64。
niginx安装
Nginx官方网站:http://nginx.org
Nginx下载地址:http://nginx.org/en/download.html
说明:
- Mainline version - 开发版本
- Stable version - 稳定版本
- Legacy versions - 历史版本
- Source Code - 源代码
- Pre-Built Packages - Linux源安装
基本操作
启动nginx
[root@Linux7 ~]# systemctl start nginx.service
重启nginx
[root@Linux7 ~]# systemctl restart nginx.service
目录说明
路径 | 类型 | 作用 |
---|---|---|
/etc/logrotate.d/nginx | 配置文件 | 使用logrotate服务,切割Nginx日志 |
/etc/nginx /etc/niginx/nginx.conf /etc/nginx/conf.d /etc/nginx/conf.d/default.conf | 目录、配置文件 | Nginx主配置文件(nginx启动时,主要读取nginx.conf,然后默认读取defaylt.conf) |
/etc/nginx/fastcgi_params /etc/nginx/uwsgi_params /etc/nginx/scgi_params | 配置文件 | cgi配置相关,fastcgi配置相关 |
/etc/nginx/koi-utf /etc/nginx/koi-win /etc/nginx/win-utf | 配置文件 | 编码转换映射转化文件 |
/etc/nginx/mime.types | 配置文件 | 设置http协议的Content-Type与扩展名对应关系 |
/usr/lib/systemd/system/nginx-debug.service /usr/lib/systemd/system/nginx.service /etc/sysconfig/nginx /etc/sysconfig/nginx-debug | 配置文件 | 用于配置系统守护进程管理器管理方式 |
/usr/lib64/nginx/modules /etc/nginx/modules | 目录 | Nginx模块目录 |
/usr/sbin/nginx /usr/sbin/nginx-debug | 命令 | Nginx服务的启动管理的终端命令 |
/usr/share/doc/nginx-1.14.0 /usr/share/doc/nginx-1.14.0/COPYRIGHT /usr/share/man/man8/nginx.8.gz | 文件、目录 | Nginx的手册和帮助文件 |
/var/cache/nginx | 目录 | Nginx的缓存目录 |
/var/log/nginx | 目录 | Nginx的日志目录 |
编译参数说明
查看安装编译参数
[root@Linux7 ~]# nginx -V
编译选项 | 作用 |
---|---|
--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 --lock-path=/var/run/nginx.lock | Nginx的基础路径(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 --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp | 执行对应模块时,Nginx所保留的临时性文件 |
--user=nginx --group=nginx | 设定Nginx进程启动的用户和用户组1 |
--with-cc-opt= [parameter] | 设置C语言编译器参数(将额外的参数,添加到CFLAGS变量) |
--with-ld-opt= [parameter] | 设置附加的参数,链接系统库 |
nginx.conf
Nginx的主配置文件
Nginx默认配置语法
语法 | 作用 |
---|---|
user | 设置nginx服务的系统使用用户 |
worker_process | 工作进程数,一般和CPU核数保持一致 |
error_log | nginx的错误日志 |
pid | nginx服务启动时候pid |
events | worker_connections | 每个进程允许最大连接数 |
use | 工作进程数 |
nginx.conf http配置
http的参数 | 说明 |
---|---|
include | 子配置文件 |
log_format | 定义日志类型 |
access_log | 访问的日志 |
sendfile | (默认是打开的) |
keepalive_tiomeout | 客户端与服务端的超时时间(单位:秒) |
niginx会首先读取nginx.conf主配置文件,然后会读取nginx.conf里http
中include
对应的配置文件。
nginx.conf http中的log_format配置
log_format的参数 | 说明 |
---|---|
$remote_addr | 客户端地址 |
$remote_user | 客户端用户名称 |
$time_local | 访问时间和时区 |
$request | 请求的URI和HTTP协议 |
$http_host | 请求地址,即浏览器中你输入的地址(IP或域名) |
$status | HTTP请求状态 |
$upstream_status | upstream状态 |
$body_bytes_sent | 发送给客户端文件内容大小 |
$http_referer | url跳转来源 |
$http_user_agent | 用户终端浏览器等信息 |
$http_x_forwarded_for | 记录每一级用户通过http请求所携带的http信息 |
$ssl_protocol | SSL协议版本 |
$ssl_cipher | 交换数据中的算法 |
$upstream_addr | 后台upstream的地址,即真正提供服务的主机地址 |
$request_time | 整个请求的总时间 |
$upstream_response_time | 请求过程中,upstream响应时间 |
nginx.conf http中的server配置
一个http中可以有多个server服务,一个server配置一个独立的站点。
server的参数 | 说明 |
---|---|
listen | server所监听的端口 |
server_name | server服务名 |
location | 一个server里面可以有多个location |
error_page | 定义错误状态跳转的页面 |
location后的/
表示当server里面没有其他访问路径的默认访问。root
表示根路径
Nginx变量
HTTP请求变量 - arg_PARAMETER、http_HEADER、sent_http_HEADER
内置变量 - Nginx内置的
自定义变量 - 自己定义的变量
检查nginx配置文件
可检查nginx配置文件格式是否正确。
[root@Linux7 ~]# nginx -tc /etc/nginx/nginx.conf
重载nginx配置文件
[root@Linux7 ~]# nginx -s reload -c /etc/nginx/nginx.conf
http请求
request : 包括请求行、请求头部、请求数据
response:包括状态行、消息报头、响应正文
Nginx模块讲解
Nginx模块分为 Nginx官方模块
和 第三方模块
。
使用 nginx -V
可查看到Nginx所编译(开启)的模块,下图中选中的就是Nginx模块。
编译选项 | 作用 |
---|---|
--with-http_stub_status_module | Nginx的客户端状态 |
--with-http_random_index_module | 目录中选择一个随机主页 |
http_stub_status_module配置
配置语法: stub_status;
默认值: —
可配置位置: server, location
使用示例:
修改 /etc/nginx/conf.d/vim default.conf
文件,增加以下内容:
location /mystatus {
stub_status;
}
重载nginx配置文件后,可在浏览器中查看Nginx相关信息,访问方式为http://IP地址:端口/mystatus
参数 | 说明 |
---|---|
Active connections | Nginx当前活跃的连接数 |
server accepts handled requests | 第一个数:Nginx处理的握手次数 第二个数:Nginx处理的连接数 第三个数:总请求数 (正常情况下,握手数=连接数,表示没有丢失) |
Reading | 正在读取的数 |
Writing | 正在写入的数 |
Waiting | 等待数(表示客户端和服务端已建立连接,并且没有读写的熟练) |
http_random_index_module配置
配置语法: random_index on | off;
默 认 值: random_index off;
可配置位置: location
使用示例:
location / {
root /opt/app/code;
random_index on;
}
通过浏览器访问nginx时,nginx会随机选择/opt/app/code目录下的页面进行展示。但如果有隐藏的主页面(以.
开头的隐藏文件)则不会访问。
Nginx出于安全性的考虑,就算使用root用户启动Nginx,Nginx真正工作的进程也是会用nginx用户运行,这个是可以在配置文件进行修改的。 ↩︎