龙空技术网

Nginx基础、部署、配置

捕猹少年小闰土 76

前言:

此时大家对“iphash算法”大约比较重视,大家都想要剖析一些“iphash算法”的相关内容。那么小编在网络上搜集了一些关于“iphash算法””的相关文章,希望朋友们能喜欢,咱们快快来学习一下吧!

Nginx的特点

擅长处理静态小文件(1M)支持高并发(支持epoll模型)占用资源少2W并发连接,10个进程,200M内存配置简单、灵活、轻量功能丰富(Web、Proxy、Cache)工作在IOS第七层(支持限速、连接数限制等)

Nginx基本功能

静态服务(图片、视频、css、js、html)基于域名/IP/端口的虚拟主机Http/Https、SMTP、POP3反向代理TCP/UDP反向代理FastCGI、uWSGI反向代理负载均衡页面缓存(CDN)支持gzip、expirseURL Rewrite路径识别基于IP、用户的访问控制支持访问速率、并发限制反向代理(适用2000WPV、并发连接1W/秒)

Nginx原理

master process

与外界通讯和工作进程管理读取nginx配置文件并验证有效性建立、绑定和关闭Socket按照配置文件生成、管理和结束工作进程nginx重启、停止、重载配置文件、平滑升级、管理日志文件

worker process

接收客户端请求,讲请求交给各个功能模块处理系统IO调用,获取相应的数据,发送相应给客户端数据缓存管理接收主进程指令,比如重启、关闭

缓存索引重建及管理进程

cache模块主要由缓存索引重建和缓存索引管理两个进程完成,缓存索引重建进程是在nginx服务启动一段时间后(默认1分钟),由主进程生成,对本地磁盘的索引文件在内存中建立元数据,包括扫描、过期更新等操作,完成后退出

模块只有使用时才加载

Nginx常用模块

核心模块

Core functionality

标准模块

ngx_http_core_modulengx_http_access_modulengx_http_fastcgi_modulengx_http_gzip_modulengx_http_rewrite_modulengx_http_upstream_modulengx_http_proxy_modulengx_http_limit_conn_modulengx_http_limit_req_modulengx_http_auth_basic_modulengx_http_log_modulengx_http_ssl_modulengx_http_status_modulengx_http_realip_module

Nginx(Proxy)支持的算法

rr 轮询

wrr 权重轮询

iphash hash算法(解决session命中)

urlhash hash算法

fair 动态算法(响应时间最短)

Nginx部署

安装依赖

yum install openssl-devel pcre-devel pcre gcc zlib -y

pcre 正则处理需要gcc 编译需要zlib 压缩需要openssl 安全链接需要

编译安装

useradd nginx -s /sbin/nologin -M wget  xzf nginx-1.12.2.tar.gz && cd nginx-1.12.2./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_geoip_module --with-stream --with-http_stub_status_module --with-http_realip_module make && make install 

基本操作

#启动/usr/local/nginx/sbin/nginx#停止/usr/local/nginx/sbin/nginx -s stop# 检查语法/usr/local/nginx/sbin/nginx -t #重启/usr/local/nginx/sbin/nginx -s #重启原理:生成基于新配置的线程,新请求转发到新线程,旧线程处理完成后停止。

主配置文件 nginx.conf

user nginx nginx;# 设置所有worker进程可以打开的最大文件句柄数worker_rlimit_nofile 65535;worker_processes auto;worker_cpu_affinity auto;events { # 让多个worker进程轮询相应新请求 accept_mutex on; # 限制每个worker进程的并发连接数 worker_connections 10240; use epoll;}http { include mime.types; default_type application/octet-stream; # 开启高效文件传输模式 sendfile on; tcp_nodelay on; tcp_nopush on; # 设置客户端连接保持会话的超时时间 keepalive_timeout 10; # 设置客户端请求头读取超时时间 client_header_timeout 15; # 设置客户端请求主体读取超时时间 client_body_timeout 15; # 指定响应客户端的超时时间 send_timeout 15; # 设置最大的容许的客户端请求主体大小 client_max_body_size 10M; # 将http请求响应头里的nginx版本号信息隐藏 server_tokens off; # 开启压缩 gzip on; # 设置容许压缩的页面最小字节数 gzip_min_length 1k; # 申请4个单位为32K的内存作为压缩结果流缓存 gzip_buffers 4 32k; # 压缩的版本 gzip_http_version 1.1; # 由于IE6对Gzip压缩效果不好,建议在IE6的环境下关闭压缩功能 gzip_disable "MSIE[1-6]"; # 压缩比率 gzip_comp_level 3; # 指定压缩的类型 gzip_types text/plain text/css text/xml application/javascript application/x-javascript; # 设置从web服务器传给缓存服务器的时候不被解压。只有传送到用户后才解压缩 gzip_vary on; # 设置为Json格式,方便ELK日志分析软件处理 log_format json_main '{"@timestamp":"$time_iso8601",' '"remote_addr":"$server_addr",' '"remote_user":"$remote_user",' '"request_time":"$request_time",' '"request":"$request",' '"body_bytes_sent":"$body_bytes_sent",' '"request_method":"$request_method",' '"upstream_response_time":"$upstream_response_time",' '"upstream_addr":"$upstream_addr",' '"url":"$uri",' '"http_x_forwarded_for":"$http_x_forwarded_for",' '"http_referrer":"$http_referer",' '"status":"$status"}'; access_log logs/access.log json_main;  error_log logs/error.log error;}

扩展配置文件(便于管理) vhost.conf

server { listen 8000; server_name ; root /data/webroot/; set_real_ip_from 10.0.0.0/16; set_real_ip_from 192.168.0.0/16; real_ip_header X-Forwarded-For; real_ip_recursive on; location / { index index.html; } # 设置资源缓存时间 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 3650d; } # 设置资源缓存时间 location ~ .*\.(js|css)?$ { expires 30d; } # 设置状态页访问信息 location /ngx_status { stub_status on; access_log off; allow 192.168.0.0/16; deny all; } access_log logs/lego_access.log json_main;}

扩展配置 proxy.conf

 # 当后端web服务器上也配置多个虚拟主机时,需要用该header来区分反向代理哪个主机名。 proxy_set_header Host $host; # 通过$remote_addr变量获取前端客户真实IP地址。 proxy_set_header X-Real-IP $remote_addr; # 通过$remote_addr变量获取前端客户真实IP地址。 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 设置缓冲区 proxy_buffering on; # 设置后端服务器的响应头大小,是响应头的缓冲区。 proxy_buffer_size 4k; # 设置缓冲区的数量和大小,nginx从代理的后端服务器获取的响应信息会放置到缓冲区。 proxy_buffers 8 32k; #此设置表示nginx会在没有完全读完后端响应的时候就开始向客户端传送数据,所以它会划出一部分缓冲区来专门向客户端传送数据。建议为proxy_buffers中单个缓冲区大小的2倍) proxy_busy_buffers_size 64K; # 指定当响应内容大于proxy_buffers指定的缓冲区时, 写入硬盘的临时文件的大小。 proxy_max_temp_file_size 1024m; # 一次访问能写入的临时文件的大小,默认是proxy_buffer_size和proxy_buffers中设置的缓冲区大小的2倍。 proxy_temp_file_write_size 128k; #  proxy_request_buffering on; #  proxy_ignore_headers Set-Cookie; # 表示与后端服务器连接的超时时间 proxy_connect_timeout 60s; # 表示后端服务器的数据回传时间 proxy_send_timeout 300; # 设置nginx从代理的后端服务器获取信息的时间 proxy_read_timeout 300s;

标签: #iphash算法