架构师第一天之:Nginx

nginx:

诞生背景:

prefork机制不能支持过大的并发请求,

C10K问题的解决

官方站点:

http://nginx.org

二次开发版:

tengine,openresty

特性:

模块化设计,较好的拓展性

高可靠性:master/worker架构

支持热部署:不停机更新配置文件,更换日至文件,更新服务器版本

低内存消耗:10000个keep-alived连接模式下的非活动链接仅消耗2.5M内存

event-driven机制,支持事件驱动

基本功能:

静态资源的web服务器

作为httd反向代理服务器

pop3/imap4协议反向代理服务器(邮件存取服务)

FastCGI(LNMP),uWSGI等协议

模块化机制(非DSO动态模块拓展机制),著名的模块例如:zip,ssl..

web相关功能:虚拟主机,keepalive,访问日志,url重写,路径别名,基于ip及用户的访问控制,速率限制和并发数限制

##代理和反向代理的概念

正向代理:snat代理本地用户访问互联网

反向代理:dnat代理服务器负责从后端服务器请求数据

程序架构:

架构师第一天之:Nginx

master/worker架构

一个master进程可以生成一个或者多个worker进程

master:加载配置文件,管理worker进程,平滑升级

worker:http服务,http代理,fastcgi代理

模块类型:

核心模块:core modules

标准模块:

standard http modules

optional http modules

mail modules

3rd party modules:第三方模块

用来做什么:

静态资源服务器

http协议反向代理

一、安装nginx

系统环境和软件选择:

centos7

yum源:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1

编译选项:

–prefix=/usr/local                                    #安装位置
–sbin-path=/usr/sbin/nginx                    #启动脚本定义位置
–conf-path=/etc/nginx/nginx.conf         #配置文件定义位置
–error-log-path=/var/log/nginx/error.log     #错误日志定义位置
–http-log-path=/var/log/nginx/access.log    #访问日志位置
–pid-path=/var/run/nginx.pid                        #pid文件位置
–lock-path=/var/run/nginx.lock                     #lock文件位置
–http-client-body-temp-path=/var/cache/nginx/client_temp      #客户端缓存位置
–http-proxy-temp-path=/var/cache/nginx/proxy_temp               #代理缓存位置
–http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp           #fastcgi缓存位置
–http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp             #uwsgi缓存位置
–http-scgi-temp-path=/var/cache/nginx/scgi_temp                    #scgi缓存位置
–user=nginx                                                                                  #所属用户
–group=nginx                                                                               #所属组
–with-http_ssl_module                                                                  #安装ssl模块
–with-http_realip_module                                                             #realipmo模块
–with-http_addition_module                                                        #附加模块
–with-http_sub_module                                                                #其他子模块
–with-http_dav_module                                                                 #http_dav模块
–with-http_flv_module                                                                   #flv文件模块
–with-http_mp4_module                                                               #mp4模块
–with-http_gunzip_module                                                           #压缩模块
–with-http_gzip_static_module                                                     #静态压缩模块
–with-http_random_index_module                                               #随机index模块
–with-http_secure_link_module                                                     #安全连接模块
–with-http_stub_status_module                                                      #http状态模块
–with-http_auth_request_module                                                 #http认证请求模块
–with-threads                                                                                #线程模块
–with-stream                                                                                #stream模块
–with-stream_ssl_module                                                             #stream_ssl模块
–with-http_slice_module                                                               #slice模块
–with-mail                                                                                      #邮件模块
–with-mail_ssl_module                                                                   #邮件加密模块
–with-file-aio                                                                                   #aio模块(异步io模块)
–with-http_v2_module                                                                     #http2模块

编译安装nginx:

yum install openssl-devel pcre-devel zlib-devel -y

yum groupinstall "Development Tools" "Server Platform Development" -y      #基本环境安装

useradd -s /sbin/nologin -M nginx

下载好源码编译安装包:

tar xf nginx-1.8.1.tar.gz

cd nginx-1.8.1/

./configure –prefix=/usr/local/nginx –conf-path=/etc/nginx/nginx.conf –user=nginx –group=nginx –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –pid-path=/var/run/nginx/nginx.pid –lock-path=/var/lock/nginx.lock –with-http_ssl_module –with-http_gzip_static_module –with-http_stub_status_module                                       #开始编译

make && make install

cd /usr/local/nginx/                    #去安装路径下

/usr/local/nginx/sbin/nginx -t     #配置文件测试

/usr/local/nginx/sbin/nginx -h     #帮助

/usr/local/nginx/sbin/nginx -s stop, quit, reopen, reload   #四种状态

/usr/local/nginx/sbin/nginx          #启动

/usr/local/nginx/sbin/nginx -s stop   #停止

