配置文件
默认的配置文件就在/usr/local/nginx/conf/nginx.conf
默认的首页index页面路径 /usr/share/nginx/html
查看配置文件路径
# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful
user nginx;worker_processes 1;error_log /var/log/nginx/error.log warn;pid /var/run/nginx.pid;events {worker_connections 1024;}http {include /etc/nginx/mime.types;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"';access_log /var/log/nginx/access.log main;sendfile on;#tcp_nopush on;keepalive_timeout 65;#gzip on;include /etc/nginx/conf.d/*.conf;}
全局块
从配置⽂件开始到events块之间的内容,此处的配置影响nginx服务器整体的运⾏
user 设置Nginx服务的系统用户
worker_processes 工作进程数 和硬件CPU核数一致
error_log nginx的错误日志
pid nginx服务启动时候pid
woker_connections 每个进程允许最大连接数
use 内核模型select epoll
events块
events块主要影响nginx服务器与⽤户的⽹络连接。
worker_connections 1024,标识每个workderprocess⽀持的最⼤连接数为1024
HTTP块
http块是配置最频繁的部分,虚拟主机的配置,监听端⼝的配置,请求转发、反向代理、负载均衡等 http下可配置多个server
Server
server {listen 80;listen [::]:80;server_name localhost;#charset koi8-r;#access_log /var/log/nginx/host.access.log main;location / {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;#}}
配置指令语法
配置文件由指令和指令块构成
每条指令以;分号结尾,指令与参数间以空格符号分隔
指令块以大括号{}将多条指令组织在一起
include 语句允许组合多个配置文件以提升可维护性
使用# 符号添加注,提高可读性
使用$符号使用变量
部分指令的参数支持正则表达式
注释
以#开头的单行内容视为注释,注释必须是单行的,如果多行注释,那么每行都要以#开头。
简单指令:
以分号结尾,比如下面的:
user nginx;worker_processes 1;
块指令
将一组组合组合在一起,使用花括号{}进行分组,如:
events {worker_connections 1024;}
包含指令
将一些指令放置到一个单独的文件中,然后使用include指令将这些文件包含到配置文件中,就好像在把这个文件的内容填充到了这个位置一样。
include /etc/nginx/conf.d/*.conf;
指令层级
| 配置级别 | 概述 |
|---|---|
| 全局配置 | nginx核心功能 |
| events级别配置 | nginx事件机制 |
| http 级别 | nginx的http核心模块 |
| server 级别 | 一个server代表一个虚拟主机 |
| location级别 | 一个location代表一类url |
user nobody; # main 级别配置,events {use epoll; # events级别配置, nginx事件机制}http { # http 级别 nginx的http核心模块server { # server 级别,一个server代表一个虚拟主机location {# location级别, 一个location代表一类url}}}
日志设置
日志类型
- error.log、
- access.log
- log_format
语法
syntax: log_format name [escape=default | json] string...;default: log_format combined "...";context: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 logs/access.log main;
gzip 压缩
gzip压缩后页面大小可以变为原来的更小,提高用户浏览页面的访问速度
gzip on;gzip_buffers 32 4K;gzip_comp_level 2;gzip_min_length 100;gzip_types application/javascript text/css text/xml;gzip_disable "MSIE [1-6]\.";gzip_vary off;
gzip配置的常用参数
gzip on|off; #是否开启gzip
gzip_buffers 32 4K| 16 8K #缓冲(压缩在内存中缓冲几块? 每块多大?)
gzip_comp_level [1-9] #推荐6 压缩级别(级别越高,压的越小,越浪费CPU计算资源)
gzip_disable #正则匹配UA 什么样的Uri不进行gzip
gzip_min_length 200 # 开始压缩的最小长度(再小就不要压缩了,意义不在)
gzip_http_version 1.0|1.1 # 开始压缩的http协议版本(可以不设置,目前几乎全是1.1协议)
gzip_proxied # 设置请求者代理服务器,该如何缓存内容
gzip_types text/plain application/xml # 对哪些类型的文件用压缩 如txt,xml,html ,css
gzip_vary on|off # 是否传输gzip压缩标志
autoindex
autoindex on;

####日志格式
21 log_format main '$remote_addr - $remote_user [$time_local] "$request" '22 '$status $body_bytes_sent "$http_referer" '23 '"$http_user_agent" "$http_x_forwarded_for"';2425 access_log logs/access.log main;
安装goaccess
Fedora
# yum install goaccess
Arch Linux
# pacman -S goaccess
OS X / Homebrew
# brew install goaccess
启动 goaccess
# goaccess logs/access.log -o html/report.html --log-format=COMBINED --real-time-htmlWebSocket server ready to accept new client connections
增加请求地址的location
location /report.html {alias /usr/local/nginx/html/report.html;}

