Nginx基本配置¶
认识配置文件¶
1、配置文件结构
main:子进程
events:控制nginx处理连接方式
http:处理http请求
server:主机配置
location:目录级别的配置,如location /,即根目录配置
2、指令
worker_process:工作进程数,一般为CPU数或×2,默认1
worker_connections:允许单个进程并发连接的最大值,默认1024
include:引入配置文件
default_type:默认文件类型,如application/octet-stream
sendfile:开启高效文件传输模式,默认on
keepalive_timeout:设置长连接超时时间(秒),默认65
listen:监听端口,默认80
server_name:设置主机域名,默认localhost
root:设置主机站点根目录地址,默认html
index:设置默认索引文件,默认index.html index.htm
error_page:自定义错误页面,指向location配置,默认500 502 503 504 /50x.html
3、配置用户和组user
编译前配置:./configuer --user=
访问控制¶
权限控制指令allow,deny 默认:全部allow 禁止所有用户访问:deny all; 只允许指定用户访问: server {... allow 192.168.1.100; # 同级按先后顺序, deny all; } 嵌套:内部优先级高于外部 访问控制应用 location:匹配目录,有以下规则 无符号,最大前缀匹配 location /ng.test { ... # http://xxx/ng.test/xxx 匹配 location /ng.test/log { ... # http://xxx/ng.test/log/xxx 匹配 =精确匹配,location =/admin/auth { ... ~正则匹配,location ~.html$ { ... location ~^/aaa/.*.html$ { ... ~*正则不区分大小写匹配 ^~指定模式匹配 @不被客户端访问仅nginx内部访问 root和alias: root从该目录开始查找匹配到的location,即{root}/{location}/a.img alias以该目录为location的别名即{alias}/a.img
日志文件¶
log_format指令,配置日志格式
格式:log_format {name} '{format}' '{format}'
参数:
$remote_addr客户端IP地址
$remote_user客户端用户名
$time_locale时区
$request请求的URI和协议
$status返回的状态码
$body_bytes_sent发送给客户端文件内容大小
$http_referer来路URL
$http_user_agent客户端浏览器信息
$http_x_forwarded_for客户端IP地址列表(包括代理)
自定义日志格式:log_format mylog '[ip:] $remote_addr [time:] time_local [user_agent:] "http_user_agent"';
access_log指令,访问日志
如access_log logs/xxx/access.log mylog buffer=2k flush=5s
如access_log off # 关闭访问日志
log_subrequest指令
允许记录子请求的日志记录,默认off,可以设置为on
error_log指令,错误日志
如error_log logs/xxx/error.log;
如error_log logs/xxx/error.log notice # 指定记录等级
如error_log dev/null # 关闭错误日志
日志文件切割
手动切割
$ ls /usr/local/nginx/logs/ # 查看目录
$ mv /usr/local/nginx/logs/xxx/access.log /usr/local/nginx/logs/xxx/20190101.log # 按日期格式重命名日志文件
$ nginx -s reopen # 生成新的日志文件
自动切割,autolog.sh
#! /bin/bash
logs_path="/usr/local/nginx/logs/xxx"
mv $logs_path/access.log $logs_path/date +"%Y%m%d%H%M"
.log
/usr/local/nginx/sbin/nginx -s reopen
如果要保存前一天的日志可改为
mv $logs_path/access.log $logs_path/date -d yesterday +"%Y%m%d"
.log
定时任务,系统自动备份
$ crontab -e # 进入定时任务编辑
0 0 * * * /user/local/nginx/logs/xxx/autolog.sh >/dev/null 2>&1 # 分 时 日 月 星期 命令 错误不输出
虚拟主机¶
基于端口号配置虚拟主机nginx.conf
# 配置监听8001端口号的虚拟主机 server { listen 8001; server_name 192.168.1.100; root html/html8001; index index.html index.htm; } # 配置监听8001端口号的虚拟主机 server { listen 8002; server_name 192.168.1.100; root html/html8002; index index.html index.htm; }
基于IP配置虚拟主机 step1配置子IP 1.修改文件方式 复制文件 $ cd /etc/sysconfig/network-scripts/ $ cp ifcfg-eth0 ifcfg-eth0:1 $ cp ifcfg-eth0 ifcfg-eth0:2 编辑文件,ifcfg-eth0:1和ifcfg-eth0:2 DEVICE=eth0:1 IPADDR=192.168.1.101 和 DEVICE=eth0:2 IPADDR=192.168.1.102 生效配置 service network reload 2.命令方式 $ ifconfig eth0:1 192.168.1.101 broadcast 192.168.1.255 net mask 255.255.255.0 up $ route add -host 192.168.1.101 dev eth0:1 此方式重启后配置会重置,可将此命令复制到/etc/rc.local中,开机自动配置。 step2修改配置文件nginx.conf
# 配置基于IP为192.168.1.101的虚拟主机 servser { listen 80; server_name 192.168.1.101; root html/192.168.1.101; index index.html index.htm; } # 配置基于IP为192.168.1.102的虚拟主机 servser { listen 80; server_name 192.168.1.102; root html/192.168.1.102; index index.html index.htm; }
基于域名配置虚拟主机 step1修改hosts文件,/etc/hosts 添加IP地址与域名映射
127.0.0.1 www.xxx.com 127.0.0.1 xxx.com
# 配置域名为www.xxx.com的虚拟主机 servser { listen 80; server_name www.xxx.com; root html/www.xxx.com; index index.html index.htm; }
设置目录列表,在无index文件时显示目录列表 在server或location中: autoindex on; # 打开显示目录列表功能 autoindex_exact_size off; # 以kB/MB/GB显示文件大小 autoindex_localtime on; # 最后修改时间以本地时间显示,不设置默认为GMT格林尼治时间。
引入子配置文件 include file | mask # file文件名,mask绝对或相对路径下的文件 include vhost/*.conf #通配符引入配置文件