Nginx:Nginx概要

 

简介

nginx是俄罗斯开源的HTTP和代理服务,也能够做邮件服务器。javascript

核心功能:php

一、正向代理:客户机的请求先到达nginx,再由nginx代理访问互联网资源css

二、反向代理:客户机请求互联网,到达nginx后转发给相应的服务器html

三、负载均衡:支持轮询、加权轮询、IPhash等负载均衡算法;支付服务器热备、冷备前端

四、Web缓存:能够对不一样文件作不一样缓存,相关组件FastCGI_Cache、ngx_cache_purgejava

 

源码:https://trac.nginx.org/nginx/browsernginx

官网:http://www.nginx.org/c++

官网文档:http://nginx.org/en/docs/beginners_guide.htmlweb

 

CentOS7安装Nginx

一、安装编译环境

#yum update # yum -y install gcc gcc-c++ autoconf automake libtool make cmake # yum -y install zlib zlib-devel openssl openssl-devel pcre-devel

 

 

yum -y install wget

 

 

cd  /usr/local/
mkdir software cd /usr/local/software //下载
wget wget http://nginx.org/download/nginx-1.13.0.tar.gz //解压
 tar -zxvf nginx-1.13.0.tar.gz //编译
./configure --prefix=/usr/local/nginx

 

make & make install

 

配置见后文算法

 

启动

/usr/local/nginx/sbin/nginx 
/usr/local/webserver/nginx/sbin/nginx -s reload # 从新载入配置文件 /usr/local/webserver/nginx/sbin/nginx -s reopen # 重启 Nginx /usr/local/webserver/nginx/sbin/nginx -s stop # 中止 Nginx

 

 

 

 二、配置

1、配置概要

一、配置每行分号结束

二、全局模块:直接属性配置的,全局设置;如:服务器用户组、进程、pid、日志路径等等

三、events{}块:配置网络相关内容;如网络序列化,最大链接等

四、http{}块,内部包含upstream块,多个server{}子块;配置主要负责文件类型与扩展映射、日志内容格式定义、文件传输、链接超时时间,热备等

五、upstream块,http内子块,主要作热备做用

六、server{}块,是http内的子块。主要配置主机相关参数;单链接请求上限次数,监听端口,服务器地址等;内涵多个location块

七、location{}块,server内的子块。主要配置请求路由。正则匹配

