Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”,是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器。Nginx是由俄罗斯人 Igor Sysoev为俄罗斯访问量第二的 Rambler.ru站点开发的,它已经在该站点运行超过两年半了。Igor Sysoev在创建的项目时,使用基于BSD许可。其特色是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。javascript
apache 仍然是目前的主流,拥有丰富的特性,成熟的技术和开发社区php
二者最核心的区别在于 apache 是同步多进程模型,一个链接对应一个进程,而 nginx 是异步的,多个链接(万级别)能够对应一个进程css
通常来讲,须要性能的 web 服务,用 nginx 。若是不须要性能只求稳定,更考虑 apache ,后者的各类功能模块实现得比前者,例如 ssl 的模块就比前者好,可配置项多。epoll(freebsd 上是 kqueue ) 网络 IO 模型是 nginx 处理性能高的根本理由,但并非全部的状况下都是 epoll 大获全胜的,若是自己提供静态服务的就只有寥寥几个文件,apache 的 select 模型或许比 epoll 更高性能。固然,这只是根据网络 IO 模型的原理做的一个假设,真正的应用仍是须要实测了再说的。html
更为通用的方案是,前端 nginx 抗并发,后端 apache 集群,配合起来会更好。前端
该文件在 /etc/nginx/nginx.confjava
#nginx进程,通常设置为和cpu核数同样 worker_processes 4; #错误日志存放目录 error_log /data1/logs/error.log crit; #运行用户,默认便是nginx,可不设置 user nginx #进程pid存放位置 pid /application/nginx/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. #最大文件打开数(链接),可设置为系统优化后的ulimit -HSn的结果 worker_rlimit_nofile 51200; cpu亲和力配置,让不一样的进程使用不一样的cpu worker_cpu_affinity 0001 0010 0100 1000 0001 00100100 1000; #工做模式及链接数上限 events { use epoll; #epoll是多路复用IO(I/O Multiplexing)中的一种方式,可是仅用于linux2.6以上内核,能够大大提升nginx的性能 worker_connections 1024; #;单个后台worker process进程的最大并发连接数 } ################################################### http { include mime.types; #文件扩展名与类型映射表 default_type application/octet-stream; #默认文件类型 #隐藏响应header和错误通知中的版本号 server_tokens off; #开启高效传输模式 sendfile on; ------------------------------------------------------------------------------------------------- #激活tcp_nopush参数能够容许把httpresponse header和文件的开始放在一个文件里发布, 积极的做用是减小网络报文段的数量 tcp_nopush on; #激活tcp_nodelay,内核会等待将更多的字节组成一个数据包,从而提升I/O性能 tcp_nodelay on; #链接超时时间,单位是秒 keepalive_timeout 60; #开启gzip压缩功能 gzip on; #设置容许压缩的页面最小字节数,页面字节数从header头的Content-Length中获取。默认值是0,表示无论页面多大都进行压缩。建议设置成大于1K。若是小于1K可能会越压越大。 gzip_min_length 1k; #压缩缓冲区大小。表示申请4个单位为16K的内存做为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来存储gzip压缩结果。 gzip_buffers 4 16k; #压缩版本(默认1.1,前端为squid2.5时使用1.0)用于设置识别HTTP协议版本,默认是1.1,目前大部分浏览器已经支持GZIP解压,使用默认便可。 gzip_http_version 1.0; #压缩比率。用来指定GZIP压缩比,1压缩比最小,处理速度最快;9压缩比最大,传输速度快,但处理最慢,也比较消耗cpu资源。 gzip_comp_level 9; #用来指定压缩的类型,“text/html”类型老是会被压缩 gzip_types text/plain application/x-javascript text/css application/xml; #vary header支持。该选项可让前端的缓存服务器缓存通过GZIP压缩的页面,例如用 Squid缓存通过Nginx压缩的数据。 gzip_vary off; #开启ssi支持,默认是off ssi on; ssi_silent_errors on; #设置日志模式 log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; #反向代理负载均衡设定部分 #upstream表示负载服务器池,定义名字为backend_server的服务器池 upstream backend_server { server 10.254.244.20:81 weight=1 max_fails=2 fail_timeout=30s; server 10.254.242.40:81 weight=1 max_fails=2 fail_timeout=30s; server 10.254.245.19:81 weight=1 max_fails=2 fail_timeout=30s; server 10.254.243.39:81 weight=1 max_fails=2 fail_timeout=30s; #设置由 fail_timeout 定义的时间段内链接该主机的失败次数,以此来判定 fail_timeout 定义的时间段内该主机是否可用。默认状况下这个数值设置为 1。零值的话禁用这个数量的尝试。 设置在指定时间内链接到主机的失败次数,超过该次数该主机被认为不可用。 #这里是在30s内尝试2次失败即认为主机不可用! } ################### #基于域名的虚拟主机 server { #监听端口 listen 80; server_name www.abc.com abc.com; index index.html index.htm index.php; #首页排序 root /data0/abc; #站点根目录,即网站程序存放目录 error_page 500 502 404 /templates/kumi/phpcms/404.html; #错误页面 #伪静态 将www.abc.com/list....html的文件转发到index.php。。。 #rewrite ^/list-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /index.php?m=content&c=index&a=lists&catid=$1&types=$2&country=$3&language=$4&age=$5&startDate=$6&typeLetter=$7&type=$8&page=$9 last; #location 标签,根目录下的.svn目录禁止访问 location ~ /.svn/ { deny all; } location ~ \.php$ { #符合php扩展名的请求调度到fcgi server fastcgi_pass 127.0.0.1:9000; #抛给本机的9000端口 fastcgi_index index.php; #设定动态首页 include fastcgi.conf; }
反向代理:在收到客户端请求以后,会修目标IP地址和端口node
正向代理:在收到客户端请求以后,会修源IP地址和端口linux
上游服务器:代理服务器后端的哪些真正给客户端提供服务的节点,这样的服务器称之为上游服务器nginx
下游服务器:客户端就是下游节点web
只有proxy_pass 是必须的
模块:nginx_http_proxy_module 指令 proxy_pass:指定上游服务器的ip和端口 proxy_set_header:指定在从新封装请求报文的时候,添加一个新的首部 Syntax: proxy_pass URL; Default: — Context: location, if in location, limit_except 例子:proxy_pass http://10.220.5.200:80; Syntax: proxy_set_header field value; Default: proxy_set_header Host $proxy_host; Context: http, server, location
例如
location / { proxy_pass http://10.220.5.180; }
centos7.5
反向代理服务器IP:172.20.10.7/28
web1服务器IP:172.20.10.8/28
web2服务器IP:172.20.10.9/28
yum安装nignx须要配置网络源,复制下面的代码到你的yum仓库中
[ken] name=ken enabled=1 gpgcheck=0 baseurl=https://mirrors.aliyun.com/epel/7Server/x86_64/
安装
[root@ken ~]# yum install nginx -y
配置nginx文件,咱们实现这样一个效果,静态文件都被代理到172.20.10.8,动态文件都被调度到172.20.10.9,实现动静分离。
[root@ken ~]# vim /etc/nginx/nginx.conf # For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { 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 /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # include /etc/nginx/conf.d/*.conf; server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /var/www/html; index index.html index.php; # Load configuration files for the default server block. location /songqi { #在页面输入当前IP后在后面加上 /songqi 便可 proxy_pass http://172.20.10.8/; #上游节点,也便是访问这个页面时跳转到的网页,后面最好加 / ,免去建立同名目录的麻烦 } location ~ /.*\.php$ { #动态网页配置,使用了正则表达式 proxy_pass http://172.20.10.9; #使用了正则表达式因此不能在后面加 / proxy_set_header host $proxy_host; proxy_set_header realip $remote_addr; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } }