echo安装,nginx编译过程中遇到的问题及解决方案:

错误信息:./configure: error: the HTTP XSLT module requires the libxml2/libxslt 缺少libxml2

解决办法:yum -y install libxml2 libxml2-dev && yum -y install libxslt-devel

错误信息:./configure: error: the HTTP image filter module requires the GD library. You can either do not enable the module or install the libraries.

解决方法:http_image_filter_module是nginx提供的集成图片处理模块,需要gd-devel的支持 yum -y install gd-devel

错误信息:./configure: error: perl module ExtUtils::Embed is required 缺少ExtUtils

解决方法:yum -y install perl-devel perl-ExtUtils-Embed

错误信息:./configure: error: the GeoIP module requires the GeoIP library. You can either do not enable the module or install the library. 缺少GeoIP

解决方法:yum -y install GeoIP GeoIP-devel GeoIP-data

一、负载均衡及高可用的集群概念

由多台服务器整合成一组同时对外服务的服务器集群,最前端设置一台调度服务器通过设置某种负载技术规则把大量的客户请求分担到后端真实服务器,平衡了高并发对后台真实服务器的负载压力,,大大缩短了客户响应时间,并且可以在前端调度器上做一个双主热备,以防单点故障,从而达到一个不间断给客户提供服务的高可用集群.

集群:是一组协同工作的服务实体,用以提供比单一服务实体更具扩展性与可用性的平台。

1、LB:负载均衡集群

LB负载均衡集群使任务可以在集群中尽可能平均地分摊不同的计算机进行处理充分利用集群的处理能力提高对任务的处理效率。

概念:使用专门的路由算法将数据包分散到多个真实服务器中进行处理,从而到达负载均衡的作用。当单台服务器性能升级到一定程度时,再升级就不会得到更大的效果,此时可以使用LB将请求分发到多个服务器上。

实现方式:

硬件方式:F5

软件方式:LVS、Nginx、HAProxy、Bind

2、HA:高可用集群

HA这类集群致力于提供高度可靠的服务。就是利用集群系统的容错性对外提供7*24小时不间断的服务如高可用的文件服务器、数据库服务等关键应用。

HA的有三种方式(主从式,对称式,集群式)

3、Hardware:硬件集群(F5:BIG-IP 、 A10)

4、Software:软件集群

(1)、区别

四层:lvs :(转发)

七层:nginx、haproxy:(反向代理)

(2)分类

Ipvsadm:管理lvs集群服务的命令行工具

Lvs:Linux Virtual Server(linux虚拟服务器)

NAT:**地址转换**

                集群节点跟director必须在同一个IP网络中;

                RIP通常是私有地址,仅用于各集群节点间的通信;

                Director位于client和real server之间,并负责处理进出的所有通信;

                Real server必须将网关指向DIP;

                支持端口映射;

                Real server可以使用任意OS;

                较大规模应用场景中,director易成为系统瓶颈

DR:**直接路由**(用最多)

        集群节点跟director必须在同一个物理网络中;

        RIP可以使用公网地址,实现便捷的远程管理和监控;

        Director仅负责处理入站请求,响应报文则由real server直接发往客户端;

        Real server不能将网关指向DIP;

        不支持端口映射;

TUN:**隧道**

集群节点可以跨越internet;

RIP必须是公网地址;

Director仅负责入站请求,响应报文则由real server直接发往客户端;

Real server网关不能指向director;

只有支持隧道功能的OS才能用于real server;

不支持端口映射;


Lvs支持的10种调度算法:

        四种静态:Rr、Wrr、Dh、Sh

        六种动态调度方法: Lc:最少连接;、              Wlc:加权最少连接;

                        Sed:最短期望延迟、           Nq:nerner queue;
                        、
                        LBLC:基于本地的最少连接;、LBLCR:基于本地的带复制功能的最少连接;

        默认方法:wlc(用的最多)        

二、Nginx 介绍

(1)nginx 的特点:

作为web服务器:处理静态文件、索引文件、自动索引的效率较高;