########### 每一个指令必须有分号结束。################# #user administrator administrators; #配置用户或者组,默认为nobody nobody。 #worker_processes 2; #容许生成的进程数,默认为1 #pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址 error_log log/error.log debug;  #制定日志路径,级别。这个设置能够放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg events { accept_mutex on; #设置网路链接序列化,防止惊群现象发生,默认为on multi_accept on; #设置一个进程是否同时接受多个网络链接,默认为off #use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport worker_connections 1024; #最大链接数,默认为512 } http { include mime.types; #文件扩展名与文件类型映射表 default_type application/octet-stream; #默认文件类型,默认为text/plain #access_log off; #取消服务日志 log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式 access_log log/access.log myFormat; #combined为日志格式的默认值 sendfile on; #容许sendfile方式传输文件,默认为off,能够在http块,server块,location块。 sendfile_max_chunk 100k; #每一个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。 keepalive_timeout 65; #链接超时时间,默认为75s,能够在http,server,location块。 upstream mysvr { server 127.0.0.1:7878; server 192.168.10.121:3333 backup; #热备 } error_page 404 https://www.baidu.com; #错误页
 server { keepalive_requests 120; #单链接请求上限次数。 listen 4545; #监听端口 server_name 127.0.0.1; #监听地址 location ~*^.+$ {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。 #root path; #根目录 #index vv.txt; #设置默认页 proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
           deny 127.0.0.1; #拒绝的ip allow 172.18.5.54; #容许的ip } } }
配置

 

一、配置关键字

$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址;

$remote_user :用来记录客户端用户名称;

$time_local : 用来记录访问时间与时区;

$request : 用来记录请求的url与http协议;

$status : 用来记录请求状态;成功是200, 

$body_bytes_s ent :记录发送给客户端文件主体内容大小;

$http_referer :用来记录从那个页面连接访问过来的;

$http_user_agent :记录客户端浏览器的相关信息;

二、惊群现象:一个网路链接到来,多个睡眠的进程被同事叫醒,但只有一个进程能得到连接,这样会影响系统性能。

 

2、代理配置

反向代理配置:

upstream mysvr { server 192.168.10.121:3333; server 192.168.10.122:3333; } server { .... location ~*^.+$ { proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表 } 或 upstream mysvr { server http://192.168.10.121:3333; server http://192.168.10.122:3333; } server { .... location ~*^.+$ { proxy_pass mysvr; #请求转向mysvr 定义的服务器列表 }

 


一、热备:若是你有2台服务器,当一台服务器发生事故时,才启用backup服务器给提供服务。例如:AAAAAA——>A挂掉,BBBBBBBBBBBBBB.....

upstream mysvr { server 127.0.0.1:7878; server 192.168.10.121:3333 backup; #热备 }

 


二、轮询:nginx默认就是轮询其权重都默认为1,服务器处理请求的顺序:ABABABABAB....

upstream mysvr { server 127.0.0.1:7878; server 192.168.10.121:3333; }

 


三、加权轮询:跟据配置的权重的大小而分发给不一样服务器不一样数量的请求。若是不设置,则默认为1。下面服务器的请求顺序为:ABBABBABBABBABB....

upstream mysvr { server 127.0.0.1:7878 weight=1; server 192.168.10.121:3333 weight=2; }

 


四、ip_hash:nginx会让相同的客户端ip请求相同的服务器。

upstream mysvr { server 127.0.0.1:7878; server 192.168.10.121:3333; ip_hash; }

 


五、其余

down,表示当前的server暂时不参与负载均衡。

backup,预留的备份机器。当其余全部的非backup机器出现故障或者忙的时候,才会请求backup机器,所以这台机器的压力最轻。

max_fails,容许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误。

fail_timeout,在经历了max_fails次失败后,暂停服务的时间。max_fails能够和fail_timeout一块儿使用。

upstream mysvr { server 127.0.0.1:7878 weight=2 max_fails=2 fail_timeout=2; server 192.168.10.121:3333 weight=1 max_fails=2 fail_timeout=1; }

 

 配置详细

 

user nobody; #工做进程数,建议设置为CPU的总核数 worker_processes 4; #全局错误日志定义类型,日志等级从低到高依次为: #debug | info | notice | warn | error | crit error_log /logs/nginx/error.log info; #记录主进程ID的文件 pid /logs/nginx/nginx.pid; #指定进程能够打开的最大描述符数目 worker_rlimit_nofile 204800; events { use epoll; #单个进程最大链接数:链接数*进程数 65535 worker_connections 204800; } #设定http服务器,利用它的反向代理功能提供负载均衡支持 http { #文件扩展名与文件类型映射表 include mime.types; #默认文件类型 default_type application/octet-stream; charset utf-8; server_names_hash_bucket_size 128; client_header_buffer_size 2k; large_client_header_buffers 4 4k; client_max_body_size 20m; #为打开文件指定缓存 open_file_cache max=65535 inactive=60s; #这个是指多长时间检查一次缓存的有效信息。 #语法:open_file_cache_valid time 默认值:open_file_cache_valid 60 使用字段:http, server, location 这个指令指定了什么时候须要检查open_file_cache中缓存项目的有效信息. open_file_cache_valid 80s; #open_file_cache指令中的inactive参数时间内文件的最少使用次数,若是超过这个数字,文件描述符一直是在缓存中打开的,如上例,若是有一个文件在inactive时间内一次没被使用,它将被移除。 #语法:open_file_cache_min_uses number 默认值:open_file_cache_min_uses 1 使用字段:http, server, location 这个指令指定了在open_file_cache指令无效的参数中必定的时间范围内能够使用的最小文件数,若是使用更大的值,文件描述符在cache中老是打开状态. open_file_cache_min_uses 1; #语法:open_file_cache_errors on | off 默认值:open_file_cache_errors off 使用字段:http, server, location 这个指令指定是否在搜索一个文件是记录cache错误. open_file_cache_errors on; #日志格式 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'
                      '$request_body'; #access log 记录了哪些用户,哪些页面以及用户浏览器、ip和其余的访问信息 access_log /logs/nginx/access.log main; #隐藏ngnix版本号 server_tokens off; #sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。若是用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,下降系统uptime。 sendfile on; #长链接超时时间,单位是秒 keepalive_timeout 120; #gzip模块设置 gzip on; #开启gzip压缩输出 gzip_min_length 1k; #最小压缩文件大小 gzip_buffers 4 16k; #压缩缓冲区 gzip_http_version 1.0;    #压缩版本(默认1.1,前端若是是squid2.5请使用1.0) gzip_comp_level 2; #压缩等级 gzip_types text/plain application/x-javascript text/css application/xml; #压缩类型,默认就已经包含textml,因此下面就不用再写了,写上去也不会有问题,可是会有一个warn。 gzip_vary on; server { listen 80; server_name xxxxx; access_log /mydata/nginx_log/xx.log main; location / { proxy_pass http://localhost:8080;
 proxy_set_header Host $host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } #php-fpm server { listen 80; server_name xx; access_log /mydata/nginx_log/xx.log main; location / { root /xx; index index.php; } location ~ \.php$ { root /xx; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } }

 

 

 

资料:

 

博客

https://www.cnblogs.com/mrlinfeng/p/6146866.html

https://www.cnblogs.com/knowledgesea/p/5199046.html

http://www.javashuo.com/article/p-xkijqyyb-hr.html

相关文章
相关标签/搜索