如果你的nginx是通过yum安装的,那么nginx的配置文件默认在/etc/下的nginx下的二级子目录里,主配置文件名为nginx.conf。
如果你用编译安装nginx并且安装在了/usr/local/下,那么nginx的配置文件应该在/usr/local/nginx/conf/nginx.conf。下面我们可以看看nginx的主配置文件都写了些什么。
我会以–开头给出我的注释,请不要与#注释混淆。
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
--使用的用户和用户组
worker_processes auto;
--指定工作衍生进程数,默认配置文件这里是auto。一般来说应等于CPU的总核数或总核数的二倍,两个四核CPU,总核数就是8
error_log /var/log/nginx/error.log;
--指定错误日志存放路径
pid /run/nginx.pid;
--指定pid存放路径
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
--这里省略了使用的网络I/O模型,Linux默认采用epoll模型,
worker_connections 1024;
--允许连接数
}
http {
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;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
事实上nginx的配置文件主要包括两部分
############
events {
############
}
############
http {
############
server {
############
}
############
}
nginx的虚拟主机配置
虚拟主机就是把一台服务器分割成一台台虚拟的主机,每台主机都可以提供不同的web服务。虚拟主机可以基于IP或域名进行区分。
以前写过,这里就不再写了。
nginx日志文件配置
与日志相关的配置有两条,一条是log_format,用来设置日志格式;另一条是access_log,用来指定日志存放路径。我们来看在nginx.conf里的默认配置。
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;
指定日志格式
首先介绍一下log_format中各项含义
$remote_addr | 记录IP地址 |
$remote_user | 记录远程客户端用户名称 |
$time_local | 记录访问时间与时区 |
$request | 记录请求URL与http协议 |
$status | 记录请求状态 |
$body_bytes_sent | 记录发送给客户端的文件主体内容大小 |
$http_referer | 记录是从哪个页面链接访问过来的 |
$http_user_agent | 记录客户端浏览器的相关信息 |
$http_x_forwarded_for | 记录IP地址 |
细心的童鞋可能发现了,有两个记录IP地址的,它们显然不是把同一条信息写两遍
假设本服务器作为web服务器,在生产环境中客户端想要访问到本主机很可能经过反向代理中间层,这时我们获取的第一个IP就不再是客户端的IP而是反向代理服务器的IP了,但是反向代理服务器可以在转发请求的HTTP头信息里记录原来的客户端IP地址和原来的客户端请求的服务器地址。
指定日志存放路径
默认的日志路径设置在这里
access_log /var/log/nginx/access.log main;
如果想关闭日志功能,可以:
access_log off;
日志路径可以包含变量:
access_log /var/log/nginx/$server_name.log main;
nginx日志文件切割
生产环境下的web服务器,日志的增长速度非常快,我们最好对日志按天切割。
mv access.log 20190913.log
事实上这里有一个有趣的现象,如果我执行了上述命令并且nginx继续提供服务,那么新的日志信息会被写到哪里呢?是重新生成一个access.log吗。日志信息会继续写到20190913.log里,这时我们需要通知应用程序重新加载配置文件,也就是:
ps aux|grep nginx
root 7381 0.0 0.1 122916 2112 ? Ss 08:12 0:00 nginx: master process /usr/sbin/nginx
nginx 7382 0.0 0.1 123304 3600 ? S 08:12 0:00 nginx: worker process
root 7387 0.0 0.0 112724 988 pts/0 R+ 08:15 0:00 grep --color=auto nginx
kill -USR1 7381
要想在生产环境中实现日志分割,我们还需要cron的帮助。
首先写好日志分割的shell脚本(这里只给出最基础的脚本,请自行优化)
#!/bin/bash
pid=`ps aux|grep nginx|grep master|awk '{print $2}'`
date=`date '+%F'`
mv /var/log/nginx/access.log /var/log/nginx/"$date".log
kill -USR1 "$pid"
设置一个定时任务,在每天00:00自动执行此脚本
0 0 * * * sh /var/shell/nginx_log_cut.sh
nginx的目录列出配置
Apache默认显示当前页面下的目录,而nginx默认不显示目录,当然我们只需要修改主配置文件一个参数就可以实现nginx相同功能
location / {
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
}
autoindex | 显示目录 | nginx默认不显示目录 |
autoindex_exact_size | 设置索引时文件大小的单位 | 默认为on,精确显示文件大小,单位为bytes;改为off时会显示文件的大概大小,单位是KB,MB,GB |
autoindex_localtime | 以本地时间显示文件时间 | 默认为off,显示GMT时间,修改为on后显示文件的服务器时间 |