Nginx详解反向代理、负载均衡、lnmp架构上线动态网站

1.Nginx

  Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”,是一个高性能的HTTP和反向代理服务器,同时也是一个iMaP/POP3/SMTP 代理服务器。Nginx是由俄罗斯人 igor Sysoev为俄罗斯访问量第二的 Rambler.ru站点开发的,它已经在该站点运行超过两年半了。igor Sysoev在建立的项目时,使用基于BSD许可。

2. Nginxapache的区别(至少记三条)

Nginx:

  1> 轻量级,采用 C 进行编写,同样的 web 服务,会占用更少的内存及资源;

  2> 抗并发,nginx 以 epoll and kqueue 作为开发模型,处理请求是异步非阻塞的,负载能力比 apache 高很多,而 apache 则是阻塞型的。在高并发下 nginx 能保持低资源低消耗高性能 ,而 apache 在 PHP 处理慢或者前端压力很大的情况下,很容易出现进程数飙升,从而拒绝服务的现象;

  3> nginx 处理静态文件好,静态处理性能比 apache 高三倍以上;

  4> nginx 的设计高度模块化,编写模块相对简单;

  5> nginx 配置简洁,正则配置让很多事情变得简单,而且改完配置能使用 -t 测试配置有没有问题,apache 配置复杂 ,重启的时候发现配置出错了,会崩溃;

  6> nginx 作为负载均衡服务器,支持 7 层负载均衡;

  7> nginx 本身就是一个反向代理服务器,而且可以作为非常优秀的邮件代理服务器;

  8> 启动特别容易, 并且几乎可以做到 7*24 不间断运行,即使运行数个月也不需要重新启动,还能够不间断服务的情况下进行软件版本的升级;

  9> 社区活跃,各种高性能模块出品迅速。

 

apache:

  1> apache 的 rewrite 比 nginx 强大,在 rewrite 频繁的情况下,用 apache;

  2> apache 发展到现在,模块超多,基本想到的都可以找到;

  3> apache 更为成熟,少 bug ,nginx 的 bug 相对较多;

  4> apache 超稳定;

  5> apache 对 PHP 支持比较简单,nginx 需要配合其他后端用;

  6> apache 在处理动态请求有优势,nginx 在这方面是鸡肋,一般动态请求要 apache 去做,nginx 适合静态和反向。

 

总的来说

  两者最核心的区别在于 apache 是同步多进程模型,一个连接对应一个进程,而 nginx 是异步的,多个连接(万级别)可以对应一个进程。一般来说,需要性能的 web 服务,用 nginx 。如果不需要性能只求稳定,更考虑 apache ,后者的各种功能模块实现得比前者,例如 ssl 的模块就比前者好,可配置项多。epoll(freebsd 上是 kqueue ) 网络 IO 模型是 nginx 处理性能高的根本理由,但并不是所有的情况下都是 epoll 大获全胜的,如果本身提供静态服务的就只有寥寥几个文件,apache 的 select 模型或许比 epoll 更高性能。当然,这只是根据网络 IO 模型的原理作的一个假设,真正的应用还是需要实测了再说的。

  更为通用的方案是,前端 nginx 抗并发,后端 apache 集群,配合起来会更好。

 

3. 集群

  简单来说,集群就是指一组相互独立的计算机,利用高速通信网络组成的一个较大的计算机服务系统,每个集群节点都是运行各自服务的独立服务器。这些服务器之间可以彼此通信,协同向用户提供应用程序、系统资源和数据,并以单一系统的模式加以管理。当用户客户机请求集群系统时,集群给用户的感觉就是一个单一的服务器,而实际上用户请求的是一组集群服务器。

  集群主要包括几大特点:高性能、价格有效性、可伸缩性、高可用性、透明性、可管理性和可编程性。

 

3.1 负载均衡集群

  常见的负载均衡的架构包括有负载均衡集群、高可用性集群、高性能计算集群等等。这里着重介绍负载均衡集群,其他的集群方式不做介绍。

  负载均衡集群为企业提供了更为实用、性价比更高的系统架构解决方案。负载集群可以把很多客户集中的访问请求负载压力尽可能平均分摊到计算机集群中处理。客户访问请求负载均衡通常包含应用程序处理负载均衡和网络流量负载。这样的系统非常适合使用同一组应用程序为大量用户提供服务的模式,每个节点都可以承当一定的访问请求负载压力,并且可以实现访问请求在各节点之间动态分配,以实现负载均衡。

  负载均衡集群运行时,一般是通过一个或多个前端负载均衡器将客户访问请求分发到后端的一组服务器上,从而达到整个系统的高性能和高可用性。一般高可用性集群和负载均衡集群使用类似的技术,或同时具有高可用与负载均衡的特点。负载均衡的作用为:分担用户访问及数据流量、保持业务的连续性、应用于Web业务及数据库从库等服务器的业务。

 

