序言
- 本文章以windows版本的nginx-1.14.2的nginx.conf文件作为例子,其他关于nginx的知识,可参考我的其他文章。
- 我的博客关于nginx的内容地址为:https://blog.csdn.net/zengwende/article/category/8635956
正文
#user nobody;(运行用户)
worker_processes 1;(启动进程,通常设置成和cpu的数量相等)#error_log logs/error.log;(全局错误日志)
#error_log logs/error.log notice;(全局错误日志)
#error_log logs/error.log info;(全局错误日志)#pid logs/nginx.pid;(PID文件存储位置)
events {(工作模式及连接数上限)#use epoll;(使用epoll的I/O 模型。linux建议epoll,FreeBSD建议采用kqueue,window下不指定)
worker_connections 1024;(单个后台worker process进程的最大并发链接数)
}
http {(设定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"';(日志格式设置。
$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
$remote_user:用来记录客户端用户名称;
$time_local: 用来记录访问时间与时区;
$request: 用来记录请求的url与http协议;
$status: 用来记录请求状态;成功是200,
$body_bytes_sent :记录发送给客户端文件主体内容大小;
$http_referer:用来记录从那个页面链接访问过来的;
$http_user_agent:记录客户浏览器的相关信息;
通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。)
#access_log logs/access.log main;(设定日志格式,用了log_format指令设置了日志格式之后,需要用access_log指令指定日志文件的存放路径;)
sendfile on;(对于普通应用,指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件。必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime。)
#tcp_nopush on;#keepalive_timeout 0;(连接超时时间)
keepalive_timeout 65;#gzip on;(开启gzip压缩)
server { (配置虚拟机)
listen 80;(侦听80端口,80为默认端口)
server_name localhost;(定义域名,比如使用www.xx.com访问)#charset koi8-r;
#access_log logs/host.access.log main;(设定本虚拟主机的访问日志)
location / {(默认请求)
root html;(定义服务器的默认网站根目录位置)
index index.html index.htm;(定义首页索引文件的名称)
}#error_page 404 /404.html;(定义404错误提示页面)
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;(定义500 502 503 504错误提示页面)
location = /50x.html {
root 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$ { (PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置)
# 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 {(#禁止访问 .htxxx 文件)
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration(使用基于IP、名称和端口的配置组合的另一个虚拟主机)
#
#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;
# }
#}}