ps -ef | grep nginx                #查进程

cd /etc/nginx/                     #配置文件目录

cp nginx.conf nginx.conf.bak    #初始文件备份

##配置文件的组成部分:

大型服务的配置文件片段化:

vim /etc/nginx/nginx.conf

include conf.d/*.conf          #片段化配置文件

fastcgi,scgi,uwscgi相关配置

配置指令(必须以分号结尾):

directive value1 [value2];

支持变量使用:

内置变量:由模块引入,可直接调用

自定义变量:set variables_name value;

引用变量:$value_name

##配置文件结构:

main block :对http以及mail模块均有效

event{

}  :事件驱动类型配置

http{

} :http相关配置

mail{

} :邮件相关配置

***http相关配置:

http{

server {

listen     port;

root       path;              #根文件位置

server_name     hostname;       #主机名

alias                   #别名配置

location / {          #location配置

…..

}

}

server{

}

}

###main配置端的解析

main block:

配置指令的类别:

正常运行必备的配置;

优化性能的配置;

用于调试,定位问题的配置

(1)正常运行必备的配置:

user   nginx nginx;              #用户配置

pid     /path/to/PID_FILE;     #指定pid 文件位置

worker_rlimit_nofile number;  #单个worker进程能够打开的最大文件数

(2)性能优化方面的配置 :

worker_processes   auto;      #指明工作进程数能使用auto(1.8以上)cpu核心数-1

worker_cpu_affinity auto; #cpu_mask就是cpu个数的二进制(二核心:0001 0010)cpu亲缘性绑定

worker_priority nice ;     #进程优先级的指定(nice(-20~19对应(100-139)))

(3)调试定位问题的配置:

deamon     on|off;        #是否以守护进程模式运行nginx(on)

master_process off;      #启动master进程(off)

error_log     file   模式;   #错误日志文件的记录方式和级别(debug级别需要编译的时候加载)

#方式:stderr:发送到错误输出

syslog:server=address[,paraameter=value]  #发送给syslog服务器

memory:size      #计入到内存中(性能比较好,降低磁盘io)

#日志级别:debug   依赖于configure的debug模式的开启

原创文章,作者:wanghui,如若转载,请注明出处:http://www.178linux.com/53982

(0)
wanghuiwanghui
上一篇 2016-10-29 10:47
下一篇 2016-10-29 12:27

相关推荐

  • 用户和组的四大配置文件简介

    一. 用户的由来 linux系统拥有的就是资源,最重要的事就是对资源的分配,资源分给谁?在linux上资源的访问是对用户赋予不同的权限实现,也就是说能访问资源的单位是用户。那用户在获取资源之前要实现证明自己是本用户,这个过程称为认证,他通过密码和用户名实现。在用户登录时会将用户输入的用户名和密码进行校验,校验过程就是将输入的用户名和密码与linux系统上记录…

    Linux干货 2016-10-24
  • 马哥教育网络班28期—第1、2天知识点概略

    没做随堂笔记,根据记忆手打,看看自己能记住多少。

    Linux干货 2017-12-03
  • N21天天第十五周课程练习

    1、总结sed和awk的详细用法; sed Sed本质上是一个编辑器,但是它是非交互式的;同时它又是面向字符流的,输入的字符流经过sed的处理后输出。 Sed本身是一个管道命令,可以分析standard input的,主要是用来分析关键字的使用、统计等,此外还可以将数据进行替换、删除、   选取特定行等功能 格式:sed&nbs…

    Linux干货 2016-12-05
  • 简单的shell脚本结合awk实现防止对web服务的dos攻击

    一 实验环境         鄙人使用的是centos 6.8操作系统   需要安装iptables(常见的linux系统貌似都会自动安装iptables)这条可以忽略   awk也需要安装没有的话就用yum装下吧       &…

    2017-04-24
  • Linux文件管理初步(第二周)

    第二周作业 1.Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示 目录管理管理类命令 mkdir make directories mkdir [OPTION]… DIRECTORY… 注意:路径基名作为命令的作用对象时,基名之前的路径必须要存在;     -p:自动按…

    Linux干货 2016-09-05
  • ldirectord 结合ipvsadm 配置nat,dr模型

    ldirectord 结合ipvsadm 配置nat,dr模型  一、nat模型 1、 drector # wget ftp://172.16.0.1/pub/Sources/7.x86_64/crmsh/ldirectord-3.9.6-0rc1.1.1.x86_64.rpm # yum -y install nginx (同时用于做为sorry…

    Linux干货 2014-01-03

评论列表(1条)

  • 马哥教育
    马哥教育 2016-11-02 17:08

    希望能加入一些自己的理解,排版可以在好一些的哈,加油!