Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个IMAP/POP3/SMTP 代理服务器。

Nginx优点:

1) 高并发响应性能非常好,官方 Nginx 处理静态文件并发 5w/s

2) 反向代理性能非常强。(可用于负载均衡)

3) 内存和 cpu 占用率低。(为 Apache 的 1/5-1/10)

4) 对后端服务有健康检查功能。

5) 支持 PHP cgi 方式和 fastcgi 方式。

6) 配置代码简洁且容易上手。

Nginx作为Web服务器应用场景:

1.使用Nginx运行HTML.CSS.JS等静态数据

2.Nginx+FastCGI运行php等动态程序

3.Nginx+tomact/resin等支持java动态程序

4.反向代理服务器

正向代理服务器:替代我去访问网络;

反向代理服务器:替代顾客访问服务器,如lvs;

模型:

nginx使用模型epoll模型,apache使用模型select模型,两者区别:

Select 特点:select 选择句柄的时候,是遍历所有句柄,也就是说句柄有事件响应时,select 需要遍历所有句柄才能获取到哪些句柄有事件通知,因此效率是非常低。

好比:去学校找李四同学,宿管大妈带你寻遍每个宿舍来寻找直到找到;

epoll 的特点:epoll 对于句柄事件的选择不是遍历的,是事件响应的,就是句柄上事件来就马上选择出来,不需要遍历整个句柄链表,因此效率非常高 。

好比:去学校找张三同学,宿管大妈根据学号等信息找到他住在哪个宿舍,找到你

安装nginx步骤如下

1 wget http://nginx.org/download/nginx-1.14.2.tar.gz2 tar -zxvf nginx-1.14.2.tar.gz -C /usr/local/src/3 cd /usr/local/src/nginx-1.14.24 ./configure --help5 ./configure --prefix=/usr/local/nginx-1.14.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module6 yum install gcc -y #nginx所需安装包7 ./configure --prefix=/usr/local/nginx-1.14.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module8 yum install pcre-devel -y#nginx所需安装包9 ./configure --prefix=/usr/local/nginx-1.14.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module10 yum install openssl-devel -y#nginx所需安装包11 ./configure --prefix=/usr/local/nginx-1.14.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module12 make && make install13 history

查看编译时的参数

[root@www sbin]# ./nginx -Vnginx version: nginx/1.14.2built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)built with OpenSSL 1.0.1e-fips 11 Feb 2013TLS SNI support enabledconfigure arguments: --prefix=/usr/local/nginx-1.14.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module[root@www sbin]#

使用网页检查web服务器是否配置正常,直接在网页中输入ip:80测试

需要关闭linux防火墙

[root@www conf]# chkconfig --levels 2345 iptables off[root@www conf]# /etc/init.d/iptables stop

在linux中测试,两种方法

[root@www conf]# wget 192.168.6.6[root@www conf]# curl -l 192.168.6.6

启动、关闭、重新加载配置方法:

1.启动,直接执行可执行文件[root@WebA-136 nginx]# ./sbin/nginx[root@WebA-136 nginx]# lsof -i :80COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAMEnginx   4497 root    6u  IPv4  23796      0t0  TCP *:http (LISTEN)nginx   4498  www    6u  IPv4  23796      0t0  TCP *:http (LISTEN)nginx   4499  www    6u  IPv4  23796      0t0  TCP *:http (LISTEN)2.直接关闭nginx[root@WebA-136 nginx]# ./sbin/nginx -s stop3.优雅关闭nginx,当nginx处理完当前请求后关闭[root@WebA-136 nginx]# ./sbin/nginx -s quit4.当修改了配置文件后,重新加载匹配文件到内存中[root@WebA-136 nginx]# ./sbin/nginx -s reload5.重新打开日志文件[root@WebA-136 nginx]# ./sbin/nginx -s reopen

nginx配置文件

指令上下文:多个作用域,称为指令上下文。

main:nginx在运行时与具体业务功能(比如http服务或者email服务代理)无关的一些参数,比如工作进程数,运行的身份等

http:与提供http服务相关的一些配置参数。例如:是否使用keepalive啊,是否使用gzip进行压缩等

