nginx配置文件
在/etc/nginx 里面
conf.d(扩展配置文件)
modules (模块)
nginx.conf (主配置文件)
nginx.conf配置文件讲解
user nginx;
这里表示nginx的子进程以nginx身份运行(user代表当前nginx以那个用户运行)ps aux |grep 'nginx'查看nginx是那个用户运行 nginx的主进程永远都是root用户运行
worker_processes auto;
启动Nginx工作进程的数量,一般设为和CPU核心数相同,auto:自动检测,设置为当前主机cpu的核心数
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
error_log logs/error.log 错误日志
pid /var/run/nginx.pid; 当nginx运行的时候,nginx的主进程号就会放在这里
events {
worker_connections 1024;
}
上面是 工作模式 是worker 1024表示nginx的子进程有1024个(一个子进程可以接受多个请求)
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/mime.types;文件扩展名与文件类型映射表
default_type application/octet-stream;默认的文件类型,默认为text/plain
log_format main '$remote_addr - time_local] “request" '
'status http_referer” ’
‘“http_x_forwarded_for”’;
自定义格式
remote_addr
客户端地址,表示发起访问的客户端 ip(一般为该用户的公网出口地址)
若客户端是代理访问,则默认为代理服务器的地址
若使用多层代理,则默认为最后一台代理服务器的地址
注:可以通过修改服务端配置实现
remote_user
客户端用户名称,启用用户验证()时所使用的用户(一般情况下不开启)
[$time_local]
访问时间和时区
$request
http请求的方法、url和http协议版本
$status
Http的请求状态
$upstream_status
upstream状态
$body_bytes_sent
发送给客户端文件内容大小
$http_referer
url跳转来源
$http_user_agent
用户终端浏览器信息
$ssl_protocol
SSL协议版本
$ssl_cipher
交换数据中的算法
$upstream_addr
后台upstream的地址,即真正提供服务的主机地址
$request_time
整个请求的总时间
$upstream_response_time
请求过程中,upstream响应时间
access_log /var/log/nginx/access.log main;
access.log 日志文件 combined为日志格式的默认值
sendfile on;
允许sendfile方式传输文件,默认的是off,可以在http块,server块,location块。
#tcp_nopush on;
这个只能在将连接转变为长连接的时候才会被启用
keepalive_timeout 65;
#连接超时时间为多少,可以在http,server,location块。
#gzip on;
打开压缩
include /etc/nginx/conf.d/*.conf;
}
default.conf配置文件讲解
在vim /etc/nginx/conf.d/default.conf
server {
listen 80;
配置监听端口
server_name localhost;
配置域名
#access_log /var/log/nginx/host.access.log main;
配置这个虚拟机的访问日志
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
当server里面没有其他的路径访问时,就会默认访问配置目录root /usr/share/nginx/html; 下的文件
index index.html index.htm;首页默认访问的哪些文件
#error_page 404 /404.html; # 错误页
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ { #对****做负载均衡的
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}