您的位置:首页 > 运维架构 > Nginx

nginx 笔记

2016-06-10 08:01 591 查看
 nginx 这个轻量级、高性能的 web server 主要可以干两件事情:

  〉直接作为http server(代替apache,对PHP需要FastCGI处理器支持);
  〉另外一个功能就是作为反向代理服务器实现负载均衡

http://www.netcraft.com/ 解决 c10k
connection 10000
http://blog.csdn.net/jysg9/article/details/7901321
阻塞模型
(只能一个用户,其他用户等待)
-----------------------------------------------------------------------------
多进程模型
fork--->专子进程--->专用户
(时间片,内存资源,大量进程切换(内核切换)额外内核时间,无助于用户响应)

非DMA机制
用户请求(HTML)----->进程(阻塞,不可中断sleep, WAIT)---->内核(取)------>磁盘文件(HTML)--->
到内核空间内存(buffer cache)----->copy to 进程空间内存

磁盘块(1K,2K,4K)(内核要管理)----------->page frame 4k(内核管理)------>用户空间

DMA机制,直接内存防问

用户进程请求 ---->(CPU给内核内存地址交给->DMA)---->磁盘文件---->DMA加载内核空间空间内存完成--CPU中断(中断当前进程)-->一次拷贝到用户空间内存中
这样CPU可以处理网卡请求

缺陷:每个进程的地址空间是重复数据,内存使用效益低
-----------------------------------------------------------------------
一个进程多线程模型:
thread
linux轻量级进程 :lwp
linux 技持多少种类型的线程库?如何切换切换线程库

线程并行执行流
code----BSS(全部变量,未初始化变量)--- HEAP(文件)----动态内存区---STACK(局部变量)
解决每个进程的地址空间是重复数据,共享数据,文件,内存需求小了许了(相对进程)

每个线程响应一个请求:
线程依然切换,但是轻量级

进程切换,保存现场,恢复现场(一级数据缓存,一级指令缓存据,二级数据缓存,CPU寄存器 需要恢复)
线程间只需要CPU寄存器切换

线程分配到多核CPU的每个CPU中

http://www.cnblogs.com/EthanCai/p/3705834.html
CPU忙等:时间片内,每隔一段时间看看,自旋锁
CPU闲等: 马上切换掉,不会占全部时间片
快速切换时,会导致线程抖动

---------------------------------------------------------------------------
多程程多线程模型

开机时,留一个CPU核,给系统,其他CPU核进程绑定到第个进程,进程这样需要切换
(多进程下)select (1024),每当内核准备好了一个IO,内核进行扫描文件描述符,设置,通 知用户进程
-------------------------------------------------------------------------------
多线程:N个请求
一个线程响应多个请求

一个线程有多个IO(磁盘IO与网络IO)

磁盘IO用AIO,不能阻塞线程,

多路IO机制,IO复用 (SELECT POLL)

基于事件驱动 epoll(linux) /dev/poll(solaris) kqueue(freebsd)

http://www.cnblogs.com/Anker/archive/2013/08/14/3258674.html
aio 不阻塞异步IO

NGINX:AIO,MMAP EVENT-DRIVEN,具有前端反向代理

httpd:
mpm
prefork:一个进程响应一个请求,1024
worker:一个线程响应一个请求,多进程,一个进程生成多个线程
event:基于事件驱动
支持反向代理

LAMP:
--->NGINX(反向代理)---->缓存服务器 ----> [apache(web server)(php模块)] ---->mysqlproxy---->mysql主从
| | ----->缓存服务器----> [web server (动态内容,php页面)]
| |
| NGINX(服务器静态 CSS JPG)
|
HTTP附件服务器

IO模型:
同步阻塞:
异步阻塞: IO复用
异步阻塞:event-driven
异步非阻塞:AIO

NGINX:
mmap
event-driver :一个进程响应多个请求:单线程进程
aio

lemp:

-------->nginx(输入与输出缓存区)----->fastcgi(php服务器):同步

--------nginx(输入与输出缓存区)------暂存请求NGINX------> PHP服务器 :异步

enginx(fastcgi)+php-fpm

nginx转发:

location ~*\>php${
fastcgi_pass 127.0.0.1:9000

}

nginx配制文件
main

events{
事件驱动相关内容
}

location /forum/{
directive <parameters>;
proxy_pass http://172.16.100.11/bbs/; URL的访问属性
}

http://www.magedu.com/forum/----->http://172.16.100.11:8080/bbs/;

httpd{
http相关的配置
}

server
{
虚拟主机
listen 80;
server_name www.baidu.com
location /{
后端服务器:
}}

模式:
location ~* ^/forum{
proxy_pass http://172.16.100.11:8080 :不加(FIRUM)
}
http://www.magedu.com/forum/----->http://172.16.100.11:8080/forum/;(forum,在服务器本地)

eg:
yum insqll httpd

cd /var/www/html/index.html
mv /var/www/html/index.html /var/www/html/bbs.html

location /forum/{

proxy_pass http://172.16.100.6/bbs/;
}
--------------------------------------------------------------
location ~* ^/forum/{

proxy_pass http://172.16.100.6;
}