server:http服务上支持若干虚拟主机。每个虚拟主机一个对应的server配置项,配置项里面包含该虚拟主机相关的配置。在提供mail服务的代理时,也可以建立若干server.每个server通过监听的地址来区分

location:http服务中,某些特定的URL对应的一系列配置项

mail:实现email相关的SMTP/IMAP/POP3代理时,共享的一些配置项(因为可能实现多个代理,工作在多个监听地址上)

nginx应用场景:

一、nginx最最最基本的功能之一:提供静态服务

一个web服务器提供静态文件服务(图片/HTML.CSS.JS等静态数据),在http块中设置server块,在server块中设置location块,如下最简单的配置文件~

[root@www conf]# cat /usr/local/nginx/conf/nginx.confworker_processes  2;                #开启几个worker,一般cpu核数相同数量events {    worker_connections  1024;          #每个worker可以处理的请求数量}http {    include       mime.types;       #nginx支持的媒体类型库文件包含。    default_type  application/octet-stream;   #默认的媒体类型    sendfile        on;          #开启高效传输模式    keepalive_timeout  65;           #链接超时    server {                   #一个server是一个站点        listen       80;        server_name  haifu.com;        location / {            #location区块,默认匹配的   root指定文件目录,index是默认文件            root   html;                #设置请求的根目录,此指令可以存在 http,server,location,if in location中            index  index.html index.htm;        }        error_page   500 502 503 504  /50x.html; #若没有找到默认的就返回的页面        location = /50x.html {             #另一个location区块            root   html;        }    }}[root@www conf]#[root@WebA-136 nginx]# ./sbin/nginx -t  #检查配置文件的语法是否错误。nginx: the configuration file /usr/local/nginx-1.15.8/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx-1.15.8/conf/nginx.conf test is successful

二、简单代理服务器

以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,简单来说就是真实的服务器不能直接被外部网络访问,想要访问必须通过代理

实例:

