Nginx入门,
Nginx入门,
Nginx
1.nginx简介:
Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
2.Nginx国内版本
Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网 ,天猫商城 等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。
从2011年12月开始,Tengine成为一个开源项目,Tengine团队在积极地开发和维护着它。Tengine团队的核心成员来自于淘宝 、搜狗 等互联网企业。
3.传统All IN ONE架构:
4.反向代理:
反向代理: :互联网中的机器访问局域网中代理服务器 代理服务器通过各种规则找到对应后台服务器 获取数据响应 反向代理中 访问机器明确访问的
5.Tegine安装
$ ./configure :检查
$ make :编译
$ sudo make install :安装
搜索库:yum search gcc
安装:yum install gcc
缺少pcre和pcre-devel:yum -y install pcre pcre-devel
缺少openssl和openssl-devel 安装 :yum -y install openssl openssl-devel
6.nginx命令参数:
nginx -m 显示所有加载的模块
nginx -l 显示所有可以使用的指令
nginx -t 检查nginx的配置文件是否正确
nginx -s 启动nginx
nginx -s reload 重启nginx
nginx -s stop 停止nginx
7.配置nginx
nginx负责负载均衡转发
配置负载均衡使用nginx的代理模块 具体参考 tengine官网配置(http://tengine.taobao.org/document_cn/http_upstream_check_cn.html)
找到usr/local/nginx/conf/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;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream backend {
server 192.168.182.128:8080;
server 192.168.182.1:8080;
}
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;//配置反向代理
}
内核模块
#cup个数:
worker_processes 1;
支持的最大数:
events {
worker_connections 1024;
}
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; #日志文件(默认开启)
sendfile on; #快速发送文件
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; #长连接
#gzip on;
#负载均衡
upstream backend {
server 192.168.182.128:8080;
server 192.168.182.1:8080;
#session_sticky模块功能中
session_sticky; #粘性session
#健康检查(首页/文档/健康检查模块
#interval:间隔时间
#rise=2表示连续两次连接后端服务器表示存活
#fall=5表示连续5次连接后端服务器失败表示后端tomcat挂了
)
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/路径 阻止访问(nginx文档/文档/ngx_htt_rewrite_module)
if ($uri ~* ^/admin/.*$ ){
return 505;
}
}
location /status {
check_status;
#添加用户名密码框
auth_basic "closed site";#标题
auth_basic_user_file /usr/local/nginx /conf/htpasswd;#绝对路径
access_log off;
allow 192.168.182.1;
deny all;
}
#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;
}
location = /my505.html {
root html;
}
添加用户名密码:
Nginx/文档/ngx_http_auth=basic_module
指定保存用户名和密码的文件,格式如下:
# comment
name1:password1
name2:password2:comment
name3:password3
密码应该使用crypt()函数加密。 可以用Apache发行包中的htpasswd命令来创建此类文件。
评论暂时关闭