作为代理服务器:实现无缓存的反向代理加速,提高网站运行速度;

作为负载均衡服务器:其内部直接支持Rails 和 PHP,也可以支持HTTP 代理服务器对外进行服务;

在性能方面:采用 epoll 模型,可以支持更多的并发连接,最大可以支持5万并发连接数的响应;

在稳定性方面:支持热部署,平滑升级,启动速度快,可保证7*24小时不间断运行;

(2)Nginx 的企业级应用场景

使用Nginx 结合 FastCGI 运行 PHP、JSP、Perl等程序;

使用Nginx 做反向代理、负载均衡、规则规律;

使用Nginx 运行静态 HTMl 页、图片;

使用Nginx 加 cache 插件实现对web服务器缓存功能;

Nginx 与其他新技术的结合应用;

(3)Nginx 的处理请求过程

首先,Nginx 在启动时,会解析配置文件,得到需要监听的端口与IP 地址,然后在Nginx 的master 进程里面先初始化好这个监控的socket,再进行listen,然后再fork出多个子进程来,子进程会竞争 accept 新的连接。此时,客户端就可以向Nginx 发起连接了。当客户端与Nginx 进行三次握手,与Nginx 建立好一个连接后,此时,某一个子进程会 accept 成功,然后创建 Nginx 对连接的封装,即ngx_connection_t 结构体,接着,根据时间调用相应的事件处理模块,到此,一个连接就终止了。

二、select 与 epoll

1573311629351

三、Nginx 与 Apache 的比较

Nginx Apache
占用内存及资源少,可处理高并发请求。 rewrite功能强大
本身是反向代理服务器,负载均衡能力突出,可实现分布式架构及高可用集群。 动态页面处理能力强
社区活跃,高性能模块出品迅速 bug少,稳定性强
模块化设计,编写模块相对简单。

四、I/O多路复用

多路复用是指使用一个线程来检查多个文件描述符(Socket)的就绪状态,比如调用select和poll函数,传入多个文件描述符,如果有一个文件描述符就绪,则返回,否则阻塞直到超时。得到就绪状态后进行真正的操作可以在同一个线程里执行,也可以启动线程执行(比如使用线程池)。

这样在处理1000个连接时,只需要1个线程监控就绪状态,对就绪的每个连接开一个线程处理就可以了,这样需要的线程数大大减少,减少了内存开销和上下文切换的CPU开销。

img

五、Nginx的安装(源码安装)

rpm 包获取:http://nginx.org/packages/

源码包获取

http://nginx.org/download/nginx-0.1.14.tar.gz

解压目录:/usr/local/src/

安装目录:/opt/data/nginx/

配置文件:/etc/nginx/nginx.conf

站点目录:/data/nginx/

服务启动与停止:/etc/init.d/nginx {start | stop |restart |reload |status}

(1)安装环境准备,安装依赖包,创建安装目录

[root@localhost ~]# mkdir -p /opt/data/nginx/
[root@localhost ~]# groupadd nginx
[root@localhost ~]# useradd -g nginx nginx
[root@localhost ~]# yum install gcc gcc-c++ make recp pcre-devel openssl openssl-devel -y

(2)编译安装

解压至:cd /usr/local/src/
       cd nginx-1.14.0
./configure  --prefix=/opt/data/nginx  --with-http_stub_status_module  --with-http_ssl_module  --with-stream
make
make install

参数详解:

--prefix = 只想安装目录

--sbin-path = 指向(执行)程序文件(nginx)

--conf-path = 指向配置文件(nginx.conf)

--error-log-path = 指向错误日志目录

--http-log-path = 只想访问日志

--pid-path = 指向pid 文件(nginx-pid)

--lock-path = 指向lock 文件(nginx.lock)(安装文件锁定,防止安装文件被别人利用,或自己误操作。)

--user = 指定程序运行时的非特权用户

--group = 指定程序运行时的非特权用户组

--with-http_ssl_module  启用ngx_http_ssl_module 支持(使支持https请求,需已安装openssl)

