nginx相关操作
来源:互联网 发布:薪酬优化方案 编辑:程序博客网 时间:2024/06/05 19:40
- nginx信号量与进程控制
- term int 迅速的关闭进程 相当于 ./sbin/nginx -s stop
- HUP 重新读取配置文件 开启新的工作进程 ./sbin/nginx -s reload
- USER1 用于日志分离时使用,重读配置文件 ./sbin/nginx -s reopen
- USER2 与 winch(关闭旧进程) 用于nginx升级时配置使用
nginx虚拟主机配置
`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;
events {
// 一般是配置nginx连接的配置
worker_connections 1024; // 此为 每个子进程的最大允许连接数
}
http {
// 此为配置http服务器的配置
include mime.types;
default_type application/octet-stream;
log_format main ‘
remoteaddr− remote_user [timelocal]" request”
access_log logs/access.log main; // main是一种定义好的格式(该日志使用main格式)(远程Ip 等等)sendfile on;
tcp_nopush on;
keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
server {
listen 80; //监听端口
server_name localhost; //服务器域名#charset koi8-r;#access_log logs/host.access.log main;location / { root html; // 位置,此为相对路径(相对于nginx根目录) 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 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;#}
nginx rewrite
1. 对url进行重写.
2. 判断语句
2.1 if 空格( 条件 ) {} 2.2 条件中使用的符号 "=" "~"(正则) ~*(不区分大小写的正则) -d-f-e (是否是文件夹,是否是文件,是否存在) 2.3 rewrite的上下午环境是server或者location,所以 可以写在server内 或者 location内
nginx gzip(压缩传输数据)
- 浏览器的请求头中, content-encoding:中 可以看到该浏览器支持的压缩方式
gzip on|off 是否开启
gzip_buffers 32 4k(一般是这个) | 16 8K 压缩在内存中缓冲几块,每块多大
gzip_comp_level 推荐6
gzip_disable 正则表达式,什么样的url不压缩
gzip_min_length 开始压缩的最小长度 200
gzip_http_version 开始压缩的http协议版本 1.0|1.1
gzip_proxied 如果请求者是代理服务器,该如何缓存
gzip_types text/plain application/xml 对那些类型进行压缩(默认就包括html)(mime.types配置文件中有各类型对应的格式)
gzip_vary on|off 是否传输gzip压缩标志注: 二进制文件就不要压缩了..
expires 使用
1. 图片(长期不易变得数据)等, 当用户在访问后,缓存在用户端.
2. Context: server, location
3. 可以使用 s m h d为单位等
4. 可以在 location中 添加 图片等, 给其缓存加时长location 定位
- 类型 location [=|~|~*|^~] patt{
}
- location = patt {} [精准匹配]
- location patt{} [一般匹配]
- location ~ patt {} [正则匹配]
- 优先级 : 优先匹配 精准匹配
- 例子 如果: url=http://域名/,会先精准匹配,然后Url为 http:域名/index.html 在走下一个location,进行跳转 (不纠结这个地方…. )
location = / {
root html
index index.html index.htm
}
location /index.html {
root html
index index.html index.htm
} - 正则表达式会覆盖掉其他
反向代理 和 负载均衡
- proxy
- upstream