NGINX是什么?
nginx是开源的,支持高性能的,高并发的www服务和代理服务软件,就是web服务器,nginx不但是一个优秀的web服务软件,还可以做反向代理,负载均衡,以及缓存服务使用.
优点:支持高并发,支持几万的并发连接;资源消耗少.在3万并发连接下开启10个nginx线程消耗内存不到200M;可以做负载均衡,反向代理;支持异步网络I/O事件模型epoll
Tengine是淘宝网的web项目,是Nginx的高性能版,tengine的性能和稳定性极高.
web服务器和web框架的关系
web服务器(nginx):接收HTTP请求(例如www.alonemans.com/dudu.jpg)并返回数据
web框架(django,flask):开发web应用程序,处理接收到的数据
基础安装
- 下载源码包 --- wget http://tengine.taobao.org/download/tengine-2.2.0.tar.gz
- 卸载掉之前通过yum源安装的nginx -- yum remove nginx -y
- 解决编译安装的依赖环境
yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel openssl openssl-devel -y
- 解压缩源码包 --- tar -zxf tengine-2.2.0.tar.gz
- 释放Makefile --- ./configure --prefix=/opt/tnginx220
- 执行 make && make install
- 编译完成后 tnginx可以正常使用
- 了解tnginx220的目录结构作用
/opt/tnginx220
[root@master tnginx220]# ll
drwxr-xr-x. 2 root root 4096 Mar 11 08:50 conf #放nginx所有配置文件的地儿
drwxr-xr-x. 2 root root 40 Mar 11 08:50 html #存放前端 html文件的
drwxr-xr-x. 2 root root 4096 Mar 11 08:50 include
drwxr-xr-x. 2 root root 41 Mar 11 08:52 logs #nginx的日志文件夹
drwxr-xr-x. 2 root root 6 Mar 11 08:50 modules
drwxr-xr-x. 2 root root 35 Mar 11 08:50 sbin #存放nginx二进制命令的
- 添加linux环境变量PATH,使用快捷命令
echo $PATH
PATH="/opt/python36/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/opt/tnginx220/sbin"
#将tnginx220 的路径添加到PATH中,在后面添加即可
编辑 #vim /etc/profile
添加后执行 #source /etc/profile
- nginx亦可以正常使用了
nginx.conf配置文件相关
http内核模块
//公共的配置定义在http{}
http { //http层开始
...
//使用Server配置网站, 每个Server{}代表一个网站(简称虚拟主机)
'server' {
listen 80; //监听端口, 默认80
server_name localhost; //提供服务的域名或主机名
access_log host.access.log //访问日志
//控制网站访问路径
'location' / {
root /usr/share/nginx/html; //存放网站代码路径
index index.html index.htm; //服务器返回的默认页面文件
}
//指定错误代码, 统一定义错误页面, 错误代码重定向到新的Locaiton
error_page 500 502 503 504 /50x.html;
}
...
//第二个虚拟主机配置
'server' {
...
}
include /etc/nginx/conf.d/*.conf; //包含/etc/nginx/conf.d/目录下所有以.conf结尾的文件
} //http层结束
基于域名的多虚拟主机实战
也就是在一个服务器上运行多个网站
方法:
1.环境准备,准备好两个域名,这里模拟的本地域名解析,找到windows下的hosts文件( 由于我们是通过windows访问,达到访问不同的 域名,因此配置windows的hosts)
#编辑文件 C:\Windows\System32\drivers\etc\hosts
#写入
192.168.11.229 meihao.com
192.168.11.229 shenghuo.com
2.配置nginx支持多虚拟主机
-- 修改nginx.conf 修改2个server虚拟主机配置
#meihao的虚拟主机
server {
listen 80;
server_name meihao.com;
# 当我们访问meihao.com:80/的时候,就进入这个虚拟主机,且找到这个location,进行网站资源分配
location / {
root /opt/meihao/;
index index.html;
}
}
#第二个虚拟主机,shenghuo
server{
listen 80;
server_name shenghuo.com;
location / {
root /opt/shenghuo/;
index index.html;
}
}
--分别修改两个网址的根目录数据
mkdir -p /opt/{meihao,shenghuo}
#分别在/opt/meihao/创建 index.html;/opt/shenghuo/创建 index.html
--修改完配置文件,先检测语法 nginx -t
-- 平滑加载nginx(不重启nginx,重新读取配置文件) nginx -s reload
nginx的访问日志logs功能
vim nginx.conf
打开注释
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; #开启日志功能,生成access.log文件,可以监控访问者
nginx的404网页优化
编辑nginx.conf
server {
listen 80;
server_name meihao.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /opt/meihao;
index index.html index.htm;
}
#打开这个参数的配置,开启错误页面
error_page 404 403 402 401 /40x.html; #40几的错误会对应生成40x.html
}
记得在 /opt/meihao/ 创建一个40x.html 文件 --- touch 40x.html ,写入你的创意404界面...嘿嘿要美观,当你访问的meihao.com/asdfasdas 的时候自动跳转的温馨的404页面
nginx拒绝ip访问
location / {
deny 你想限制的ip; #!!!还可以限制网段
root /opt/meihao;
index index.html index.htm;
allow 10.0.0.1; #限制网段访问
}
nginx反向代理
什么是反向代理?
对于客户端而言,代理服务端就是原始服务端,实际来讲,就是为了保护和隐藏原始服务器不受到攻击.
反向代理的实现:
- 环境准备:两台服务器
192.168.220.130 #真是资源服务器
192.168.220.128 #nginx代理服务器
- 我们作为客户端,访问代理服务器,代理服务器,将资源服务器上的东西进行返回
- 先配置资源服务器 192.168.220.130 meihao.com
- 配置代理服务器 192.168.220.128 修改192.168.220.128 这台机器上的配置文件,开始反向代理
#配置 nginx.conf 的server{}如下:
server {
listen 80 ;
server_name _;
location / {
#反向代理参数,当我们请求192.168.11.136:80/的时候,进入这里server,然后location进行资源分配
proxy_pass http://192.168.220.130; #就是间接的访问了 192.168.220.130:80/
}
}
nginx负载均衡
配置nginx负载均衡环境准备,三台服务器,这三台服务器都是nginx实现的
192.168.220.128 反向代理服务器
192.168.220.130 资源服务器 返回xiaohua页面
192.168.220.131 资源服务器 返回一个h1标签,且行且珍惜
实现过程:
....
CrazyShenldon
posted @
2019-03-12 00:50
FindSoul
阅读(
194)
评论()
收藏
举报
var RENDERER = {
POINT_INTERVAL : 5,
FISH_COUNT : 3,
MAX_INTERVAL_COUNT : 50,
INIT_HEIGHT_RATE : 0.5,
THRESHOLD : 50,
init : function(){
this.setParameters();
this.reconstructMethods();
this.setup();
this.bindEvent();
this.render();
},
setParameters : function(){
this.$window = $(window);
this.$container = $('#jsi-flying-fish-container');
this.$canvas = $('');
this.context = this.$canvas.appendTo(this.$container).get(0).getContext('2d');
this.points = [];
this.fishes = [];
this.watchIds = [];
},
createSurfacePoints : function(){
var count = Math.round(this.width / this.POINT_INTERVAL);
this.pointInterval = this.width / (count - 1);
this.points.push(new SURFACE_POINT(this, 0));
for(var i = 1; i < count; i++){
var point = new SURFACE_POINT(this, i * this.pointInterval),
previous = this.points[i - 1];
point.setPreviousPoint(previous);
previous.setNextPoint(point);
this.points.push(point);
}
},
reconstructMethods : function(){
this.watchWindowSize = this.watchWindowSize.bind(this);
this.jdugeToStopResize = this.jdugeToStopResize.bind(this);
this.startEpicenter = this.startEpicenter.bind(this);
this.moveEpicenter = this.moveEpicenter.bind(this);
this.reverseVertical = this.reverseVertical.bind(this);
this.render = this.render.bind(this);
},
setup : function(){
this.points.length = 0;
this.fishes.length = 0;
this.watchIds.length = 0;
this.intervalCount = this.MAX_INTERVAL_COUNT;
this.width = this.$container.width();
this.height = this.$container.height();
this.fishCount = this.FISH_COUNT * this.width / 500 * this.height / 500;
this.$canvas.attr({width : this.width, height : this.height});
this.reverse = false;
this.fishes.push(new FISH(this));
this.createSurfacePoints();
},
watchWindowSize : function(){
this.clearTimer();
this.tmpWidth = this.$window.width();
this.tmpHeight = this.$window.height();
this.watchIds.push(setTimeout(this.jdugeToStopResize, this.WATCH_INTERVAL));
},
clearTimer : function(){
while(this.watchIds.length > 0){
clearTimeout(this.watchIds.pop());
}
},
jdugeToStopResize : function(){
var width = this.$window.width(),
height = this.$window.height(),
stopped = (width == this.tmpWidth && height == this.tmpHeight);
this.tmpWidth = width;
this.tmpHeight = height;
if(stopped){
this.setup();
}
},
bindEvent : function(){
this.$window.on('resize', this.watchWindowSize);
this.$container.on('mouseenter', this.startEpicenter);
this.$container.on('mousemove', this.moveEpicenter);
this.$container.on('click', this.reverseVertical);
},
getAxis : function(event){
var offset = this.$container.offset();
return {
x : event.clientX - offset.left + this.$window.scrollLeft(),
y : event.clientY - offset.top + this.$window.scrollTop()
};
},
startEpicenter : function(event){
this.axis = this.getAxis(event);
},
moveEpicenter : function(event){
var axis = this.getAxis(event);
if(!this.axis){
this.axis = axis;
}
this.generateEpicenter(axis.x, axis.y, axis.y - this.axis.y);
this.axis = axis;
},
generateEpicenter : function(x, y, velocity){
if(y < this.height / 2 - this.THRESHOLD || y > this.height / 2 + this.THRESHOLD){
return;
}
var index = Math.round(x / this.pointInterval);
if(index < 0 || index >= this.points.length){
return;
}
this.points[index].interfere(y, velocity);
},
reverseVertical : function(){
this.reverse = !this.reverse;
for(var i = 0, count = this.fishes.length; i < count; i++){
this.fishes[i].reverseVertical();
}
},
controlStatus : function(){
for(var i = 0, count = this.points.length; i < count; i++){
this.points[i].updateSelf();
}
for(var i = 0, count = this.points.length; i < count; i++){
this.points[i].updateNeighbors();
}
if(this.fishes.length < this.fishCount){
if(--this.intervalCount == 0){
this.intervalCount = this.MAX_INTERVAL_COUNT;
this.fishes.push(new FISH(this));
}
}
},
render : function(){
requestAnimationFrame(this.render);
this.controlStatus();
this.context.clearRect(0, 0, this.width, this.height);
this.context.fillStyle = 'hsl(0, 0%, 95%)';
for(var i = 0, count = this.fishes.length; i < count; i++){
this.fishes[i].render(this.context);
}
this.context.save();
this.context.globalCompositeOperation = 'xor';
this.context.beginPath();
this.context.moveTo(0, this.reverse ? 0 : this.height);
for(var i = 0, count = this.points.length; i < count; i++){
this.points[i].render(this.context);
}
this.context.lineTo(this.width, this.reverse ? 0 : this.height);
this.context.closePath();
this.context.fill();
this.context.restore();
}
};
var SURFACE_POINT = function(renderer, x){
this.renderer = renderer;
this.x = x;
this.init();
};
SURFACE_POINT.prototype = {
SPRING_CONSTANT : 0.03,
SPRING_FRICTION : 0.9,
WAVE_SPREAD : 0.3,
ACCELARATION_RATE : 0.01,
init : function(){
this.initHeight = this.renderer.height * this.renderer.INIT_HEIGHT_RATE;
this.height = this.initHeight;
this.fy = 0;
this.force = {previous : 0, next : 0};
},
setPreviousPoint : function(previous){
this.previous = previous;
},
setNextPoint : function(next){
this.next = next;
},
interfere : function(y, velocity){
this.fy = this.renderer.height * this.ACCELARATION_RATE * ((this.renderer.height - this.height - y) >= 0 ? -1 : 1) * Math.abs(velocity);
},
updateSelf : function(){
this.fy += this.SPRING_CONSTANT * (this.initHeight - this.height);
this.fy *= this.SPRING_FRICTION;
this.height += this.fy;
},
updateNeighbors : function(){
if(this.previous){
this.force.previous = this.WAVE_SPREAD * (this.height - this.previous.height);
}
if(this.next){
this.force.next = this.WAVE_SPREAD * (this.height - this.next.height);
}
},
render : function(context){
if(this.previous){
this.previous.height += this.force.previous;
this.previous.fy += this.force.previous;
}
if(this.next){
this.next.height += this.force.next;
this.next.fy += this.force.next;
}
context.lineTo(this.x, this.renderer.height - this.height);
}
};
var FISH = function(renderer){
this.renderer = renderer;
this.init();
};
FISH.prototype = {
GRAVITY : 0.4,
init : function(){
this.direction = Math.random() < 0.5;
this.x = this.direction ? (this.renderer.width + this.renderer.THRESHOLD) : -this.renderer.THRESHOLD;
this.previousY = this.y;
this.vx = this.getRandomValue(4, 10) * (this.direction ? -1 : 1);
if(this.renderer.reverse){
this.y = this.getRandomValue(this.renderer.height * 1 / 10, this.renderer.height * 4 / 10);
this.vy = this.getRandomValue(2, 5);
this.ay = this.getRandomValue(0.05, 0.2);
}else{
this.y = this.getRandomValue(this.renderer.height * 6 / 10, this.renderer.height * 9 / 10);
this.vy = this.getRandomValue(-5, -2);
this.ay = this.getRandomValue(-0.2, -0.05);
}
this.isOut = false;
this.theta = 0;
this.phi = 0;
},
getRandomValue : function(min, max){
return min + (max - min) * Math.random();
},
reverseVertical : function(){
this.isOut = !this.isOut;
this.ay *= -1;
},
controlStatus : function(context){
this.previousY = this.y;
this.x += this.vx;
this.y += this.vy;
this.vy += this.ay;
if(this.renderer.reverse){
if(this.y > this.renderer.height * this.renderer.INIT_HEIGHT_RATE){
this.vy -= this.GRAVITY;
this.isOut = true;
}else{
if(this.isOut){
this.ay = this.getRandomValue(0.05, 0.2);
}
this.isOut = false;
}
}else{
if(this.y < this.renderer.height * this.renderer.INIT_HEIGHT_RATE){
this.vy += this.GRAVITY;
this.isOut = true;
}else{
if(this.isOut){
this.ay = this.getRandomValue(-0.2, -0.05);
}
this.isOut = false;
}
}
if(!this.isOut){
this.theta += Math.PI / 20;
this.theta %= Math.PI * 2;
this.phi += Math.PI / 30;
this.phi %= Math.PI * 2;
}
this.renderer.generateEpicenter(this.x + (this.direction ? -1 : 1) * this.renderer.THRESHOLD, this.y, this.y - this.previousY);
if(this.vx > 0 && this.x > this.renderer.width + this.renderer.THRESHOLD || this.vx < 0 && this.x < -this.renderer.THRESHOLD){
this.init();
}
},
render : function(context){
context.save();
context.translate(this.x, this.y);
context.rotate(Math.PI + Math.atan2(this.vy, this.vx));
context.scale(1, this.direction ? 1 : -1);
context.beginPath();
context.moveTo(-30, 0);
context.bezierCurveTo(-20, 15, 15, 10, 40, 0);
context.bezierCurveTo(15, -10, -20, -15, -30, 0);
context.fill();
context.save();
context.translate(40, 0);
context.scale(0.9 + 0.2 * Math.sin(this.theta), 1);
context.beginPath();
context.moveTo(0, 0);
context.quadraticCurveTo(5, 10, 20, 8);
context.quadraticCurveTo(12, 5, 10, 0);
context.quadraticCurveTo(12, -5, 20, -8);
context.quadraticCurveTo(5, -10, 0, 0);
context.fill();
context.restore();
context.save();
context.translate(-3, 0);
context.rotate((Math.PI / 3 + Math.PI / 10 * Math.sin(this.phi)) * (this.renderer.reverse ? -1 : 1));
context.beginPath();
if(this.renderer.reverse){
context.moveTo(5, 0);
context.bezierCurveTo(10, 10, 10, 30, 0, 40);
context.bezierCurveTo(-12, 25, -8, 10, 0, 0);
}else{
context.moveTo(-5, 0);
context.bezierCurveTo(-10, -10, -10, -30, 0, -40);
context.bezierCurveTo(12, -25, 8, -10, 0, 0);
}
context.closePath();
context.fill();
context.restore();
context.restore();
this.controlStatus(context);
}
};
$(function(){
RENDERER.init();
});