--with-http_flv_module 启用ngx_http_flv_module 支持(提供寻求内存使用基于时间的偏移量文件)

--with-http_sub_module 启用ngx_http_sub_module 支持(允许用一些其它文本替换nginx响应中的一些文本)

--with-http_gzip_static_module 启用ngx_http_gzip_static_module 支持(在线实时压缩输出数据流)

--http-client-body-temp-path = 设定http客户端请求临时文件路径

--http-proxy-temp-path = 设定http代理临时文件路径

--http-fastcgi-temp-path = 设定http fastcgi 临时文件路径

--http-uwsgi-temp-path = 设定http uwsgi 临时文件路径

--http-scgi-temp-path = 设定 http scgi 临时文件路径

--with-prce 启用pcre 库

(3)配置环境变量

cd /opt/data/nginx/sbin   ./nginx -t

vim /etc/profile

末尾加export PATH=$PATH:/opt/data/nginx/sbin

source /etc/profile

(4)nginx 的配置

[root@localhost nginx]# ln -s /opt/data/nginx/conf /etc/nginx

[root@localhost nginx]# vim nginx.conf

                    user nginx;

                    pid /var/run/nginx.pid;

                    events {

                    worker_connections  1024;

                    }

                    http {

                    access_log off;

                    client_max_body_size 128M;

                    include /etc/nginx/conf.d/*.conf;

                    }

[root@localhost nginx]# mkdir /etc/nginx/conf.d

[root@localhost nginx]# mkdir /var/log/nginx

[root@localhost nginx]# chown -R nginx:nginx /var/log/nginx

(5)为 nginx 提供sysV init 脚本

vim /lib/systemd/system/nginx.service

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/opt/data/nginx/sbin/nginx -t -c /opt/data/nginx/conf/nginx.conf
ExecStart=/opt/data/nginx/sbin/nginx -c /opt/data/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

systemctl enable nginx

(5)启动 nginx

systemctl   start   nginx

systemctl status nginx

netstat -lntup | grep 80

lsof -i:80

六、Nginx虚拟主机

1.概念

所谓的虚拟主机,在web服务器里就是一个独立的网络站点,这个站点对应独立的域名(也可能是IP或端口),具有独立的程序及资源目录,可以独立地对外服务供用户访问

2.基于域名的虚拟主机

场景描述:公司业务业务扩展需求,将配置一下两个域名到已有的web服务器上,实现两个站点的独立访问。

环境:CentOS Linux release 7.4.1708 (Core)

域名1:www.xingyun.com

域名2:www.anttech.com

要求:1.网站数据分别存放在/data/xingyun/basic/和/data/anttech/basic下。

2.两网站的配置文件单独存放,日志文件分别存放在 /data/xingyun/log及/data/anttech/log下

(1)创建根域名对应的站点目录文件、日志文件

站点目录创建及授权:

[root@localhost nginx]# mkdir -p /data/xingyun/basic

[root@localhost nginx]# mkdir -p /data/anttech/basic

[root@localhost nginx]# chown -R nginx:nginx /data/xingyun/basic/

[root@localhost nginx]# chown -R nginx:nginx /data/anttech/basic/

日志目录文件的创建及授权:

[root@localhost conf.d]# mkdir -p  /data/xingyun/log

[root@localhost conf.d]# mkdir -p  /data/anttech/log

[root@localhost conf.d]# chown -R nginx:nginx /data/xingyun/log/

[root@localhost conf.d]# chown -R nginx:nginx /data/anttech/log/

(2)配置防火墙

[root@localhost conf.d]# sed -ri '/^SELINUX/c\SELINUX=disabled' /etc/selinux/config 

[root@localhost conf.d]# setenforce 0

[root@localhost conf.d]# firewall-cmd --permanent --add-service=http

[root@localhost conf.d]# firewall-cmd --permanent --add-servcie=https

[root@localhost conf.d]# firewall-cmd --reload

[root@localhost conf.d]# firewall-cmd --permanent --list-all

(3)修改配置文件

cd /etc/nginx/conf.d

vim xingyun.conf

server {
        listen       192.168.239.151:80;
        server_name  www.xingyun.com;
        access_log /data/xingyun/log/access.log combined;
        location / {
           root   /data/xingyun1/basic;
          index  index.html index.htm;
        }

}
vim anttech.conf

server {
        listen       192.168.239.151:80;
        server_name  www.anttech.com;
        access_log /data/anttech/log/access.log combined;
       location / {
           root   /data/xingyun2/basic;
            index  index.html index.htm;
       }
}

(4)添加域名解析:

C:\\windows\System32\drivers\etc\hosts

192.168.239.151 www.xingyun.com

192.168.239.151 www.anttech.com

结果测试:

[root@localhost conf.d]# echo "this is a test from xingyun" >> /data/xingyun/basic/index.html

[root@localhost conf.d]# echo "this is a test from anttech" >> /data/anttech/basic/index.html

3、基于端口的虚拟主机

场景描述:公司业务业务测试需求,需要将ww.xingyun.com的域名通过端口转发到独立站点空间上。

环境:CentOS Linux release 7.4.1708 (Core)

域名1:www.xingyun.com:8080

域名2:www.xingyun.com:6969

要求:1.网站数据分别存放在/data/xingyun1/basic/和/data/xingyun2/basic下。

2.两网站的配置文件单独存放,日志文件分别存放在/data/xingyun1/log及/data/xingyun2/log下。

(1)创建域名对应的站点目录文件

[root@localhost nginx]# mkdir -p /data/xingyun1/basic/index.html

[root@localhost nginx]# mkdir -p /data/xingyun2/basic/index.html

[root@localhost nginx]# chown -R nginx:nginx /data/xingyun1/basic/

[root@localhost nginx]# chown -R nginx:nginx /data/xingyun2/basic/

(2)日志目录文件的创建及授权:

[root@localhost conf.d]# mkdir -p  /data/xingyun1/log/access.log

[root@localhost conf.d]# mkdir -p  /data/xingyun2/log/access.log

[root@localhost conf.d]# chown -R nginx:nginx /data/xingyun1/log/

[root@localhost conf.d]# chown -R nginx:nginx /data/xingyun2/log/

(3)配置防火墙:

[root@localhost data]# firewall-cmd --zone=public --add-port=8080/tcp --permanent

success

[root@localhost data]# firewall-cmd --zone=public --query-port=8080/tcp --permanent

yes

[root@localhost data]# firewall-cmd --zone=public --add-port=6969/tcp --permanent

success

[root@localhost data]# firewall-cmd --zone=public --query-port=6969/tcp --permanent

yes

[root@localhost data]# firewall-cmd --reload

(4)修改配置文件:

6969端口:

[root@localhost ~]# cd /etc/nginx/conf.d/

server {
        listen       192.168.239.151:6969;
        server_name  www.xingyun.com;
        access_log /data/xingyun1/log/access.log combined;
        location / {
            root   /data/xingyun1/basic;
            index  index.html index.htm;
        }
}

8080端口:

[root@localhost conf.d]# vim anttech.conf 

server {
        listen       192.168.239.151:8080;
        server_name  www.xingyun.com;
        access_log /data/xingyun2/log/access.log combined;
       location / {
           root   /data/xingyun2/basic;
            index  index.html index.htm;
       }
}

(5)结果测试:

6969端口测试

80端口测试

4、基于IP的虚拟主机

场景描述:公司业务业务测试需求,需要使用内网IP转发请求到到web服务器上,并访问独立站点空间。

环境:CentOS Linux release 7.4.1708 (Core)

IP1:10.0.0.1

IP2:10.0.0.2

要求:1.网站数据分别存放在/data/xingyun1/basic/和/data/xingyun2/basic下。

2.两网站的配置文件单独存放,日志文件分别存放在/data/xingyun1/log及/data/anttech2/log下。

(1)创建域名对应的站点目录文件(同上)

[root@localhost nginx]# mkdir -p /data/xingyun1/basic/index.html

[root@localhost nginx]# mkdir -p /data/xingyun2/basic/index.html

[root@localhost nginx]# chown -R nginx:nginx /data/xingyun1/basic/

[root@localhost nginx]# chown -R nginx:nginx /data/xingyun2/basic/
[root@localhost conf.d]# mkdir -p  /data/xingyun1/log/
touch access.log

[root@localhost conf.d]# mkdir -p  /data/xingyun2/log/
touch access.log

[root@localhost conf.d]# chown -R nginx:nginx /data/xingyun1/log/

[root@localhost conf.d]# chown -R nginx:nginx /data/xingyun2/log/

(2)配置防火墙

[root@localhost data]# firewall-cmd --zone=public --add-port=8080/tcp --permanent

success

[root@localhost data]# firewall-cmd --zone=public --query-port=8080/tcp --permanent

yes

[root@localhost data]# firewall-cmd --zone=public --add-port=6969/tcp --permanent

success

[root@localhost data]# firewall-cmd --zone=public --query-port=6969/tcp --permanent

yes

[root@localhost data]# firewall-cmd --reload

(3)在网卡上添加多个IP

[root@localhost conf.d]# ip addr add 10.0.0.8/24 dev ens33
[root@localhost conf.d]# ip addr add 10.0.0.9/24 dev ens33
[root@localhost conf.d]# ip add|grep 10.0.0
                    inet 10.0.0.8/24 scope global ens33
                    inet 10.0.0.9/24 scope global secondary ens33

(4)修改配置文件

10.0.0.8

[root@localhost conf.d]# vim /etc/nginx/conf.d/xingyun.conf 

server {
        listen       10.0.0.8:80;
        server_name  www.xingyun.com;
        access_log /data/xingyun1/log/access.log combined;
        location / {
            root   /data/xingyun1/basic;
            index  index.html index.htm;
        }
}

10.0.0.9

[root@localhost conf.d]# vim /etc/nginx/conf.d/anttech.conf 

server {
        listen       10.0.0.9:80;
        server_name  www.xingyun.com;
        access_log /data/xingyun2/log/access.log combined;
       location / {
           root   /data/xingyun2/basic;
            index  index.html index.htm;
       }
}

(5)在此电脑的C盘添加域名解析:

C:\Windows\System32\drivers\etc

10.0.0.8 192.168.239.151
10.0.0.9 192.168.239.151
192.168.239.151 www.anttech.com
192.168.239.151 www.xingyun.com 

(6)在另一台主机上配置路由:

[root@localhost ~]# ip route add 10.0.00/24 via 192.168.239.151 dev ens33
[root@localhost ~]# ip route show
default via 192.168.239.2 dev ens33 proto static metric 100 
10.0.0.0/24 via 192.168.239.151 dev ens33 
192.168.239.0/24 dev ens33 proto kernel scope link src 192.168.239.150 metric 100 

(7)结果测试

[root@localhost conf.d]# echo "this is a test from 10.0.0.9">>/data/xingyun1/basic/index.html 

[root@localhost conf.d]# echo "this is a test from 10.0.0.8">>/data/xingyun2/basic/index.html 

[root@localhost conf.d]# nginx -t
nginx: the configuration file /opt/data/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/data/nginx/conf/nginx.conf test is successful
[root@localhost conf.d]# systemctl restart nginx

在另一台主机进行连接:

[root@localhost ~]# ping 10.0.0.8
PING 10.0.0.8 (10.0.0.8) 56(84) bytes of data.
64 bytes from 10.0.0.8: icmp_seq=1 ttl=64 time=0.218 ms
64 bytes from 10.0.0.8: icmp_seq=2 ttl=64 time=0.186 ms
64 bytes from 10.0.0.8: icmp_seq=3 ttl=64 time=0.252 ms
^Z
[4]+  已停止               ping 10.0.0.8
[root@localhost ~]# ping 10.0.0.*** 10.0.0.9 (10.0.0.9) 56(84) bytes of data.
64 bytes from 10.0.0.9: icmp_seq=1 ttl=64 time=0.242 ms
64 bytes from 10.0.0.9: icmp_seq=2 ttl=64 time=0.207 ms
^Z
[5]+  已停止               ping 10.0.0.9
[root@localhost ~]# curl 10.0.0.9
This is a test from xingyun2
this is a test from 8080
this is a test from 10.0.0.8
[root@localhost ~]# curl 10.0.0.8
this is a test from xingyun1
This is a test from 6969

四、Location 规则

1、规则介绍

URI:Web上可用的每种资源 HTML文档、图像、视频片段、程序等 - 由一个通过通用资源标志符(Universal Resource Identifier,
简称"URI")进行定位。

URL:是Internet上用来描述信息资源的字符串,主要用在各种WWW客户程序和服务器程序上。采用URI可以用一种统一的格式来描述各种信息资源,包括文件、服务器的地址和目录等。

nginx就是通过拦截到的请求去对配置好的location块(location block)进行请求代理的。被代理的url去对location后边的字符串(或正则)进行匹配,根据一定的规则选择走哪个location。

2、Location 规则及其优先级

语法规则:
Location[=|~*|!~|!~*|^~]/uri/{
    ....
}
=:表示精确匹配,优先级也是最高的
^~:表示uri以某个常规字符串开头,理解为匹配 url 路径即可
~ :表示区分大小写的正则匹配
~* :表示区分大小写的不正则匹配
!~ :表示不区分大小写,匹配正则
!~* :表示不区分大小写,不匹配正则
/ :通用匹配,任何请求都会匹配到

Location 优先级:
=  >  ^~  >  ( ~ ~* !~ !~* ) > /
相同的优先级,按配置文件顺序一次匹配。

1573353757075

3、Location规则(前提需要echo第三方模块)

(1)安装第三方模块

(重新对nginx增加echo模块并进行编译)

[root@localhost ~]tar -xf echo-nginx-module-0.61.tar.gz -C /usr/local/src/

[root@localhost nginx-1.14.0]# cd /usr/local/src/

[root@localhost nginx-1.14.0]# cd nginx-1.14.0/

[root@localhost nginx-1.14.0]#  ./configure --prefix=/opt/data/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --add-module=/usr/local/src/echo-nginx-module-0.61

[root@localhost nginx-1.14.0]# make

cp objs/nginx /usr/local/nginx/sbin/nginx

[root@localhost sbin]# systemctl restart nginx

(2)实例:

[root@localhost conf.d]# cd /etc/nginx/conf.d

[root@localhost ~]# curl http://www.anttech.com

[root@localhost ~]# curl http://www.anttech.com/123.jpg

[root@localhost ~]# curl http://www.anttech.com/nginx

[root@localhost ~]# curl http://www.anttech.com/nginx/111

[root@localhost ~]# curl http://www.anttech.com/static

[root@localhost ~]# curl http://www.anttech.com/static/

[root@localhost ~]# curl http://www.anttech.com/static/222

[root@localhost ~]# curl http://www.anttech.com/11.jpg

[root@localhost ~]# curl http://www.anttech.com/11.JPG

[root@localhost ~]# curl http://www.anttech.com/11.png

[root@localhost ~]# curl http://www.anttech.com/11.PNG

五、Nginx地址重写 rewrite

1.rewrite 介绍

rewrite 又称URL rewrite,即URL重写,就是把传入的web的请求重定向到URL的过程。

URL Rewrite最常见的应用是URL伪静态化,是将动态页面显示为静态页面方式的一种技术。比如
http://www.123.com/news/index.php?id=123 使用URLRewrite 转换后可以显示为 http://www.123.com/news/123.html对于追求完美主义的网站设计师,就算是网页的地址也希望看起来尽量简洁明快。
理论上,搜索引擎更喜欢静态页面形式的网页,搜索引擎对静态页面的评分一般要高于动态页面。所以,UrlRewrite可以让我们网站的网页更容易被搜索引擎所收录。

从安全角度上讲,如果在URL中暴露太多的参数,无疑会造成一定量的信息泄漏,可能会被一些黑客利用,对你的系统造成一定的破坏,所以静态化的URL地址可以给我们带来更高的安全性。

实现网站地址跳转,例如用户访问360buy.com,将其跳转到jd.com。例如当用户访问xingyun.com的80端口时,将其跳转到443端口。

2.Nginx 地址重写语法

Nginx Rewrite 相关指令有if 、rewrite 、set 、return 等。

(1)if 语句

应用环境:server,location

语法:

if (condition){...}

if 可以支持如下条件判断匹配符号

~ :正则匹配

~* :正则匹配(不区分大小写)

!~ :正则不匹配(区分大小写)

!~* :正则不匹配(不区分大小写)

-f 和 !-f :用来判断是否存在文件

-d 和 !-d :用来判断目录是否存在

-x 和 !-x :用来判断文件是否可执行

(2)Nginx匹配过程中的全局变量

$args                  请求中的参数;

$document_root      针对当前请求的根路径设置值;

$host          请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名;

$limit_rate          对连接速率的限制;

$request_method      请求的方法,比如"GET"、"POST"等;

$remote_addr          客户端地址;

$remote_port          客户端端口号;

$remote_user          客户端用户名,认证用;

$request_filename   当前请求的文件路径名(带root指定的路径)

$request_uri      当前请求的文件路径名(不带root指定的路径 /images/a.jpg)

$query_string          与$args相同;

$scheme              用的协议,比如http或者是https

$server_protocol      请求的协议版本,"HTTP/1.0"或"HTTP/1.1";

$server_addr           服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址;

$server_name          请求到达的服务器名;

$document_uri       与$uri一样,URI地址;

$server_port           请求到达的服务器端口号;

(3)rewrite

rewrite  指令根据表达式来重定向URI,或者修改字符串。可以应用于server,location,
if环境下每行rewrite指令最后跟一个flag标记,支持的flag标记有:

last             相当于Apache里的[L]标记,表示完成rewrite
break             本条规则匹配完成后,终止匹配,不再匹配后面的规则
redirect         返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent         返回301永久重定向,浏览器地址会显示跳转后URL地址

redirect 和 permanent区别则是返回的不同方式的重定向,对于客户端来说一般状态下是没有区别的。而对于搜索引擎,相对来说301的重定向更加友好,如果我们把一个地址采用301跳转方式跳转的话,搜索引擎会把老地址的相关信息带到新地址,同时在搜索引擎索引库中彻底废弃掉原先的老地址。使用302重定向时,搜索引擎(特别是google)有时会查看跳转前后哪个网址更直观,然后决定显示哪个,如果它觉的跳转前的URL更好的话,也许地址栏不会更改,那么很有可能出现URL劫持的现像。在做URI重写时,有时会发现URI中含有相关参数,如果需要将这些参数保存下来,并且在重写过程中重新引用,可以用到 () 和 $N 的方式来解决。

六、Nginx 常用模块原理实现

1.常用模块介绍

模块 作用
with-http_stub_status_module 记录nginx访问基本状态信息的模块
ngx_http_access_module 访问控制模块,用来控制网站用户对nginx的访问。
ngx_http_rewrite_module URL地址重写模块
ngx_http_gzip_module 启用ngx_http_gzip_static_module支持(在线实时压缩输出数据流)
ngx_http_ssl_module 启用ngx_http_ssl_module支持(使支持https请求,需已安装openssl)
ngx_http_proxy_module proxy代理模块
ngx_http_log_module 访问日志模块,以指定的格式记录nginx客户访问日志等信息。
ngx_http_fastcgi_module FastCGI模块,和动态应用相关的模块,如PHP
ngx_http_upstream_module 负载均衡模块,可以实现网站的负载均衡能力及节点的健康检查。
nginx lua nginx lua防刷规则模型