目录 /etc/nginx nginx配置文件目录
/etc/nginx/nginx.conf nginx的主配置文件
/etc/nginx/conf.d/ nginx的子配置文件的目录
/etc/ logrotate.d/nginx 日志切割
/usr/lib/systemd/system/nginx.service nginx的systemctl配置文件
/usr/sbin/nginx nginx管理命令
/usr/share/nginx/html nginx默认的站点目录,网站的根目录
/var/log/nginx nginx日志文件的目录
配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; 周期,周期越小,系统调用开销越大,周期越大,日志时间精度越低 include /usr/share/nginx/modules/*.conf; events { worker_connections 1024 ; } #===========================================================================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 /var/log/nginx/access.log main ; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65 ; types_hash_max_size 4096 ; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; server { listen 80 ; listen [::]:80 ; server_name localhost; client_max_body_size 1024 M; root /usr/share/nginx/html; index index.html include /etc/nginx/default .d/*.conf; location / { index index.html ; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } =============================================================================== ==================================== HTTPS 区 ================================= ================================================================================= }
Nginx 虚拟主机 虚拟主机类型 基于域名的虚拟主机 不同的域名访问不同的虚拟主机 常用
基于端口的虚拟主机 不同端口访问不同的虚拟主机 给网站增加特殊端口,限制用户访问
基于ip的虚拟主机 不同的ip地址访问不同的虚拟主机 用来增强限制,限制用户只能通过内网访问,或vpn访问
基于域名的虚拟主机 配置子系统配置 game.codfish.cn.conf
1 2 3 4 5 6 7 8 server { listen 80 ; server_name game.codfish.cn; root /app/code/game; location / { index index.html; } }
1 2 3 4 5 6 7 8 mkdir -p /app/code/{game,live,www}for name in game live www do echo $name .codfish.cn > /app/code/$name /index.htmldone head /app/code/{game,live,www}/index.html
基于端口的虚拟主机 1 2 3 4 5 6 7 8 server { listen 81 ; server_name game.codfish.cn; root /app/code/game; location / { index index.html; } }
基于IP的虚拟主机 1 2 3 4 5 6 7 8 server { listen localhost:81 ; server_name game.codfish.cn; root /app/code/game; location / { index index.html; } }
异常访问 当用户访问不存在的域名或者使用IP访问网络时,Nginx 会按字母顺序选择 最匹配的server 进行响应
所以 当域名不存在/或IP访问时 匹配字母靠前的端口一致的 server 进行响应。
或者可以在server定义中 定义一个 默认的server来进行处理
1 2 3 4 5 6 7 8 server { listen 80 default_server ; server_name some_url ; root /app/code/codfish ; location / { index index.html; } }
或者定义一个不存在的域名来处于异常访问
1 2 3 4 5 6 7 server { listen 80 default_server server_name _ charset utf8 default_type text/plain return 200 "message" }
Nginx 日志 log_format main “$remote_addr - $remote_user [$time_local] “ $request” ‘
1 2 3 ‘$status $body_bytes_sent “$http_referer” ’ ‘”http_user_agent” “$http_x_forwarded_for” ’
$remote_addr 客户端ip地址
$remote_user 用户名
$time_local 时间
$request 请求报文的起始行
$status 状态码
$body_bytes_sent 响应消息体长度
$http_referer 间接进行本次请求的域名
$http_user_agent 用户使用的访问代理
$http_x_forwarded_for 使用代理用于记录用户真实的ip地址
access_log 语法 access_log 地址/文件名 格式
access_log path [format [buffer=”size”]] [gzip[=”level”]][flush=”time”][if=”condition”]】‘
buffer 缓存用户访问日志
gzip 日志是否要压缩
flush 多久向磁盘中写入
error_log 语法 error_log 地址/文件名 级别
error_log path level
日志切割 1 系统定时任务 + logroate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 /var/log/nginx/ *.log { create 0640 nginx root daily rotate 10 missingok notifempty compress delaycompress sharedscripts postrotate /bin/kill -USR1 'cat /run/nginx.pid 2>/dev/null ' 2>/dev/null || true endscript }
2 自己实现日志切割
实现上述配置文件,使用logratate (-f) 进行文件切割