Nginx系列(二)-配置文件

4 篇文章 0 订阅

Nginx系列(二)-配置文件

上一篇: Nginx系列(一)-部署

下一篇: Nginx系列(三)-Keepalive热备高可用

一、 配置结构划分

1. 下面是默认配置文件移除注释之后的样子.
2. 最外层就是 ·全局· 域,用来配置一些全局参数. ex: worker_processes 
3. 然后就是events模块域: 用来配置工作方式
4. 然后和events平级的http模块域: 
5. http模块域内部有http全局域和server域
6. server域中有server全局域和location域
#user  nginx;
worker_processes  1; #--->这个是最外层

events { # ---这里是第二层
    worker_connections  1024;
}


http { # --->这里是第二层
    include       mime.types;
    default_type  application/octet-stream;
   
    sendfile        on;
    keepalive_timeout  65;

    server { # ----> 这里是第三层
        listen       80;
        server_name  localhost;
      
        location / { # --->这里是第四层
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

二、 相关配置参数

user nginx; # 这里是用户,需要重新创建,也可以root
worker_processes  1; # 工作进程数,通常设置为CPU核心数或*2
#下面3个是日志记录组
#error_log  logs/error.log; 
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
pid        logs/nginx.pid; # 进程id

events {
    use epoll; #指定工作运行模式,linux下用epoll性能最好
    worker_connections  1024; # 工作连接数,根据自身情况调整
    # 进程的最大连接数受linux系统进程的最大打开文件数限制,再执行·ulimit -n 65536· 
    # 后worker_connections  才能生效
}

#http代理模块
http {
    include       mime.types; # 定义了nginx可以识别的类型
    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; # 日志格式和日志记录配置

    sendfile        on;# 开启文件上传和下载功能
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65; # 超时时间配置
    gzip  on;# 开启gzip压缩

	upstream nginxtest{ # 热备组
		server 127.0.0.1:8081;
		server 127.0.0.1:8082 backup;
	}
	upstream nginxtest{ # 轮询负载组
		server 127.0.0.1:8081;
		server 127.0.0.1:8082;
	}
	upstream nginxtest{ # 权重负载组
		server 127.0.0.1:8081 weight=1;
		server 127.0.0.1:8082 weight=3;
	}
	upstream nginxtest{ # ip_hash负载组,可以和weight搭配使用
		ip_hash;
		server 127.0.0.1:8081 weight=1;
		server 127.0.0.1:8082 weight=3;
	}
	upstream nginxtest{ # 最小连接数负载组,可以和weight搭配使用
		least_conn;
		server 127.0.0.1:8081 weight=1;
		server 127.0.0.1:8082 weight=3;
	}
	#限流,漏铜算法, 10M请求缓存大小, 1r/s 每秒处理1个请求
	limit_req_zone  $binary_remote_addr  zone=nginxtest:10m   rate=1r/s;
    server {
        listen       80; # 监听的端口
        server_name  localhost; # 监听的虚拟主机名称
        proxy_set_header   Host    $host;
        proxy_set_header   X-Real-IP   $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
		location / { # 这个是监听到的请求,路由匹配过程.支持正则
            root   html;
            index  index.html index.htm;
        }
        location ~*\.(gif|jpg|png|jepg|bmp)$ { # 本地缓存配置
            expires 3d; #浏览器缓存过期有效期3天
            proxy_set_header Accept-Encoding '';
            root /home/mpeg/nginx # 此目录为根目录,下面if判断就是取这个目录
            proxy_store on; #开启缓存
            proxy_store_access user:rw group:rw all:rw; #表示用户读写权限
            proxy_temp_path /home/mpeg/nginx; # 图片等本地缓存目录
            if(!-e $request_filename){
            	proxy_pass http://127.0.0.1:8081 #这里是要被代理的服务器地址
            }
        }
		location /test { 
            proxy_pass http://nginxtest;#代理转发
            # 访问请求缓存,nodelay打破限流规则来快速处理请求
            limit_req zone=nginxtest burst=5 nodelay; 
            limit_req_status 598; #自定义限流返回状态
            index  index.html index.htm;
            root   html;
        }
		location ~* \.(jpg|gif|png)${  # 动静分离
            proxy_pass http://127.0.0.1:8083;#代理转发
        }
        #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;
        }
    }
}

三、 总结

本文是博主笔记篇.没有很仔细的介绍相关配置.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值