nginx(发音同engine x)是一款轻量级的web服务器/反向代理服务器及电子邮件(imap/pop3)代理服务器,并在一个bsd-like协议下发行。
nginx由俄罗斯的程序设计师igor sysoev所开发,最初供俄国大型的入口网站及搜寻引擎rambler使用。
第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类bsd许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
nginx的特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
nginx是一个很牛的高性能web和反向代理服务器,它具有很多非常优越的特性:
nginx由内核和模块组成。其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block(location是nginx配置中的一个指令,用于url匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。
nginx的模块从结构上分为核心模块、基础模块和第三方模块
用户根据自己的需要开发的模块都属于第三方模块。正是有了如此多模块的支撑,nginx的功能才会如此强大
nginx模块从功能上分为三类,分别是:
nginx模块分为:核心模块、事件模块、标准http模块、可选http模块、邮件模块、第三方模块和补丁等
具体的指令,请参考nginx的官方文档
nginx的模块直接被编译进nginx,因此属于静态编译方式。
启动nginx后,nginx的模块被自动加载,与apache不一样,首先将模块编译为一个so文件,然后在配置文件中指定是否进行加载。
在解析配置文件时,nginx的每个模块都有可能去处理某个请求,但是同一个处理请求只能由一个模块来完成。
nginx的进程架构:
启动nginx时,会启动一个master进程,这个进程不处理任何客户端的请求,主要用来产生worker线程,一个worker线程用来处理n个request。
主配置文件:/usr/local/nginx/conf/nginx.conf
配置文件 | 作用 |
---|---|
nginx.conf | nginx的基本配置文件 |
mime.types | mime类型关联的扩展文件 |
fastcgi.conf | 与fastcgi相关的配置 |
proxy.conf | 与proxy相关的配置 |
sites.conf | 配置nginx提供的网站,包括虚拟主机 |
6.1 nginx.conf配置详解
配置指令:要以分号结尾,语法格式如下:
derective value1 [value2 ...]
支持使用变量:
daemon {on|off}; #是否以守护进程方式运行nginx,调试时应设置为off master_process {on|off}; #是否以master/worker模型来运行nginx,调试时可以设置为off error_log 位置 级别; #配置错误日志
error_log里的位置和级别能有以下可选项:
[root@hyrule ~]# useradd -r -m -s /sbin/nologin nginx [root@hyrule ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ [root@hyrule ~]# yum -y groups mark install 'development tools' [root@hyrule ~]# mkdir -p /var/log/nginx [root@hyrule ~]# chown -r nginx.nginx /var/log/nginx [root@hyrule ~]# cd /usr/src/ [root@hyrule src]# wget http://nginx.org/download/nginx-1.14.2.tar.gz [root@hyrule src]# tar xf nginx-1.14.2.tar.gz [root@hyrule src]# cd nginx-1.14.2 [root@hyrule nginx-1.14.2]# ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-debug \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_image_filter_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --http-log-path=/var/log/nginx/access.log \ --error-log-path=/var/log/nginx/error.log [root@hyrule nginx-1.14.2]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
[root@hyrule ~]# echo 'export path=/usr/local/nginx/sbin:$path' > /etc/profile.d/nginx.sh [root@hyrule ~]# . /etc/profile.d/nginx.sh [root@hyrule ~]# nginx [root@hyrule ~]# ss -antl state recv-q send-q local address:port peer address:port listen 0 128 *:80 *:* listen 0 128 *:22 *:* listen 0 100 127.0.0.1:25 *:* listen 0 128 *:10050 *:* listen 0 128 :::22 :::* listen 0 100 ::1:25 :::* listen 0 80 :::3306 :::*
#编写nginx的启动脚本 [root@hyrule ~]# vim nginx_service.sh #!/bin/bash # # chkconfig: - 85 15 # description: nginx # processname: nginx nginx=/usr/local/nginx/sbin/nginx conf=/usr/local/nginx/conf/nginx.conf case $1 in start) $nginx -c $conf ;; stop) killall -9 nginx ;; restart) killall -9 nginx $nginx -c $conf *) echo "usage: service {start|stop|restart}" ;; [root@hyrule ~]# chmod +x nginx_service.sh #将 [root@hyrule ~]# cp nginx_service.sh /etc/init.d/nginx [root@hyrule ~]# chkconfig –add nginx
如对本文有疑问, 点击进行留言回复!!
linux下文本编辑器vim的使用方法(复制、粘贴、替换、行号、撤销、多文件操作)
网友评论