nginx安装目录
# Nginx日志轮转,用于 logrotate服务的日志切割
/etc/logrotate.d/nginx
# Nginx主配置文件
/etc/nginx
/etc/nginx/nginx.conf
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
# cgi配置相关,fastcgi配置
/etc/nginx/fastcgi_params
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
# 编码转换映射转化文件
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/win-utf
# 设置http协议的Content-Type与扩展名对应关系
/etc/nginx/mime.types
# 用于配置出系统守护进程管理器管理方式
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
# Nginx的模块目录
/etc/nginx/modules
/usr/lib64/nginx/modules
# Nginx服务启动管理的终端命令
/usr/sbin/nginx
/usr/sbin/nginx-debug
# Nginx的手册和帮助文件
/usr/share/doc/nginx-1.12.2
/usr/share/doc/nginx-1.12.2/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
# Nginx的缓存目录
/var/cache/nginx
# Nginx的日志目录
/var/log/nginx
nginx变量
HTTP变量:
比如 $http_user_agent,$http_cookie等表示HTTP请求信息的变量。
内置变量:
$args, 请求中的参数;
$content_length, HTTP请求信息里的"Content-Length";
$content_type, 请求信息里的"Content-Type";
$document_root, 针对当前请求的根路径设置值;
$document_uri, 与$uri相同;
$host, 请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名;
$limit_rate, 对连接速率的限制;
$request_method, 请求的方法,比如"GET"、"POST"等;
$remote_addr, 客户端地址;
$remote_port, 客户端端口号;
$remote_user, 客户端用户名,认证用;
$request_filename, 当前请求的文件路径名
$request_body_file, ??
$request_uri, 请求的URI,带参数;
$query_string, 与$args相同;
$scheme, 所用的协议,比如http或者是https,比如rewrite ^(.+)$ $scheme://example.com$1 redirect;
$server_protocol, 请求的协议版本,"HTTP/1.0"或"HTTP/1.1";
$server_addr, 服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费);
$server_name, 请求到达的服务器名;
$server_port, 请求到达的服务器端口号;
$uri, 请求的URI,可能和最初的值有不同,比如经过重定向之类的。
nginx模块
http-auth模块
- linux操作
#安装htpasswd模块
yum install httpd
#到指定的目录
cd /etc/nginx
#生成auth_conf文件,用户名jehad
htpasswd -c ./auth_conf jehad
#在文件中添加一个用户,不覆盖原来的
htpasswd ./auth_conf jhcoder
- 下面是nginx配置
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
}
#匹配admin页面 只允许某ip访问(也可以反过来)原理是使用remote_addr,使用代理时准确性不高,使用frp也不行
# location ~ ^/admin.html {
# root /home/project/nginx-code;
# deny all;
# allow 113.87.88.143;
# }
location ~ ^/admin.html {
root /home/project/nginx-code;
auth_basic ".......输入密码哇.........";
auth_basic_user_file /etc/nginx/auth_conf;
}
}
- 模块的局限性
1、用户信息和系统不能同步 2、操作管理机械
- 解决方法
1、nginx+lua高效验证 2、nginx+ladp nginx-auth-ladp模块
常见的nginx中间件架构
1、静态资源web服务(文件读取sendfile、tcp_nopush、tcp_nodelay、压缩,预读gzip模块)
2、代理服务
3、负载均衡调度器SLB
4、动态缓存(添加cache-control、expires头)
静态资源处理
静态资源访问、压缩
- nginx配置
server{
listen 80;
server_name localhost;
sendfile on;
access_log /var/log/nginx/log/static_access.log main;
#匹配图片
location ~ .*\.(jpeg|jpg|gif|png)$ {
#gzip on;
#gzip_http_version 1.1;
#gzip_comp_level 2;
#gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
root /home/project/nginx-code/images;
}
#匹配文本
location ~ .*\.(txt|xml)$ {
gzip on;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
root /home/project/nginx-code/doc;
}
#相当于预先压缩文件,这个文件夹下的可以由其他脚本生成压缩文件访问
location ~ ^/download$ {
gzip_static on;
tcp_nopush on;
root /home/project/nginx-code;
}
}
- linux命令
#测试配置文件
nginx -tc /etc/nginx/nginx.conf
#重新加载配置文件
nginx -s reload -c /etc/nginx/nginx.conf
结果
开启压缩前:
开启压缩后:
其实gzip对于图片的压缩效率可能没那么高,大家可以试下对文件的压缩,会更高一些
总结
开启压缩主要是为了减少网络传输消耗,浏览器会对压缩的文件进行解压缩,这个过程要快很多。
nginx缓存配置
- nginx配置
server{
listen 80;
server_name localhost;
location ~ .*\.(html|htm)$ {
#缓存开关
expires 24h;
root /home/project/nginx-code;
}
}
结果
没加缓存之前的请求:
加了缓存第一次请求:
加了缓存第二次请求:
结论
- 没加缓存前服务端每次都会响应200。
- 加了缓存
2.1 如果cache-control生效,那么浏览器在这段时间内都不会向服务器请求,直接返回数据。
2.2 如果cache-control不生效,即客户端强制和服务端确定是否过期校验(比如chrome设置max-age=0),确认成功后,服务端返回304,客户端再读取缓存。
跨域配置
server{
listen 80;
server_name localhost;
location ~ .*\.(html|htm)$ {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
root /home/project/nginx-code;
}
}