3.2  Nginx负载均衡集群介绍

  互联网企业中常见的开源集群软件有:Nginx、lVS、Haproxy、Keepalived等,硬件有F5、Netscaler等。

  严格地说,Nginx仅仅是作为Nginx Proxy反向代理使用的,因为反向代理功能表现的效果是负载均衡集群的效果,所以也叫做Nginx负载均衡。反向代理和负载均衡的区别在于负载均衡通常都是对请求的数据包的转发(也有可能会改写数据包)、传递,其中DR模式明显的特征就是从负载均衡下面的节点服务器来看,接收到的请求还是来自负载均衡器的客户端的真实用户。而反向代理,反向代理接收访问用户的请求后,会代理用户重新发起请求代理下的节点服务器,最后把数据返回给客户端用户。在节点服务器来看,访问节点服务器的客户端用户是反向代理服务器,而不是真实的网站访问用户。Nginx负载均衡的模块主要有两个,ngx_http_proxy_module,ngx_http_upstream_module。编译的时候需要把这两个模块编译进去。

 

4. Nginx安装

[root@localhost ~]# yum install nginx -y 

Nginx占用的也是80端口,启动Nginx时首先将httpd关闭

[root@localhost ~]# ss -tnl
State      Recv-Q Send-Q local address:Port               Peer address:Port   
liSTEN     0      128              *:111                          *:*         
liSTEN     0      128              *:22                           *:*         
liSTEN     0      100      127.0.0.1:25                           *:*         
liSTEN     0      128             :::111                         :::*         
liSTEN     0      128             :::22                          :::*         
liSTEN     0      100            ::1:25                          :::*
[root@localhost ~]# systemctl restart nginx                  #启动Nginx
  LISTEN   0    128            *:80                    *:*   
[root@localhost ~]# nginx -V                                #查看Nginx版本
nginx version: nginx/1.12.2

5. Nginx配置文件

 

