nginx配置文件就是变量文件,让用户自己指定变量,保存软件运行的各种参数
配置文件:/usr/local/nginx/conf/nginx.conf
user nobody;
//启动子进程的默认用户
/*
定义nginx用户是由谁来启动的
默认用户查看:
lsof -i :80
结果:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 10931 root 6u IPv4 58300 0t0 TCP *:http (LISTEN)
nginx 10932 nobody 6u IPv4 58300 0t0 TCP *:http (LISTEN)
一个主进程(root)和多个子(工作)进程,工作进程是单进程的,且不需要特殊授权即可运行
*/
worker_processes 1;
//定义子(工作)进程数量
/*
查看默认用户和进程:lsof -i :80
结果:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 12486 root 6u IPv4 72054 0t0 TCP *:http (LISTEN)
nginx 12487 nobody 6u IPv4 72054 0t0 TCP *:http (LISTEN)
修改默认用户为:user www
修改进程数量为:worker_processes 4
创建测试用户www:useradd -s /sbin/nologin -r www
杀死nginx进程:killall nginx
启动nginx进程:/usr/local/nginx/sbin/nginx
查看用户和进程数:lsof -i :80
结果:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 12426 root 6u IPv4 71805 0t0 TCP *:http (LISTEN)
nginx 12427 www 6u IPv4 71805 0t0 TCP *:http (LISTEN)
nginx 12428 www 6u IPv4 71805 0t0 TCP *:http (LISTEN)
nginx 12429 www 6u IPv4 71805 0t0 TCP *:http (LISTEN)
nginx 12430 www 6u IPv4 71805 0t0 TCP *:http (LISTEN)
*/
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
//全局错误日志的位置及级别格式
#pid logs/nginx.pid;
/*主(父)进程号存放地址
查询看一下是不是上面的PID 12486
查看PID:cat /usr/local/nginx/logs/nginx.pid
结果:12486
*/
events {
worker_connections 1024;
//每个工作进程的最大并发数
/*进程数量*线程数量等于并发数量
就是最多可以连接多少用户
*/
}
http {
include mime.types;
//设定mime类型,类型由mime.type文件定义
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"';
/*
字段解释:
main是日志的格式
$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址
$remote_user:用于记录客户端用户名称
$time_lcoal:用于记录访问时间与时区
$request:用来记录请求的URl与http协议
$status:用来记录请求状态:成功是200
$body_bytes_sent:记录发送给客户文件主体内容的大小
$http_referer:用来记录从哪个页面链接访问过来的
$http_user_agent:记录客户浏览器的相关信息
*/
access_log logs/access.log main;
/*日志存放路径 存放采用main这种格式
查看日志:
cat /usr/local/nginx/logs/access.log
查看日志结果:
192.168.43.128 - - [05/Nov/2019:20:39:02 +0800] "GET / HTTP/1.1" 200 612 "-" "ELinks/0.12pre6 (textmode; Linux; 171x36-2)"
192.168.43.128 - - [05/Nov/2019:20:40:03 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0"
192.168.43.128 - - [05/Nov/2019:20:42:26 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0"
192.168.43.128 - - [05/Nov/2019:20:43:25 +0800] "GET / HTTP/1.1" 200 612 "-" "ELinks/0.12pre6 (textmode; Linux; 171x36-2)"
*/
sendfile on;
/*
sendfile指令指定 nginx 是否调用sendfile函数(zero copy方式)来输出文件
对于普通应用,必须设为on
*/
tcp_nopush on;
/*
此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在sendfile使用
*/
#keepalive_timeout 0;
keepalive_timeout 65;
//长连接超时时间
gzip on;
//开启压缩
#配置虚拟主机--一个server就是一个主机
server {
listen 80;
//配置虚拟主机使用的端口
server_name localhost;
//虚拟主机域名
charset koi8-r;
//虚拟主机支持的字符集
access_log logs/host.access.log main;
//虚拟主机的访问日志路径
location / {
//定义web根路径
root html;
//根目录路径 在这里是/usr/local/nginx/html
index index.html index.htm;
//索引页面 在这里是/usr/local/nginx/html/index.html
}
#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 html;
//定义错误页面的路径 也是在/usr/local/nginx/html下
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
//定义反向代理服务器 数据服务器是lamp模型
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
//定义PHP为本机服务的模型
#
#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;
//拒绝apache DR目录下的.htaccess文件访问
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}