配置文件

默认的配置文件就在/usr/local/nginx/conf/nginx.conf
默认的首页index页面路径 /usr/share/nginx/html
查看配置文件路径

  1. # nginx -t
  2. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  3. nginx: configuration file /etc/nginx/nginx.conf test is successful


  1. user nginx;
  2. worker_processes 1;
  3. error_log /var/log/nginx/error.log warn;
  4. pid /var/run/nginx.pid;
  5. events {
  6. worker_connections 1024;
  7. }
  8. http {
  9. include /etc/nginx/mime.types;
  10. default_type application/octet-stream;
  11. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  12. '$status $body_bytes_sent "$http_referer" '
  13. '"$http_user_agent" "$http_x_forwarded_for"';
  14. access_log /var/log/nginx/access.log main;
  15. sendfile on;
  16. #tcp_nopush on;
  17. keepalive_timeout 65;
  18. #gzip on;
  19. include /etc/nginx/conf.d/*.conf;
  20. }

全局块

从配置⽂件开始到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

  1. server {
  2. listen 80;
  3. listen [::]:80;
  4. server_name localhost;
  5. #charset koi8-r;
  6. #access_log /var/log/nginx/host.access.log main;
  7. location / {
  8. root /usr/share/nginx/html;
  9. index index.html index.htm;
  10. }
  11. #error_page 404 /404.html;
  12. # redirect server error pages to the static page /50x.html
  13. #
  14. error_page 500 502 503 504 /50x.html;
  15. location = /50x.html {
  16. root /usr/share/nginx/html;
  17. }
  18. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  19. #
  20. #location ~ \.php$ {
  21. # proxy_pass http://127.0.0.1;
  22. #}
  23. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  24. #
  25. #location ~ \.php$ {
  26. # root html;
  27. # fastcgi_pass 127.0.0.1:9000;
  28. # fastcgi_index index.php;
  29. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  30. # include fastcgi_params;
  31. #}
  32. # deny access to .htaccess files, if Apache's document root
  33. # concurs with nginx's one
  34. #
  35. #location ~ /\.ht {
  36. # deny all;
  37. #}
  38. }

配置指令语法

配置文件由指令和指令块构成
每条指令以;分号结尾,指令与参数间以空格符号分隔
指令块以大括号{}将多条指令组织在一起
include 语句允许组合多个配置文件以提升可维护性
使用# 符号添加注,提高可读性
使用$符号使用变量
部分指令的参数支持正则表达式

注释

#开头的单行内容视为注释,注释必须是单行的,如果多行注释,那么每行都要以#开头。

简单指令:

以分号结尾,比如下面的:

  1. user nginx;
  2. worker_processes 1;

块指令

将一组组合组合在一起,使用花括号{}进行分组,如:

  1. events {
  2. worker_connections 1024;
  3. }

包含指令

将一些指令放置到一个单独的文件中,然后使用include指令将这些文件包含到配置文件中,就好像在把这个文件的内容填充到了这个位置一样。

  1. include /etc/nginx/conf.d/*.conf;

指令层级

配置级别 概述
全局配置 nginx核心功能
events级别配置 nginx事件机制
http 级别 nginx的http核心模块
server 级别 一个server代表一个虚拟主机
location级别 一个location代表一类url
  1. user nobody; # main 级别配置,
  2. events {
  3. use epoll; # events级别配置, nginx事件机制
  4. }
  5. http { # http 级别 nginx的http核心模块
  6. server { # server 级别,一个server代表一个虚拟主机
  7. location {
  8. # location级别, 一个location代表一类url
  9. }
  10. }
  11. }

日志设置

日志类型

  • error.log、
  • access.log
  • log_format

语法

  1. syntax: log_format name [escape=default | json] string...;
  2. default: log_format combined "...";
  3. context:http
  1. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  2. '$status $body_bytes_sent "$http_referer" '
  3. '"$http_user_agent" "$http_x_forwarded_for"';
  4. access_log logs/access.log main;

gzip 压缩

gzip压缩后页面大小可以变为原来的更小,提高用户浏览页面的访问速度

  1. gzip on;
  2. gzip_buffers 32 4K;
  3. gzip_comp_level 2;
  4. gzip_min_length 100;
  5. gzip_types application/javascript text/css text/xml;
  6. gzip_disable "MSIE [1-6]\.";
  7. 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

  1. autoindex on;

image.png
####日志格式

  1. 21 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  2. 22 '$status $body_bytes_sent "$http_referer" '
  3. 23 '"$http_user_agent" "$http_x_forwarded_for"';
  4. 24
  5. 25 access_log logs/access.log main;

安装goaccess

Fedora

  1. # yum install goaccess

Arch Linux

  1. # pacman -S goaccess

OS X / Homebrew

  1. # brew install goaccess

启动 goaccess

  1. # goaccess logs/access.log -o html/report.html --log-format=COMBINED --real-time-html
  2. WebSocket server ready to accept new client connections

增加请求地址的location

  1. location /report.html {
  2. alias /usr/local/nginx/html/report.html;
  3. }

image.png