[root@localhost ~]# vim /etc/nginx/nginx.conf
user nginx;                                      #Nginx用户
[root@localhost ~]# ps aux | grep nginx
nginx  2361  0.0  0.3 121200  3124 ?  S 23:04   0:00 nginx: worker process
worker_processes auto;                           #工作进程,auto同步于虚拟机内核数
更改进程数Nginx进程会改变
worker_processes 5;
[root@localhost ~]# ps aux | grep nginx
nginx
nginx      2410  0.0  0.3 121200  3128 ?        S    23:17   0:00 nginx: worker process
nginx      2411  0.0  0.3 121200  3128 ?        S    23:17   0:00 nginx: worker process
nginx      2412  0.0  0.3 121200  3128 ?        S    23:17   0:00 nginx: worker process
nginx      2413  0.0  0.3 121200  3128 ?        S    23:17   0:00 nginx: worker process
nginx      2414  0.0  0.3 121200  3128 ?        S    23:17   0:00 nginx: worker process
events {
    worker_connections 1024;               #一个进程可以生成1024个线程
}
error_log /var/log/nginx/error.log;        #错误日志
pid /run/nginx.pid;                        #pid存放路径
include /usr/share/nginx/modules/*.conf;   #加载文件
http {                                     #web全局信息
    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;               #发送文件相关
    tcp_nopush          on;               #
    tcp_nodelay         on;               #
    keepalive_timeout   65;               # 长链接超时时间,65秒后自动断开
    types_hash_max_size 2048;             #hash加密
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

server {                                        #虚拟主机
        listen       80 default_server;         #监听端口
        listen       [::]:80 default_server;    #ipv6
        server_name  _;                         #域名
        root         /usr/share/nginx/html;     #网站根目录
        root         /var/www/htlm;             #修改为/var(习惯)
        index        index.html;                #默认首页文件
        # load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
 
        location / {
        }
        location / {               
        }

        error_page 404 /404.html;                #404页面文件
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
[root@localhost ~]# nginx -t                     #Nginx检测配置
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful   
[root@localhost ~]# systemctl restart nginx      #重启
root@localhost ~]# mkdir -p /var/www/html     #给首页文件写入内容
[root@localhost ~]# cd /var/www/html
[root@localhost html]# touch index.html
[root@localhost html]# echo "4" > index.html
[root@localhost html]# cat index.html
4
[root@localhost html]# cd
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld
在windows端输入主机ip访问

 编写404 文件:

[root@localhost ~]# cd /var/www/html
[root@localhost html]# vim 404.html
The page is missing
~    
:wq

在windows浏览器界面输入不存在的网页,查看404文件

 

7. Nginx实现反向代理 

  正向解析:在收到客户端请求之后,会修改源IP地址和端口

  反向解析:在收到客户端请求之后,会修改目标IP地址和端口

  上游服务器:代理服务器后端的哪些真正给客户端提供服务的节点,这样的服务器称之为上游服务器。

  下游服务器:客户端就是下游节点。

 

7.1 Nginx反向代理

模块ngx_http_proxy_module

指令

      proxy_pass:指定上游服务器的ip和端口

      proxy_set_header:指定在重新封装请求报文的时候,添加一个新的首部

Syntax:  proxy_pass URl;                           #语法 
Default: —                                         #没有默认值
Context: location, if in location, limit_except    #定义位置

 

规则:

  一定要先启动集群节点服务机的apache服务,作为支持(systemctl restart httpd);服务机192.168.16.5的网首文件

[root@localhost html]# cat index.html
you are wonderful

 

[root@localhost ~]# vim /etc/nginx/nginx.conf
location /zxj {                     #搜索zxj的时候自动跳转到192.168.16.5服务器上去
        proxy_pass http://192.168.16.5/;   # /表示完全代理。能匹配到
        }                                  #location可以写多个
[root@localhost ~]# systemctl restart nginx

windows端访问输入本机ip及匹配名即可跳转到服务机

不加/即不是完全代理时则连接不到

  不加完全代理是可以在.5服务机的相应目录下目录zxj并将index.html文件拷入,反向代理会搜索此目录。


8.Nginx
负载均衡 

8.1 相关概念

   调度器:分发用户的请求到一个后端节点

  上游服务器(真实服务器):每个真正用来处理用户请求的节点都是一个上游服务器

  CIP:client,客户端的IP地址

  RIP:real,真实服务器的IP地址

  VIP:virtual,虚拟IP,用户所看到的是也是虚拟IP

 

8.2  指令模块

  ngx_stream_proxy_module

  upstream

      作用:定义一个上游服务器组

      格式

          upstream name {

              server  上游服务器1  参数 参数;

              server  上游服务器1  参数 参数;

              server  上游服务器1  参数 参数;

             }

Syntax:  proxy_pass address;          #语法
Default: —                            #无默认值
Context: server                       #定义在server

8.3  参数

  weight=#:设置服务器的权重(数字越大,权重越高);

  backup: 设置服务器处于备用状态(其他节点出现故障,备用节点才开始工作);

  down:设置让一个节点处于离线状态(经常用在维护一个节点的情况下);

  max_fails=number:设置连续几次转发失败就认为该节点出现故障,然后就不再向该节点转发用户请求了;

  fail_timeout=time: 和上个参数组合使用,作用是设置等待上游服务器响应超时时间。

 

8.5  配置

  .5首页文件为 you are wonderful;.7首页文件为7777777777

[root@localhost ~]# vim /etc/nginx/nginx.conf
upstream zxj {                                                # 集群名
server 192.168.16.5 weight=5  max_fails=2 fail_timeout=2;     #.5权重为5
server 192.168.16.7 weight=1  max_fails=2 fail_timeout=2;     #.7权重为1
           #假设访问6次请求,.5承担5次客户端请求,.7承担 1次客户端请求
}
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /var/www/html;
        index        index.html;

        # load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        proxy_pass http://zxj/;                         #改为集群名
;wq

windows端输入客户机访问

刷新5次后

  

9. 反向代理和负载均衡的区别

  1> 反向代理proxy后跟一个具体的ip地址;负载均衡proxy后跟的是集群名;

  2> 反向代理代理到的是某一个特定的服务器,而负载均衡器是根据算法调度到集群中的某个节点上。

 

10.LNMP架构上线动态网站

10.1 安装LNMP架构

[root@localhost html]# yum install nginx mariadb-server php php-mysql  php-fpm -y                                                             #完整的LNMP架构

10.2 配置 

   server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /var/www/html;
        index       index.php  index.html;                     #添加php

        # load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location ~ php$ {                                      #正则
        fastcgi_pass 127.0.0.1:9000;                           #php-fpm的监听端口
        include fastcgi.conf;                                  #加载文件
        }
[root@localhost ~]# nginx -t                                   #检查
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]# systemctl restart nginx                    #重启

10.3 上线动态网站 

[root@localhost ~]# cd /var/www/html
[root@localhost html]# rz
 
[root@localhost html]#unzip wordpress-3.3.1-zh_CN.zip
[root@localhost html]# ls
404.html  index.html  wordpress  wordpress-3.3.1-zh_CN.zip
[root@localhost html]# mv wordpress/* .           #将所有文件移动到当前目录
[root@localhost html]# cp wp-config-sample.php  wp-config.php
[root@localhost html]# vim wp-config.php           #更改配置文件
/** WordPress 数据库的名称 */
define('DB_NaME', 'zxj');

/** MySQl 数据库用户名 */
define('DB_USER', 'zxj');

/** MySQl 数据库密码 */
define('DB_PaSSWORD', '123');
;wq
[root@localhost html]# systemctl restart mariadb   #启动数据库
[root@localhost html]# mysql -u root -p            #进入数据库
MariaDB [(none)]> create database zxj;             #创建数据库
MariaDB [(none)]> grant all on *.* to zxj@'localhost' identified by '123';
                                                   #授权用户
MariaDB [(none)]> exit                             #退出
Bye
[root@localhost html]#                             #配置完成

 在windows端访问

成功上线!

猜你喜欢

转载自www.cnblogs.com/ajunyu/p/10886678.html