http://172.16.100.6/forum
----------------------------------------

proxy_set_header X-Real-IP

$remote_addr
$remote_user
$request_filename

$request_method HTTP请求方法
GET POST HEAD PUT TRACE OPTIONS CONNECTION DELETE

http://172.16.100.6/bbs/
$request_uri /bbs/
$scheme http https
$server_name 代理服务器名

日志反映真实客户端IP
location ~* ^/forum/{

proxy_pass http://172.16.100.6; proxy_set_header X-Real-IP $remote_addr

}

vim /etc/httpd/conf/httpd.conf

logformat 加入 %(X-Real-IP)i

---------------------------------------------------

整站转换
location /{

proxy_pass http://172.16.100.6/; proxy_set_header X-Real-IP $remote_addr

}
----------------------------------------------------
/var/www/html/index.html

upstream :负载均衡

upstream webservs{
ip_hash;
server 172.16.100.6 weight=1;
server 172.16.100.7 weight=1 max_fails=2 fail_timeout=2;
server 127.0.0.1:8080 backup; ////当 上面的SERVER 全DOWN 掉 重定向本机 //ip_hash不能使用 这行
}

server{
listen 80;
server_name localhost;
location / {
proxy_pass http://websrvs/; proxy_set_header X-Real-IP $remote_addr;

}}
----------------------------------------------------------------------------------

server{
listen 8080;
server_name localhost;
//定义本机错误页面
root /web/errorpages; //本机目录
index index.html;

location / {
proxy_pass http://websrvs/; proxy_set_header X-Real-IP $remote_addr;

}}
-------------------------------------------------------------------
nginx: 算法

round-robin
ip_hash
least_conn

----------------------------------------------------------------------

netstat -ant |awk '/:80\>/{S[$NF]++}END{for(A in S){print A,S[A]}}'

----------------------------
upstream webservs{
#ip_hash;
server 172.16.100.6 weight=1;
server 172.16.100.7 weight=1 max_fails=2 fail_timeout=2;
#server 127.0.0.1:8080 backup; ////当 上面的SERVER 全DOWN 掉 重定向本机 //ip_hash不能使用 这行

}
缓存目录级别:eg /nginx/cache/firest/2/b3/bc909.......
proxy_cache_path /nginx/cache/first levels=1:2 keys_zone=first:20m max_size=1g

server{
listen 80;
server_name localhost;
add_header X-Via $server_addr; //谁提供的缓存IP
add_header X-Cache $upstream_cache_status from $server_addr"; //缓存是否命中 HIT MISS

location / {
proxy_pass http://websrvs/; proxy_set_header X-Real-IP $remote_addr;
proxy_cache first;
proxy_cache_valid 200 10m;

}}
mkdir /niinx/cache/first

cache_manager:lru

-------------------------------------------------------------------------
另外三种缓存:
open_log_cache:将日志写到缓存,然后再写到磁盘
open_file_cache:文本无数据缓存,提供命中
fastcgi_cache:

NGINX 的limit 限制也基于共享内存实现

nginx:gzip以压缩向客户响应
gzip on

-------------------------------

nginx 区别应用

upstream phpservs{
server
server
}

upstream imgsrvs{

server
server

}
upstream staticfilesrvs{
server
server
}

location / {
root /web/htdocs;
index index.php index.html;
}

localhost ~* \.php${

fastcgi_pass http://phpservs;
}

location ~* "\.(jpg|jpeg|gif|png)$"{

proxy_pass http://imgsrvs; }

-------------------------
nginx 区别应用

rewite:url重写模块
if (condition){
}

locaction /images/{
rewrite http://172.16.100.19/images/ }
支持正测表达式
location /{
root html;
index index.html;
rewrite "^/bbs/(.*)" http://172.16.100.19/forum/$1; }

测试:双目测试:
~ ,!~
= ,!=
~*,!~*

if ($request_method="POST"){

}
if ($request_uri ~* "/forum"){

}

单目测试:

referer:

location /photos/{
valid_referers none /blocked www.baidu.com
if($invalid_referer){
return 430;
}
}
----------------------------------------------------
http://tengine.taobao.org/book/index.html Nginx开发从入门到精通

last:本次与完成之后,重启一下轮检查
location / {
root html
index index.html
rewrite "^/bbs/(.*)images/(.*) \.jpg$ http://www.magedu.com/bbs/$2/images/$1.jpg last

http://www.magedu.com/bbs/a/image/b.jpg --->http://www.magedu.com/bbs/b/image/a.jpg

循环问题
-------------------------------------------------------

break:本次重写完成之后,直接执行后续操作;

webdev:
/etc/httpd/conf/httpd.conf

Directory
dav on ----->PUT

setfacl -m u:apache:rwx /var/www/html/
curl -T /etc/issue http://172.16.100.7
读写分离:
location /{
proxy_pass http://172.16.100.6/; if($request_method = "PUT"){
proxy_pass http://172.16.100.7; }
}
curl http://172.16.100.106 curl -T /etc/fstab http://172.16.100.106
主 RSYNC+INOTIFY SERVER
读写分离

------------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: