参考原文: http://blog.csdn.net/liaomin416100569/article/details/72897641
配置参照文档路径:进入到tengine.taobao.org官网,点击文档--->nginx文档-->文档
nginx.xml 位于/usr/local/nginx/conf (这里使用notepad++操作)
worker_processes 1; #设置工作的进程数(可设置为可用cpu的数量)默认值:1 命令:more /porc/cpuinfo 查看cup个数
#error_log logs/error.log;
#error_log logs/error.log notice; #error.log文件位于/usr/local/nginx/logs
#error_log logs/error.log info; #错误日志的实际级别是error
#pid logs/nginx.pid;
events {
worker_connections 1024; #1秒钟允许连接的个数
}
# load modules compiled as Dynamic Shared Object (DSO)
#
#dso { #可以导入c语言写的so文件
# load ngx_http_fastcgi_module.so;
# load ngx_http_rewrite_module.so;
#}
http {
include mime.types; #mime类型
default_type application/octet-stream; #默认mime类型
#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; #该日志文件和error.log位于相同目录下
#用于网站流量与性能分析指标:PV/UV/PR/IP/QPS/并发数/吞吐量/响应时间
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; #长连接的超时时间:客户端和服务端之间的连接可以保持的时间,超过这个时间没有客户端任何操作连接断开
#gzip on;
# 配置负载均衡
server 192.168.80.1:8080;
# session共享:保证同一个客户端访问的都是同一个后端服务器
session_sticky;
# 健康检查模块:查看服务器的状态
#详细查看:http://tengine.taobao.org//http_upstream_check_cn.html
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://backend; # 绑定负载均衡的配置
# 当用户访问 /admin/ 的路径 阻止访问
#查看内置变量:http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_core_module.html#variables
#查看下面的用法:http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_rewrite_module.html
if ( $uri ~* ^/admin/.*$ ) {
return 505;
# 健康检查的访问路径
location /status {
check_status;
access_log off;
allow 192.168.80.1; #允许访问该路径
deny all; #禁止所有的访问请求
# 增加访问登录限制
auth_basic "请输入用户名以及密码"; #auth_basic 相当于title的作用
auth_basic_user_file /usr/local/nginx/conf/htpasswd; #在/usr/local/nginx/conf目录下建立文件htpasswd
#在该文件中加入用户名密码,格式: 用户名:密码 密码使用密文可以在网上搜htpasswd生成
}
#健康检查的路径:http://Linux的ip/status 访问成功出现:Nginx http upstream check status
#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;
}
# 指定返回值505的映射以及返回页面
error_page 505 /505x.html;
location = /505x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
传统all in one 模式:单机资源的限制导致QPS(查询)的效率低(1000m光纤 1024QPS)
反向代理模式:使用nginx的升华版tengine
多机器 都部署相同的程序,多机器分担所有的请求压力
nginx 提供将请求平均到后台的所有的机器
Nginx的算法:
1>轮询算法:平均将请求分配到每一台机器上(tengine默认算法)
2>权重算法:指定机器分担请求的分配
3>最小连接算法:根据机器的性能高低,性能高的机器分配处理的请求多
代理分为 正向代理,透明代理,反向代理
1》正向代理为 局域网中的机器 访问目标地址(比如www.baidu.com) 不可达时 可以通过一个中间代理服务器(可以访问www.baidu.com)进行转发
正向代理 一般用于局域网联网等用途
2》反向代理:互联网中的机器 访问局域网中代理服务器 代理服务器通过各种规则找到对应后台服务器 获取数据响应 反向代理中 访问机器明确访问的
资源就是位于代理服务器 不需要在机器进行任何设置 可以用于设计cdn服务器 代理后端的静态资源
3》透明代理:
正向代理的一种特殊代理 直接在网关中 设置好代理的服务器 pc机器 直接访问网关即可连接
配置参照文档路径:进入到tengine.taobao.org官网,点击文档--->nginx文档-->文档
nginx.xml 位于/usr/local/nginx/conf (这里使用notepad++操作)
worker_processes 1; #设置工作的进程数(可设置为可用cpu的数量)默认值:1 命令:more /porc/cpuinfo 查看cup个数
#error_log logs/error.log;
#error_log logs/error.log notice; #error.log文件位于/usr/local/nginx/logs
#error_log logs/error.log info; #错误日志的实际级别是error
#pid logs/nginx.pid;
events {
worker_connections 1024; #1秒钟允许连接的个数
}
# load modules compiled as Dynamic Shared Object (DSO)
#
#dso { #可以导入c语言写的so文件
# load ngx_http_fastcgi_module.so;
# load ngx_http_rewrite_module.so;
#}
http {
include mime.types; #mime类型
default_type application/octet-stream; #默认mime类型
#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; #该日志文件和error.log位于相同目录下
#用于网站流量与性能分析指标:PV/UV/PR/IP/QPS/并发数/吞吐量/响应时间
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; #长连接的超时时间:客户端和服务端之间的连接可以保持的时间,超过这个时间没有客户端任何操作连接断开
#gzip on;
# 配置负载均衡
upstream backend {
#负载均衡,详细查看:http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_upstream_module.html#upstream
# 配置后端tomcat服务器的ip和端口
server 192.168.80.1:8080;
# session共享:保证同一个客户端访问的都是同一个后端服务器
session_sticky;
# 健康检查模块:查看服务器的状态
#详细查看:http://tengine.taobao.org//http_upstream_check_cn.html
#interval检测后端服务器的时间3000
#rise=2表示连续两次连接后端服务器表示存活
#fall=5表示连续5次连接后端服务器失败表示后端tomcat挂了
#timeout=1000 表示连接超时时间
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
#发送一个HEAD请求
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
#返回 2和3开头的响应码表示成功
check_http_expect_alive http_2xx http_3xx;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://backend; # 绑定负载均衡的配置
# 当用户访问 /admin/ 的路径 阻止访问
#查看内置变量:http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_core_module.html#variables
#查看下面的用法:http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_rewrite_module.html
if ( $uri ~* ^/admin/.*$ ) {
return 505;
}
}
# 健康检查的访问路径
location /status {
check_status;
access_log off;
allow 192.168.80.1; #允许访问该路径
deny all; #禁止所有的访问请求
# 增加访问登录限制
auth_basic "请输入用户名以及密码"; #auth_basic 相当于title的作用
auth_basic_user_file /usr/local/nginx/conf/htpasswd; #在/usr/local/nginx/conf目录下建立文件htpasswd
#在该文件中加入用户名密码,格式: 用户名:密码 密码使用密文可以在网上搜htpasswd生成
}
#健康检查的路径:http://Linux的ip/status 访问成功出现:Nginx http upstream check status
#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;
}
# 指定返回值505的映射以及返回页面
error_page 505 /505x.html;
location = /505x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
传统all in one 模式:单机资源的限制导致QPS(查询)的效率低(1000m光纤 1024QPS)
反向代理模式:使用nginx的升华版tengine
多机器 都部署相同的程序,多机器分担所有的请求压力
nginx 提供将请求平均到后台的所有的机器
Nginx的算法:
1>轮询算法:平均将请求分配到每一台机器上(tengine默认算法)
2>权重算法:指定机器分担请求的分配
3>最小连接算法:根据机器的性能高低,性能高的机器分配处理的请求多
代理分为 正向代理,透明代理,反向代理
1》正向代理为 局域网中的机器 访问目标地址(比如www.baidu.com) 不可达时 可以通过一个中间代理服务器(可以访问www.baidu.com)进行转发
正向代理 一般用于局域网联网等用途
2》反向代理:互联网中的机器 访问局域网中代理服务器 代理服务器通过各种规则找到对应后台服务器 获取数据响应 反向代理中 访问机器明确访问的
资源就是位于代理服务器 不需要在机器进行任何设置 可以用于设计cdn服务器 代理后端的静态资源
3》透明代理:
正向代理的一种特殊代理 直接在网关中 设置好代理的服务器 pc机器 直接访问网关即可连接