diff --git a/nginx-md/第三章:Nginx特性.md b/nginx-md/第三章:Nginx特性.md new file mode 100644 index 0000000..dc1a372 --- /dev/null +++ b/nginx-md/第三章:Nginx特性.md @@ -0,0 +1,564 @@ +
+
+#### 2.反向代理
+
+ 反向代理的过程隐藏了真实的服务器,客户不知道真正提供服务的人是谁,客户端请求的服务都被代理服务器处理。反向代理代理的是响应方,也就是服务端;我们请求www.baidu.com时这www.baidu.com就是反向代理服务器,真实提供服务的服务器有很多台,反向代理服务器会把我们的请求分转发到真实提供服务的各台服务器。Nginx就是性能非常好的反向代理服务器,用来做负载均衡
+
+
+
+ 反向代理中,proxy和server同属一个LAN
+
+
+
+#### 3.Nginx Proxy
+
+模块:ngx_http_proxy_module
+
+代理配置:
+
+```shell
+代理
+Syntax: proxy_pass URL; #代理的后端服务器URL
+Default: —
+Context: location, if in location, limit_except
+
+缓冲区
+Syntax: proxy_buffering on | off;
+Default: proxy_buffering on; #缓冲开关
+Context: http, server, location
+proxy_buffering开启的情况下,nignx会把后端返回的内容先放到缓冲区当中,然后再返回给客户端
+(边收边传,不是全部接收完再传给客户端)。
+
+Syntax: proxy_buffer_size size;
+Default: proxy_buffer_size 4k|8k; #缓冲区大小
+Context: http, server, location
+
+Syntax: proxy_buffers number size;
+Default: proxy_buffers 8 4k|8k; #缓冲区数量
+Context: http, server, location
+
+Syntax: proxy_busy_buffers_size size;
+Default: proxy_busy_buffers_size 8k|16k;#忙碌的缓冲区大小控制同时传递给客户端的buffer数量
+Context: http, server, location
+
+头信息
+Syntax: proxy_set_header field value;
+Default: proxy_set_header Host $proxy_host; #设置真实客户端地址
+ proxy_set_header Connection close;
+Context: http, server, location
+
+超时
+Syntax: proxy_connect_timeout time;
+Default: proxy_connect_timeout 60s; #链接超时
+Context: http, server, location
+
+Syntax: proxy_read_timeout time;
+Default: proxy_read_timeout 60s;
+Context: http, server, location
+```
+
+启用 nginx proxy 代理:
+
+ nginx-1 启动网站(内容)(作为网站服务器)
+
+ nginx-2 启动代理程序
+
+```shell
+[root@nginx-server ~]# vim /etc/nginx/conf.d/default.conf
+server {
+ listen 80;
+ server_name localhost;
+
+ location / {
+ proxy_pass http://10.0.105.199:80;
+ proxy_redirect default;
+ proxy_set_header Host $http_host;
+ proxy_set_header X-Real-IP $remote_addr;
+ proxy_set_header REMOTE-HOST $remote_addr;
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+
+ proxy_connect_timeout 30;
+ proxy_send_timeout 60;
+ proxy_read_timeout 60;
+
+ proxy_buffering on;
+ proxy_buffer_size 32k;
+ proxy_buffers 4 128k;
+ proxy_busy_buffers_size 256k;
+ proxy_max_temp_file_size 256k;
+ }
+}
+```
+
+nginx proxy 具体配置详解:
+
+```shell
+proxy_pass :真实服务器的地址,可以是ip也可以是域名和url地址
+proxy_redirect :如果真实服务器使用的是的真实 IP:非默认端口。则改成IP:默认端口。
+proxy_set_header:重新定义或者添加发往后端服务器的请求头
+proxy_set_header X-Real-IP :启用客户端真实地址(否则日志中显示的是代理在访问网站)
+proxy_set_header X-Forwarded-For:记录代理地址
+
+proxy_connect_timeout::后端服务器连接的超时时间发起三次握手等候响应超时时间
+proxy_send_timeout:后端服务器数据回传时间就是在规定时间之内后端服务器必须传完所有的数据
+proxy_read_timeout :nginx接收upstream(上游/真实) server数据超时, 默认60s, 如果连续的60s内没有收到1个字节, 连接关闭。像长连接
+
+proxy_buffering on;开启缓存
+proxy_buffer_size:proxy_buffer_size只是响应头的缓冲区
+proxy_buffers 4 128k; 内容缓冲区域大小
+proxy_busy_buffers_size 256k; 从proxy_buffers划出一部分缓冲区来专门向客户端传送数据的地方
+proxy_max_temp_file_size 256k;超大的响应头存储成文件
+```
+
+## 二:Nginx负载均衡
+
+ 随着网站、应用访问量的增加,一台服务器已经不能满足应用的需求,而需要多台服务器集群,这时就会用到负载均衡
+
+#### 1.负载均衡配置
+
+```shell
+upstream testapp {
+ server 10.0.105.199:8081;
+ server 10.0.105.202:8081;
+ }
+ server {
+ ....
+ location / {
+ proxy_pass http://testapp; #请求转向 testapp 定义的服务器列表
+ }
+
+upstream mysvr {
+ server http://10.0.105.199:8081;
+ server http://10.0.105.202:8081;
+ }
+ server {
+ ....
+ location / {
+ proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
+ }
+```
+
+#### 2.负载均衡算法
+
+ 轮询(默认):每个请求按时间顺序逐一分配到不同的后端服务器
+
+ ip_hash:每个请求按访问IP的hash结果分配,同一个IP客户端固定访问一个后端服务器
+
+ url_hash:按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器
+
+ fair:这是比上面两个更加智能的负载均衡算法。按后端服务器的响应时间来分配请求,响应时间短的优先分配
+
+注意:
+
+ Nginx本身是不支持 fair的,如果需要使用这种调度算法,必须下载Nginx的 upstream_fair模块
+
+#### 3.配置实例
+
+```shell
+1)、热备:如果你有2台服务器,当一台服务器发生事故时,才启用第二台服务器给提供服务。服务器处理请求的顺序:AAAAAA突然A挂啦,BBBBBBBBBBBBBB.....
+upstream myweb {
+ server 172.17.14.2:8080;
+ server 172.17.14.3:8080 backup; #热备
+ }
+2)、轮询:nginx默认就是轮询其权重都默认为1,服务器处理请求的顺序:ABABABABAB....
+upstream myweb {
+ server 172.17.14.2:8080;
+ server 172.17.14.3:8080;
+ }
+3)、加权轮询:跟据配置的权重的大小而分发给不同服务器不同数量的请求。如果不设置,则默认为1。下面服务器的请求顺序为:BBABBABBABBABB....
+upstream myweb {
+ server 172.17.14.2:8080 weight=1;
+ server 172.17.14.3:8080 weight=2;
+}
+4、ip_hash:nginx会让相同的客户端ip请求相同的服务器。
+upstream myweb {
+ server 172.17.14.2:8080;
+ server 172.17.14.3:8080;
+ ip_hash;
+ }
+
+5)、nginx负载均衡配置状态参数
+• down,表示当前的server暂时不参与负载均衡。
+
+• backup,预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻。
+
+• max_fails,允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误。
+
+• fail_timeout,在经历了max_fails次失败后,暂停服务的时间单位秒。max_fails可以和fail_timeout一起使用。
+ upstream myweb {
+ server 172.17.14.2:8080 weight=2 max_fails=2 fail_timeout=2;
+ server 172.17.14.3:8080 weight=1 max_fails=2 fail_timeout=1;
+ }
+```
+
+#### 4.Nginx七层负载案例
+
+
+
+## 三:Nginx会话保持
+
+#### 1.实现方式
+
+ip_hash:
+
+ ip_hash使用源地址哈希算法,将同一客户端的请求总是发往同一个后端服务器,除非该服务器不可用
+
+语法:
+
+```shell
+upstream backend {
+ ip_hash;
+ server backend1.example.com;
+ server backend2.example.com;
+ server backend3.example.com;
+}
+```
+
+ip_hash简单易用,但有如下问题:
+
+ 当后端服务器宕机后,session会丢失
+
+ 来自同一局域网的客户端会被转发到同一个后端服务器,可能导致负载失衡
+
+sticky_cookie_insert:
+
+ 使用sticky_cookie_insert启用会话亲缘关系,这会导致来自同一客户端的请求被传递到一组服务器的同一台服务器
+
+ 与ip_hash不同之处在于,它不是基于IP来判断客户端的,而是基于cookie来判断
+
+ 因此可以避免上述ip_hash中来自同一局域网的客户端和前段代理导致负载失衡的情况
+
+#### 2.部署
+
+环境:
+
+```shell
+一台服务器做负载均衡并且使用第三方模块sticky来做会话保持
+ 192.168.182.130(sticky)
+ 两台服务器做web-server
+ 192.168.182.131(web-server-1)
+ 安装nginx并运行 自己创建一个测试界面,使用curl能否访问到内容,保证web-server-1可用。
+ 192.168.182.132(web-server-2)
+ 安装nginx并运行 自己创建一个测试界面,使用curl能否访问到内容,保证web-server-2可用。
+```
+
+下载插件:
+
+```shell
+[root@xingdian ~]# wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/08a395c66e42.zip
+```
+
+下载源码包:
+
+```shell
+[root@xingdian ~]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
+```
+
+解压:
+
+```shell
+[root@xingdian ~]# unzip 08a395c66e42.zip
+[root@xingdian ~]# tar xf nginx-1.22.0.tar.gz
+[root@xingdian ~]# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ nginx-sticky
+```
+
+配置:
+
+```shell
+[root@xingdian nginx-1.22.0]# cd nginx-1.22.0
+[root@xingdian nginx-1.22.0]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --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 --lock-path=/var/run/nginx.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 --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
+```
+
+编译安装:
+
+```shell
+[root@xingdian nginx-1.22.0]# make && make install
+```
+
+配置负载均衡和会话保持:
+
+```shell
+vim /etc/nginx/nginx.conf
+以下只是要添加的内容:
+ upstream xingdian {
+ server 192.168.182.131;
+ server 192.168.182.132;
+ sticky ;
+ }
+
+vim /etc/nginx/conf.d/default.conf
+以下为配置文件修改的内容:
+ location / {
+ proxy_pass http://xingdian;
+ }
+```
+
+重启服务:
+
+```shell
+[root@xingdian nginx-1.22.0]# systemctl restart nginx
+```
+
+## 四:动静分离
+
+#### 1.介绍
+
+ 一般来说,都需要将动态资源和静态资源分开,将静态资源部署在Nginx上,当一个请求来的时候,如果是静态资源的请求,就直接到nginx配置的静态资源目录下面获取资源,如果是动态资源的请求,nginx利用反向代理的原理,把请求转发给后台应用去处理,从而实现动静分离
+
+处理静态的服务:nginx apache
+
+处理动态的服务:web容器/web中间件 php(php)tomcat(java) uwsgi(python)
+
+#### 2.实验部署
+
+环境:
+
+| 服务器 | 要求 |
+| :-------------: | :----------------: |
+| 192.168.182.130 | 动静分离、upstream |
+| 192.168.182.131 | static |
+| 192.168.182.132 | php-server |
+
+配置代理:
+
+```shell
+vim /etc/nginx/nginx.conf (只是写了修改的内容)
+upstream static {
+ server 10.0.105.196:80 weight=1 max_fails=1 fail_timeout=60s;
+ }
+upstream phpserver {
+ server 10.0.105.200:80 weight=1 max_fails=1 fail_timeout=60s;
+ }
+
+vim /etc/nginx/conf.d/default.conf (只是写了修改的内容)
+#动态资源加载
+ location ~ \.(php|jsp)$ {
+ proxy_pass http://phpserver;
+ proxy_set_header Host $host:$server_port;
+ proxy_set_header X-Real-IP $remote_addr;
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ }
+#静态资源加载
+ location ~ .*\.(html|gif|jpg|png|bmp|swf|css|js)$ {
+ proxy_pass http://static;
+ proxy_set_header Host $host:$server_port;
+ proxy_set_header X-Real-IP $remote_addr;
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ }
+```
+
+静态配置:
+
+```shell
+1.安装nginx
+2.配置nginx的配置文件,创建创展发布目录(略),写一个测试的静态网站(index.html)(略)
+vim /etc/nginx/conf.d/default.conf (你要修改的配置)
+
+ location ~ \.(html|jpg|png|js|css|gif|bmp|jpeg)$ {
+ root /qfedu;
+ index index.html;
+ }
+3.启动服务(关闭selinux和firewalld)
+```
+
+动态配置:
+
+```shell
+1.安装nginx(略),安装php
+yum -y install php php-fpm php-mysql
+2.修改配置文件
+vim /etc/nginx/conf.d/default.conf
+
+location / {
+ root /qfedu;
+ index index.php index.html;
+}
+
+location ~ \.php$ {
+ root /qfedu;
+ fastcgi_pass 127.0.0.1:9000;
+ fastcgi_index index.php;
+ fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
+ include fastcgi_params;
+
+}
+注意:删除注释,修改内容。
+3.创建网站目录(略),创建一个网站
+index.php (文件名)
+
+
+4.关闭slinux关闭firewalld,重启服务
+
+5.分别对静态和动态服务器单独测试,最后使用130的机器进行测试。
+```
+
+#### 3.如何安装高版本php
+
+安装仓库:
+
+```shell
+[root@xingdiancloud ~]# yum install http://rpms.remirepo.net/enterprise/remirelease-7.rpm
+```
+
+安装php:
+
+```shell
+[root@xingdiancloud ~]# yum install yum-utils
+[root@xingdiancloud ~]# yum-config-manager --enable remi-php72
+[root@xingdiancloud ~]# yum install httpd php php-gd php-json php-mysql phpcurl php-mbstring php-intl php-mcrypt php-imagick php-xml php-zip
+```
+
+## 五:防盗链
+
+#### 1.介绍
+
+ 两个网站 A 和 B, A网站引用了B网站上的图片,这种行为就叫做盗链。 防盗链,就是要防止A引用B的图片
+
+ nginx防止网站资源被盗用模块:ngx_http_referer_module
+
+ HTTP Referer是Header的一部分,当浏览器向Web服务器发送请求的时候,一般会带上Referer,告诉服务器我是从哪个页面链接过来的,服务器借此可以获得一些信息用于处理,例如防止未经允许的网站盗链图片、文件等。因此HTTP Referer头信息是可以通过程序来伪装生成的,所以通过Referer信息防盗链并非100%可靠,但是,它能够限制大部分的盗链情况
+
+#### 2.环境
+
+ 用来做图片服务器(防盗链的部署)192.168.182.131 (A)
+
+ 盗链(测试防盗链是否成功) 192.168.182.132(B)
+
+#### 3.部署A
+
+ 创建一个网站发布目录,上传一张图片,写一个index.html的文件
+
+```shell
+[root@a web]# vim index.html
+
+
+
+
+
+#### 2.异步非阻塞
+
+```shell
+$ pstree |grep nginx
+ |-+= 81666 root nginx: master process nginx
+ | |--- 82500 nobody nginx: worker process
+ | \--- 82501 nobody nginx: worker process
+```
+
+ 每进来一个request,会有一个worker进程去处理。但不是全程的处理,处理到什么程度呢?处理到可能发生阻塞的地方,比如向上游(后端)服务器转发request,并等待请求返回。那么,这个处理的worker不会这么一直等着,他会在发送完请求后,注册一个事件:“如果upstream返回了,告诉我一声,我再接着干”。于是他就休息去了。这就是异步。此时,如果再有request 进来,他就可以很快再按这种方式处理。这就是非阻塞和IO多路复用。而一旦上游服务器返回了,就会触发这个事件,worker才会来接手,这个request才会接着往下走。这就是异步回调
+
+案例:
+
+ 客户(发送方)向收款员(接收方)付款(发送请求)后在等待收款员找零的过程中,还可以做其他事情,比如打电话、聊天等;而收款员在等待收款机处理交易(IO操作)的过程中还可以帮助客户将商品打包,当收款机产生结果后,收款员给客户结账(响应请求)。在四种方式中,这种方式是发送方和接收方通信效率最高的一种
+
+##### 同步异步
+
+ 同步与异步的重点在消息通知的方式上,也就是调用结果通知的方式
+
+ 同步:当一个同步调用发出去后,调用者要一直等待调用结果的通知后,才能进行后续的执行
+
+ 异步:当一个异步调用发出去后,调用者不能立即得到调用结果的返回
+
+ 异步调用,要想获得结果,一般有两种方式:
+
+ 主动轮询异步调用的结果
+
+ 被调用方通过callback来通知调用方调用结果
+
+案例:
+
+ 同步取快递:小明收到快递将送达的短信,在楼下一直等到快递送达
+
+ 异步取快递:小明收到快递将送达的短信,快递到楼下后,小明再下楼去取
+
+ 异步取快递,小明知道快递到达楼下有两种方式:
+
+ 不停的电话问快递小哥到了没有,即主动轮询
+
+ 快递小哥到楼下后,打电话通知小明,然后小明下楼取快递,即回调通知
+
+##### 阻塞非阻塞
+
+ 阻塞与非阻塞在于进/线程等待消息时候的行为,也就是在等待消息的时候,当前进/线程是挂起还是非挂起状态
+
+ 阻塞:阻塞调用在发出去后,在消息返回之前,当前进/线程会被挂起,直到有消息返回,当前进/线程才会被激活
+
+ 非阻塞:非阻塞调用在发出去后,不会阻塞当前进/线程,而会立即返回
+
+案例:
+
+ 阻塞取快递:小明收到快递即将送达的信息后,什么事都不做,一直专门等快递
+
+ 非阻塞取快递:小明收到快递即将送达的信息后,等快递的时候,还一边敲代码、一边刷微信
+
+##### 总结
+
+ 异步非阻塞:小明收到信息后,边刷着微博,边等快递员通知他取快递
+
+##### I/O多路复用
+
+ Input/Output====I/O
+
+ 三种模型:select、poll、epoll
+
+epoll():
+
+ epoll能更高效的检查大量fd,UNIX中提供了类似功能的kqueue调用
+
+ epoll可以理解为event poll,不同于忙轮询和无差别轮询,当连接有I/O流事件产生的时候,epoll就会去告诉进程哪个连接有I/O流事件产生,然后进程就去处理这个事件。此时我们对这些流的操作都是有意义的
+
+案例:
+
+ 小明家楼下有一个收发室,每次有快递到了,就先代收并做了标记;然后通知小明去取送给小明的快递
+
+#### 3.Nginx内部架构
+
+ Nginx服务器,以其处理网络请求的高并发、高性能及高效率,获得了行业界的广泛认可,近年已稳居web服务器部署排名第二的位置,并被广泛用于反向代理和负载均衡
+
+技术点:
+
+ nginx启动时,会生成两种类型的进程,一个是主进程(Master),一个或多个工作进程(Worker)。主进程并不处理网络请求,主要负责调度工作进程,也就是:加载配置、启动工作进程及非停升级。所以,nginx启动以后,查看操作系统的进程列表,我们就能看到至少有两个nginx进程
+
+ 服务器实际处理网络请求及响应的是工作进程(worker),在类unix系统上,nginx可以配置多个worker,而每个worker进程都可以同时处理数以千计的网络请求
+
+ 模块化设计。nginx的worker,包括核心和功能性模块,核心模块负责维持一个运行循环(run-loop),执行网络请求处理的不同阶段的模块功能,如网络读写、存储读写、内容传输、外出过滤,以及将请求发往上游服务器等。而其代码的模块化设计,也使得我们可以根据需要对功能模块进行适当的选择和修改,编译成具有特定功能的服务器
+
+ 事件驱动、异步及非阻塞,可以说是nginx得以获得高并发、高性能的关键因素,同时也得益于对Linux、Solaris及类BSD等操作系统内核中事件通知及I/O性能增强功能的采用,如kqueue、epoll及event ports
+
+ 代理(proxy)设计,可以说是nginx深入骨髓的设计,无论是对于HTTP,还是对于FastCGI、memcache、Redis等的网络请求或响应,本质上都采用了代理机制。所以,nginx天生就是高性能的代理服务器
+
+## 二:Nginx安装部署
+
+#### 1.Yum安装
+
+方式一:
+
+ 使用默认的yum仓库,epel仓库里是有关于nginx的rpm包
+
+```shell
+[root@master ~]# ls /etc/yum.repos.d/
+CentOS-Base.repo epel.repo
+[root@master ~]# yum -y install nginx
+```
+
+方式二:
+
+ 使用官方的yum仓库
+
+获取nginx官方仓库:
+
+
+
+
+
+```shell
+[root@master ~]# vim /etc/yum.repos.d/nginx.repo
+[nginx-stable]
+name=nginx stable repo
+baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
+gpgcheck=1
+enabled=1
+gpgkey=https://nginx.org/keys/nginx_signing.key
+module_hotfixes=true
+
+[nginx-mainline]
+name=nginx mainline repo
+baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
+gpgcheck=1
+enabled=0
+gpgkey=https://nginx.org/keys/nginx_signing.key
+module_hotfixes=true
+
+[root@master ~]# yum -y install nginx
+```
+
+获取安装版本信息:
+
+```shell
+[root@xingdian ~]# nginx -V
+nginx version: nginx/1.22.0
+built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
+built with OpenSSL 1.0.2k-fips 26 Jan 2017
+TLS SNI support enabled
+configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --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 --lock-path=/var/run/nginx.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 --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
+```
+
+查看nginx版本:
+
+```shell
+[root@xingdian ~]# nginx -v
+nginx version: nginx/1.22.0
+```
+
+关闭防火墙和selinux:
+
+```shell
+[root@nginx-server ~]# sed -i '/^SELINUX=/c SELINUX=Disabled' /etc/selinux/config
+[root@nginx-server ~]# setenforce 0
+[root@nginx-server ~]# systemctl stop firewalld
+[root@nginx-server ~]# systemctl disable firewalld
+```
+
+启动并设置开机启动:
+
+```shell
+[root@nginx-server ~]# systemctl start nginx
+[root@nginx-server ~]# systemctl enable nginx
+```
+
+浏览器测试访问:
+
+
+
+#### 2.编译安装
+
+准备编译安装环境:
+
+```shell
+[root@xingdian ~]# yum -y install gcc gcc-c++ make ncurses ncurses-devel wget openssl* pcre*
+```
+
+获取编译安装包:
+
+
+
+```shell
+[root@xingdian ~]# wget http://nginx.org/download/nginx-1.22.0.tar.gz
+[root@xingdian ~]# tar xf nginx-1.22.0.tar.gz
+[root@xingdian ~]# cd nginx-1.22.0
+```
+
+配置:
+
+```shell
+[root@xingdian nginx-1.22.0]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream
+```
+
+编译和安装:
+
+```shell
+[root@xingdian nginx-1.22.0]# make && make install
+```
+
+创建管理用户:
+
+```shell
+[root@xingdian sbin]# useradd -s /sbin/nologin -M nginx
+```
+
+启动报错及解决方案:
+
+```shell
+[root@xingdian nginx-1.22.0]# cd /usr/local/nginx/sbin/
+[root@xingdian sbin]# ./nginx
+nginx: [emerg] mkdir() "/tmp/nginx/client_body" failed (2: No such file or directory)
+[root@xingdian sbin]# mkdir -pv /tmp/nginx/client_body
+mkdir: created directory '/tmp/nginx'
+mkdir: created directory '/tmp/nginx/client_body'
+[root@xingdian sbin]# ./nginx
+```
+
+浏览器访问测试:
+
+
+
+nginx编译参数:
+
+ 官方地址:http://nginx.org/en/docs/configure.html
+
+```shell
+--prefix=/usr/local/nginx //指向安装目录
+--conf-path=/etc/nginx/nginx.conf //指定配置文件
+--http-log-path=/var/log/nginx/access.log //指定访问日志
+--error-log-path=/var/log/nginx/error.log //指定错误日志
+--lock-path=/var/lock/nginx.lock //指定lock文件
+--pid-path=/run/nginx.pid //指定pid文件
+
+--http-client-body-temp-path=/var/lib/nginx/body //设定http客户端请求临时文件路径
+--http-fastcgi-temp-path=/var/lib/nginx/fastcgi //设定http fastcgi临时文件路径
+--http-proxy-temp-path=/var/lib/nginx/proxy //设定http代理临时文件路径
+--http-scgi-temp-path=/var/lib/nginx/scgi //设定http scgi临时文件路径
+--http-uwsgi-temp-path=/var/lib/nginx/uwsgi //设定http uwsgi临时文件路径
+
+--with-debug //启用debug日志
+--with-pcre-jit //编译PCRE包含“just-in-time compilation”
+--with-ipv6 //启用ipv6支持
+--with-http_ssl_module //启用ssl支持
+--with-http_stub_status_module //获取nginx自上次启动以来的状态
+--with-http_realip_module //允许从请求标头更改客户端的IP地址值,默认为关
+--with-http_auth_request_module //实现基于一个子请求的结果的客户端授权。如果该子请求返回的2xx响应代码,所述接入是允许的。如果它返回401或403中,访问被拒绝与相应的错误代码。由子请求返回的任何其他响应代码被认为是一个错误。
+--with-http_addition_module //作为一个输出过滤器,支持不完全缓冲,分部分响应请求
+--with-http_dav_module //增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法 默认关闭,需编译开启
+--with-http_geoip_module //使用预编译的MaxMind数据库解析客户端IP地址,得到变量值
+--with-http_gunzip_module //它为不支持“gzip”编码方法的客户端解压具有“Content-Encoding: gzip”头的响应。
+--with-http_gzip_static_module //在线实时压缩输出数据流
+--with-http_image_filter_module //传输JPEG/GIF/PNG 图片的一个过滤器)(默认为不启用。gd库要用到)
+--with-http_spdy_module //SPDY可以缩短网页的加载时间
+--with-http_sub_module //允许用一些其他文本替换nginx响应中的一些文本
+--with-http_xslt_module //过滤转换XML请求
+--with-mail //启用POP3/IMAP4/SMTP代理模块支持
+--with-mail_ssl_module //启用ngx_mail_ssl_module支持启用外部模块支持
+```
+
+#### 3.Nginx配置文件详解
+
+ nginx.conf的组成:nginx.conf一共由三部分组成,分别为:全局块、events块、http块
+
+ 在http块中又包含http全局块、多个server块
+
+ 每个server块中又包含server全局块以及多个location块
+
+```shell
+# 全局参数设置
+worker_processes 4; #设置nginx启动进程的数量,一般设置成与逻辑cpu数量相同
+注意:
+1.物理cpu数:主板上实际插入的cpu数量,可以数不重复的 physical id 有几个(physical id)
+2.cpu核数:单块CPU上面能处理数据的芯片组的数量,如双核、四核等 (cpu cores)
+3.逻辑cpu数:一般情况下,逻辑cpu=物理CPU个数×每颗核数
+
+error_log logs/error.log; #指定错误日志
+worker_rlimit_nofile 102400; #设置一个nginx进程能打开的最大文件数
+pid /var/run/nginx.pid;
+events {
+ worker_connections 1024; #设置一个进程的最大并发连接数
+}
+# http 服务相关设置
+http {
+ include mime.types;
+ default_type application/octet-stream;
+ log_format main 'remote_addr - remote_user [time_local] "request" '
+ 'status body_bytes_sent "$http_referer" '
+ '"http_user_agent" "http_x_forwarded_for"';
+ access_log /var/log/nginx/access.log main; #设置访问日志的位置和格式
+ sendfile on; #是否调用sendfile函数输出文件,一般设置为on,若nginx是用来进行磁盘IO负载应用时,可以设置为off,降低系统负载
+ gzip on; #是否开启gzip压缩,将注释去掉开启
+ keepalive_timeout 65; #设置长连接的超时时间
+# 虚拟服务器的相关设置
+ server {
+ listen 80; #设置监听的端口
+ server_name localhost; #设置绑定的主机名、域名或ip地址
+ charset koi8-r; # 设置编码字符
+ location / {
+ root /var/www/nginx; #设置服务器默认网站的根目录位置,需要手动创建
+ index index.html index.htm; #设置默认打开的文档
+ }
+ error_page 500 502 503 504 /50x.html; #设置错误信息返回页面
+ location = /50x.html {
+ root html; #这里的绝对位置是/usr/local/nginx/html
+ }
+ include /etc/nginx/conf.d/*.conf; 加载子配置文件
+
+ }
+ }
+
+检测nginx配置文件是否正确
+[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
+[root@localhost ~]# mkdir -p /tmp/nginx
+
+启动nginx服务
+[root@localhost ~]# /usr/local/nginx/sbin/nginx
+
+通过 nginx 命令控制 nginx 服务
+
+nginx -c /path/nginx.conf # 以特定目录下的配置文件启动nginx:
+nginx -s reload # 修改配置后重新加载生效
+nginx -s reopen # 重新打开日志文件
+nginx -s stop # 快速停止nginx
+nginx -t # 测试当前配置文件是否正确
+nginx -t -c /usr/lcoal/nginx/conf/nginx.conf # 测试特定的nginx配置文件是否正确
+```
+
+通过nginx命令控制nginx服务:
+
+```shell
+nginx -c /path/nginx.conf # 以特定目录下的配置文件启动nginx:
+nginx -s reload # 修改配置后重新加载生效
+nginx -s reopen # 重新打开日志文件
+nginx -s stop # 快速停止nginx
+nginx -t # 测试当前配置文件是否正确
+nginx -t -c /usr/lcoal/nginx/conf/nginx.conf # 测试特定的nginx配置文件是否正确
+```
+
+注意:
+
+nginx -s reload 命令加载修改后的配置文件,命令下达后发生如下事件:
+
+ Nginx的master进程检查配置文件的正确性,若是错误返回错误信息,nginx继续采用原配置文件进行工作(因为worker未受到影响)
+
+ Nginx启动新的worker进程,采用新的配置文件
+
+ Nginx将新的请求分配新的worker进程
+
+ Nginx等待以前的worker进程的全部请求已经都返回后,关闭相关worker进程
+
+ 重复上面过程,直到全部旧的worker进程都被关闭掉
+
+## 三:Nginx虚拟主机
+
+#### 1.什么是虚拟主机
+
+ 虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供www服务,这样就可以实现一台主机对外提供多个web服务,每个虚拟主机之间是独立的,互不影响
+
+
+
+#### 2.虚拟主机类型
+
+ 基于域名的虚拟主机
+
+ 基于ip的虚拟主机
+
+ 基于端口的虚拟主机
+
+#### 3.基于域名的虚拟主机
+
+ 创建虚拟主机配置文件:
+
+```shell
+[root@localhost ~]# cat /etc/nginx/conf.d/qfedu.conf (可以写到两个配置文件当中,也可以写到一个)
+server {
+ listen 80;
+ server_name www.xingdiancloud.com;
+ location / {
+ root /web1;
+ index index.html index.htm;
+ }
+ }
+
+server {
+ listen 80;
+ server_name www.1000phone.com;
+ location / {
+ root /web2;
+ index index.html index.htm;
+ }
+ }
+```
+
+ 创建网站发布目录,并且创建对应的网站:
+
+```shell
+[root@localhost ~]# mkdir /web1
+[root@localhost ~]# mkdir /web2
+[root@localhost ~]#echo "web1" > /web1/index.html
+[root@localhost ~]#echo "web2" > /web2/index.html
+```
+
+ 重新加载配置文件:
+
+```shell
+[root@localhost ~]# nginx -s reload
+```
+
+ 客户端配置路由映射(本地域名解析):
+
+```shell
+[root@localhost ~]# vim /etc/hosts
+10.0.105.199 www.xingdiancloud.com
+10.0.105.199 www.1000phone.com
+```
+
+ 测试访问:
+
+```shell
+[root@xingdian conf.d]# curl www.xingdiancloud.com
+web1
+[root@xingdian conf.d]# curl www.1000phone.com
+web2
+```
+
+#### 4.基于域名的虚拟主机
+
+ 添加IP地址:
+
+```shell
+[root@localhost ~]# ip a
+1: lo:
+
+
+
+
+
+#### 7.echo模块
+
+获取nginx的安装版本:
+
+```shell
+[root@192 ~]# nginx -v
+```
+
+上传或者下载一个相同版本的nginx包,并解压(略)
+
+下载echo模块的安装包,并解压到指定位置(略):
+
+```shell
+[root@192 ~]# wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
+```
+
+安装编译所需软件:
+
+```shell
+[root@192 local]# yum install -y pcre* openssl* gcc gcc-c++ make
+```
+
+获取源nginx的配置:
+
+```shell
+[root@192 ~]# nginx -V
+```
+
+添加上原来已经有的参数和新添加的模块:
+
+```shell
+[root@192 nginx-1.16.0]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --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 --lock-path=/var/run/nginx.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 --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/usr/local/echo-nginx-module-0.61
+```
+
+重新编译:
+
+```shell
+[root@192 nginx-1.16.0]# make
+```
+
+将原来的nignx备份:
+
+```shell
+[root@192 nginx-1.16.0]# mv /usr/sbin/nginx /usr/sbin/nginx_bak
+[root@192 nginx-1.16.0]# cp objs/nginx /usr/sbin/
+```
+
+重新启动:
+
+```
+[root@192 nginx-1.16.0]# systemctl restart nginx
+```
+
+使用echo:
+
+```shell
+配置 $foo=hello
+[root@192 ~]# cd /etc/nginx/conf.d/
+[root@192 conf.d]# vim echo.conf
+server {
+ listen 80;
+ server_name localhost;
+ location /test {
+ set $foo hello;
+ echo "foo: $foo";
+ }
+}
+
+[root@192 conf.d]# nginx -s reload
+[root@192 conf.d]# curl localhost/test
+foo: hello
+```
+
+#### 8.alias和root
+
+ alias 是一个目录别名的定义
+
+ root 则是最上层目录的定义
+
+ 还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的,而root则可有可无
+
+```shell
+location /img/ {
+ alias /var/www/image/;
+}
+```
+
+ 若按照上述配置的话,则访问/img/目录里面的文件时,ningx会自动去/var/www/image/目录找文件
+
+```shell
+location /img/ {
+ root /var/www/image;
+}
+```
+
+ 若按照这种配置的话,则访问/img/目录下的文件时,nginx会去/var/www/image/img/目录下找文件
+