1.下面代码表示:当请求不匹配location中时,会使用root根目录下的index,server {    listen 8080;    root /data/up1;#此root在server上下文中哦~    index index.html;    location / {    }}2.继续修改上面的配置,server {    location / {        proxy_pass  #需要代理的服务器名称、端口、协议(http)    }    location /images/ {        root /data;    }}3.我配置的简单如下:代理服务器:    server {        server_name month;        location / {          proxy_pass http://daili:8080;                }        }服务器:[root@WebB-137 nginx]# cat conf/nginx.confworker_processes  1;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;    server {        listen       8080;    #端口        server_name  daili;    #服务器名称        location / {            root   html;            index  index.html index.htm;        }        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }}或者是:使用location指定特定的或具体的URL后才作为代理服务器使用:location /documents/ {    proxy_pass http://daili:8080;}

image.png

三、设置FastCGI代理

nginx可用于将请求到FastCGI服务器,这些服务器运行各种框架和PHP等编程语言构建的应用程序。

略过

nginx一些概念~

1.虚拟主机:

是web服务里一个独立的网站站点,这个站点对应独立的域名或者ip地址或端口,具有独立的程序和资源,可以独立对外提供服务。

apache里是使用标签<virtuaHost>标识,nginx里是使用server{}标识。

类型有:基于ip地址/端口/域名三种

基于域名区分主机是最常用的。常用于外部网站;

基于端口区分主机,常用于公司内部网站;

基于IP地址区分主机,不常用。

实例1.基于域名的(配置简单~)

[root@WebA-136 nginx]# cat conf/nginx.confworker_processes  2;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;    server {        listen       80;        server_name  year;        location / {            root   html/year;            index  index.html index.htm;        }        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }    server {        listen  80;        server_name month;        location / {                root html/month;                index index.html;                }        }    server {        listen 80;        server_name day;        location / {                root html/day;                index index.html;                }        }}

实例2,基于端口(配置简单)

[root@WebA-136 nginx]# cat  conf/nginx.confworker_processes  2;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;    server {        listen       80;        server_name  year;        location / {            root   html/year;            index  index.html index.htm;        }        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }    server {        listen  81;        server_name year;        location / {                root html/month;                index index.html;                }        }    server {        listen 82;        server_name year;        location / {                root html/day;                index index.html;                }        }}[root@WebA-136 nginx]# netstat -altunp | grep nginx   #存在3个端口tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3916/nginxtcp        0      0 0.0.0.0:81                  0.0.0.0:*                   LISTEN      3916/nginxtcp        0      0 0.0.0.0:82                  0.0.0.0:*                   LISTEN      3916/nginx[root@WebA-136 nginx]#

实例3.基于ip的虚拟主机设置

[root@WebA-136 nginx]# ifconfig eth0:0 192.168.146.140 up[root@WebA-136 nginx]# ip addr add 192.168.146.141 dev eth0 label eth0:1[root@WebA-136 nginx]# cat conf/nginx.confworker_processes  2;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;    server {        listen 192.168.146.136:80;#       server_name  year;        location / {            root   html/year;            index  index.html index.htm;        }        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }    server {        listen  192.168.146.140:80;#       server_name year;        location / {                root html/month;                index index.html;                }        }    server {        listen 192.168.146.141:80;#       server_name year;        location / {                root html/day;                index index.html;                }        }}注意:此时需要的重启nginx,不是平滑启动。[root@WebA-136 nginx]# ./sbin/nginx -s stop[root@WebA-136 nginx]# ./sbin/nginx[root@WebA-136 nginx]# curl -l 192.168.146.141day

Welcome!

day

The nginx is working.

chaxue

nginx.org.
Commercial support is available at
nginx.com.

Thank you for using nginx.

[root@WebA-136 nginx]#

一般在工作中,我们会将多个站点放在不同的配置文件中,如下配置

1.在conf目录下新建extra目录2.在extra目录下新建站点配置文件如下,year.conf month.conf day.conf[root@WebA-136 nginx]# ls conf/extra/day.conf  month.conf  year.conf3.将nginx.conf配置文件中站点信息放入三个配置文件中[root@WebA-136 nginx]# sed -n '10,21p' ../nginx.conf.bak > year.conf[root@WebA-136 nginx]# sed -n '22,29p' ../nginx.conf.bak > month.conf[root@WebA-136 nginx]# sed -n '30,37p' ../nginx.conf.bak > day.conf4.修改nginx.conf配置文件如下[root@WebA-136 nginx]# cat conf/nginx.confworker_processes  2;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;    include extra/year.conf;    include extra/month.conf;    include extra/day.conf;}[root@WebA-136 nginx]#[root@WebA-136 nginx]# ./sbin/nginx -t  #检查配置文件语法nginx: the configuration file /usr/local/nginx-1.15.8/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx-1.15.8/conf/nginx.conf test is successful[root@WebA-136 nginx]# ./sbin/nginx -s reload  #平滑启动,如果不生效就重新启动

为基于域名虚拟主机增加别名,如下

[root@WebA-136 nginx]# cat conf/extra/year.conf    server {        listen       80;        server_name  year good;            #使用空格 后增加名字即可。        location / {            root   html/year;            index  index.html index.htm;        }        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }

别名用途:30台服务器所做工作一样,主域名一样,别名不同。可通过监控别名来判断哪台服务器由差错。

模块:2大类模块,包含核心模块(Core functionality)和http模块(标准http模块 可选http模块 邮件模块 流模块 第三方模块)

http模块介绍:

1.状态模块with-http_stub_status_module用途:记录nginx的基本访问状态信息,让使用者了解nginx的工作状态,如:连接数信息。

    server {        listen       80;        server_name  year good;        location / {            root   html/year;            index  index.html index.htm;            stub_status on; #打开            access_log off; #不记录日志            allow 192.168.146.1/24;            deny all;

显示如下:

image.png

字段:

active connections:nginx正在处理的链接数量

server:共处理多少链接,accepts:创建多少次握手,handled requests:处理多少次请求。

reading:读取客户端的header数量,writing:返回客户端的header数量,waiting:已经处理完正在等待下一次请求指令的驻留链接。

2.访问日志模块ngx_http_log_module,功能将每个用户访问网站的日志记录到指定的文件中。

指令有:

access_log 指定日志路径及使用何种日志格式

log_format 定义日志格式,可定义多种取不同名称即可

open_log_file_cache 影响静态文件更新时间

日志格式字段介绍:

字段

含义

$remote_addr
记录访问网站的客户端地址
$remote_user 远程客户端用户名称
$time_local 记录访问时间和时区
$request_time 以毫秒为单位记录请求处理时间,从接收到第一个字节到返回客户端最后一个字节计算

$request_length

请求长度(请求行、标题、正文)
$request http请求起始行信息
$status 状态码,记录请求返回的状态
$http_referer 记录此请求是从哪个链接访问过来的
$http_x_forwarded_for
当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器也设置了http_x_forwarded_for
$http_user_agent 记录客户端访问信息,浏览器或手机客户端等
$bytes_sent 发送到客户端的字节数

3.http核心模块,ngx_http_core_module

常用指令http、location、server等。

location:根据用户请求的URI来执行不同的功能,根据用户匹配的网站URL进行匹配,匹配成功就执行相关操作。

作用于:server, location中。

官方文档location语法如下:

Syntax:location [ = | ~ | ~* | ^~ ] uri { ... }location @name { ... }Default:—Context:server, location

=:精确匹配,完全匹配,最优先匹配的。

~ :区分大小写

~*:不区分大小写

^~:不检查正则表达式,只做常规匹配

URI修饰由前缀或正则表示,匹配规则如下:

1.优先匹配精确匹配;

2.匹配常规字符,不匹配正则,若有多个匹配成功就匹配最长匹配的那个;

3.正则匹配;

4.匹配常规字符,有正则优先正则;

5.location默认匹配。

实例如下:

    server {        listen 80;        server_name day;        location / {                root html/day;                index index.html;                }        location = / {                return 404;                }        location /documents/ {                return 402;                }        location ^~ /images/ {                return 403;                }        location ~* \.(gif|jpg|jpeg)$ {                return 500;                }        }

4.ngx_http_rewrite_module模块,常用指令break、if、return、rewrite、rewrite_log、set。

rewrite主要功能:实现URL地址重写。

rewrite需要PCRE软件支持,需安装。

应用位置:server, location, if

指令语法:rewrite regex replacement [flag];如果指定的正则表达式与请求的URL匹配,URL就改写为replacement并跳转

flag有:last(停止处理当前模块指定集,收集与更改的URL匹配的位置)、break(停止当前指令集)、redirect(302临时重定向)、permanent(301永久重定向)。

非常简单的URL跳转:

    server {        listen  80;        server_name month;        location / {          rewrite ^/(.*) http://day/$1 permanent;#$1值取自()内的数据                }        }

在浏览器输入http://month/documents/,则会跳转到http://day/documents/,根据上面提到的location案列显示402.

应用场景:

1.调整用户输入的URL,规范;

2.为了让搜索引擎收录网站内容及用户体验好,将动态URL地址伪装成静态地址提供服务;

3.网站更换域名后,让旧的域名跳转到新的域名上;如输入www.360buy.com会跳转到www.jd.com上。

4.根据特殊变量、目录、客户端的信息进行URL跳转。

5.访问认证

模块ngx_http_auth_basic_module,功能使用“http基本身份认证”协议验证用户名和密码来限制对资源的访问。

常用指令:auth_basic、auth_basic_user_file

auth_basic:

语法auth_basic string | off;默认关闭提示功能;也可以设置~

auth_basic_user_file:

语法auth_basic_user_file file;

[root@WebA-136 nginx]# cat conf/extra/year.conf    server {        listen       80;        server_name  year good;       access_log logs/access.log ;       location / {            root   html/year;            index  index.html index.htm;#           stub_status on;#           access_log off;            auth_basic "tishi";                    #提示信息            auth_basic_user_file /usr/local/nginx/conf/htpasswd;   #密码文件位置        }        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }    [root@WebA-136 nginx]# htpasswd -cb /usr/local/nginx/conf/htpasswd test test   #设置密码文件,-c指定文件,-b非交互式输入密码  密码文件,用户名 密码Adding password for user tes

image.png