Nginx基础

概念

Nginx是一个异步框架的Web服务器,也可以作为反向代理,负载均衡器和HTTP缓存。

特点
  • 单次请求会得到更快的响应
  • Nginx 是基于模块化设计,由多个耦合度极低的模块组成,因此具有很高的扩展性
  • 低内存消耗,Nginx 支持的并发连接上限取决于内存
  • 热部署
  • 最自由的 BSD 许可协议

配置文件

  • 默认时间单位秒s
  • 默认度量单位字节byte
结构

简单指令 指令名 指令值;
块级指令 {},如果包含简单指令则称为上下文
处在配置文件里,块级指令之外的环境称之为主上下文

变量

nginx可以使用变量简化配置与提高配置的灵活性,变量统一以 $ 开头

自定义变量

语法:set $var value
nginx中变量全局可见,但变量的作用范围不是全局的,在一个块中定义变量$a,在另外一个快中引用$a,nginx不会报错但另一个块中$a值为空。

内置变量

$remote_addr 客户端地址
$remote_port 客户端端口
$server_addr 服务器的地址
$server_port 服务器的端口号
$server_name 服务器名称
$server_protocol 请求使用的协议如HTTP/1.1
 
$request_uri 客户端请求参数的原始URI无法修改
$uri 请求中的当前URI,可修改
$args GET请求中的参数
$arg_PARAMETER GET请求中变量名为PARAMETER的参数值
$is_args 如果$args设置,值为"?",否则为""
$cookie_COOKIE cookie中名为COOKIE的值
 
$content_length 请求头中的Content-length值
$content_type 请求头中的Content-type值

正则匹配规则

~ 区分大小写匹配    !~ 区分大小写不匹配
~* 不区分大小写匹配   !~* 不区分大小写不匹配
!-d dir 匹配目录是否存在
!-f filename 匹配文件是否存在
!-e dir/file 匹配文件或目录是否存在
!-x name 匹配文件是否可执行

配置内容理解

#user  nobody;  // 配置用户或者组
worker_processes  1; // 指定了Nginx要开启的进程数,auto根据cpu核数决定

#error_log  logs/error.log;  // 错误日志存放路径及级别
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;// pid文件,记录nginx的进程号
#worker_rlimit_nofile 65535;// 一个nginx进程打开的最多文件描述符数目

// nginx工作模式的配置
events {
	accept_mutex on;   // 添加互斥锁,防止惊群现象发生,默认为on
    multi_accept on;  //设置一个进程是否同时接受多个网络连接,默认为off
	use epoll;// 事件模型
    worker_connections  1024;// 单个进程最大连接数
}


http {
    include       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  logs/access.log  main; // 使用main日志格式来记录服务日志

    sendfile        on; // 开启高效传输模式
    #tcp_nopush     on;// 防止网络阻塞

    #keepalive_timeout  0;
    keepalive_timeout  65;// 长连接超时时间

    #gzip  on;// 开启gzip压缩输出
    error_page 404 https://www.xx.com;
    
    // 常用配置
    client_body_timeout 60;// 请求体超时时间
    client_body_buffer_size 128k;// 请求体缓冲区大小
    client_max_body_size 10m;// 最大请求体大小

// 负载均衡的配置
#upstream abc.com{ // 轮询
#	server localhost:8080  max_fails=3 fail_timeout=20s fail_time=60s;// 超时内最大失败次数、超时时间、服务器会被认为停机的时间长度
#   server localhost:8081 backup;// 标记为备用服务器
#   server localhost:8082 down;// 标记为服务器永久停机
#}
#upstream abc.com{ // 权重,此策略可以与least_conn和ip_hash结合使用。
#	server localhost:8080  weight=2;
#   server localhost:8081;// 默认weight为1
#}
#upstream abc.com{ // ip_hash
#	ip_hash;
#	server localhost:8080  weight=2;
#   server localhost:8081;// 默认weight为1
#}
#upstream abc.com{ // least_conn
#	least_conn;
#	server localhost:8080  weight=2;
#   server localhost:8081;// 默认weight为1
#}
#upstream abc.com{ // url_hash
#	hash $request_uri;
#	server localhost:8080;
#   server localhost:8081;
#}
#upstream abc.com{ // fair
#	fair;
#	server localhost:8080;
#   server localhost:8081;
#}
	// 虚拟主机配置
    server {
        listen       80;// 监听端口80
        server_name  localhost;// 访问域名

        #charset koi8-r;// 编码

        #access_log  logs/host.access.log  main;// 日志记录
        #keepalive_requests 120;  // 单连接请求上限次数
    	#root html;// 定义网站根目录位置
    	#index xx.html //访问主页

		// 重写
		#if($http_user_agent ~ Firefox){
		#	rewrite ^(.*)$  /firefox/$1 break; 
		#}
		#rewrite 后若为 break,重写后直接使用当前资源
		#rewrite 后若为 last,重写后,马上发起一个新的请求
		#rewrite 后若为 redirect,则返回302临时重定向
		#rewrite 后若为 permanent,则进行301永久重定向
		
		// 对url进行匹配,=精确匹配,^~ 如果为最佳匹配则直接采用
        location / {
        	// 访问根路径
            root   html;
            // 主页
            index  index.html index.htm;
        }

		location /test {
		    proxy_pass http://127.0.0.1:8090;// 代理设置
		    // 添加http响应头
		    add_header Access-Control-Allow-Origin $http_origin;
		    add_header Access-Control-Allow-Headers Origin,X-Requested-With,Content-Type,Accept;
		    add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
		    add_header Access-Control-Allow-Credentials true;
		    proxy_connect_timeout 6000s;// 代理连接超时
		    proxy_send_timeout 6000s;// 代理发送超时
		    proxy_read_timeout 6000s;// 代理读取超时
		    proxy_redirect off;// 关闭代理重定向,修改从被代理服务器传来的应答头中的"Location"和"Refresh"字段
		    // 设置代理请求头
		    proxy_set_header Host $host;
		    proxy_set_header X-Real-IP $remote_addr;
		    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
		// 错误页面
        #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;
        }

        // URL以.php结尾则自动转交给127.0.0.1
        #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;
        #}

        // 设置禁止或允许访问路径
        #location ~ /\.ht {
        #    allow    127.0.0.1;
        #    deny  all;
        #}
    }

}

负载均衡策略

nginx中有7中负载均衡策略

  • 轮询,默认方式,每个请求会按时间顺序逐一分配到不同的服务器
  • 随机,随机访问列表中的服务器
  • 权重,通过为不同服务器设置不同权重,权重越高被访问次数越多,默认权重1
  • least-conn,最少连接数
  • fair,按响应时长,需要在Nginx编译时加入nginx-upstream-fair模块
  • url_hash,按照url的hash作为标示分配
  • ip_hash,按照客户端ip的hash来作为标示分配

当动态添加服务器后,通过hash算法分配的服务器可能不均匀,这里引入一致性hash算法和虚拟节点来平衡。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值