Nginxphp
apache 仍然是目前的主流,拥有丰富的特性,成熟的技术和开发社区html
二者最核心的区别在于 apache 是同步多进程模型,一个链接对应一个进程,而 nginx 是异步的,多个链接(万级别)能够对应一个进程前端
通常来讲,须要性能的 web 服务,用 nginx 。若是不须要性能只求稳定,更考虑 apache ,后者的各类功能模块实现得比前者,例如 ssl 的模块就比前者好,可配置项多。epoll(freebsd 上是 kqueue ) 网络 IO 模型是 nginx 处理性能高的根本理由,但并非全部的状况下都是 epoll 大获全胜的,若是自己提供静态服务的就只有寥寥几个文件,apache 的 select 模型或许比 epoll 更高性能。固然,这只是根据网络 IO 模型的原理做的一个假设,真正的应用仍是须要实测了再说的。mysql
更为通用的方案是,前端 nginx 抗并发,后端 apache 集群,配合起来会更好。nginx
简单来讲,集群就是指一组相互独立的计算机,利用高速通讯网络组成的一个较大的计算机服务系统,每一个集群节点都是运行各自服务的独立服务器。这些服务器之间能够彼此通讯,协同向用户提供应用程序、系统资源和数据,并以单一系统的模式加以管理。当用户客户机请求集群系统时,集群给用户的感受就是一个单一的服务器,而实际上用户请求的是一组集群服务器。web
集群主要包括几大特色:高性能、价格有效性、可伸缩性、高可用性、透明性、可管理性和可编程性。sql
常见的负载均衡的架构包括有负载均衡集群、高可用性集群、高性能计算集群等等。这里着重介绍负载均衡集群,其余的集群方式不作介绍。数据库
负载均衡集群为企业提供了更为实用、性价比更高的系统架构解决方案。负载集群能够把不少客户集中的访问请求负载压力尽量平均分摊到计算机集群中处理。客户访问请求负载均衡一般包含应用程序处理负载均衡和网络流量负载。这样的系统很是适合使用同一组应用程序为大量用户提供服务的模式,每一个节点均可以承当必定的访问请求负载压力,而且能够实现访问请求在各节点之间动态分配,以实现负载均衡。apache
负载均衡集群运行时,通常是经过一个或多个前端负载均衡器将客户访问请求分发到后端的一组服务器上,从而达到整个系统的高性能和高可用性。通常高可用性集群和负载均衡集群使用相似的技术,或同事具备高可用与负载均衡的特色。负载均衡的做用为:分担用户访问及数据流量、保持业务的连续性、应用于Web业务及数据库从库等服务器的业务。编程
互联网企业中常见的开源集群软件有:Nginx、LVS、Haproxy、Keepalived等,硬件有F五、Netscaler等。
严格地说,Nginx仅仅是做为Nginx Proxy反向代理使用的,由于反向代理功能表现的效果是负载均衡集群的效果,因此也叫作Nginx负载均衡。反向代理和负载均衡的区别在于负载均衡一般都是对请求的数据包的转发(也有可能会改写数据包)、传递,其中DR模式明显的特征就是从负载均衡下面的节点服务器来看,接收到的请求仍是来自负载均衡器的客户端的真实用户。而反向代理,反向代理接收访问用户的请求后,会代理用户从新发起请求代理下的节点服务器,最后把数据返回给客户端用户。在节点服务器来看,访问节点服务器的客户端用户是反向代理服务器,而不是真实的网站访问用户。
Nginx负载均衡的模块主要有两个,ngx_http_proxy_module,ngx_http_upstream_module。编译的时候须要把这两个模块编译进去。
关闭防火墙能够经过方形80端口
iptables -l INPUT -p tcp -d 192.168.23.137 -j ACCEPT
配置网络源
[local]
name=local
enabled=1
gpgcheck=0
baseurl=file:///mnt
[epel]
name=epel
enabled=1
gpgcheck=0
baseurl=https://mirrors.aliyun.com/epel/7Server/x86_64/
下载ngnix并启动
yum install nginx -y
systemctl restart nginx
浏览器访问
反向代理:在收到客户端请求以后,会修改目标IP地址和端口
正向代理:在收到客户端请求以后,会修改源IP地址和端口
上游服务器:代理服务器后端的那些真正给客户端提供服务的节点,这样的服务器称之为上游服务器
下游服务器:客户端就是下游节点
定义:
192.168.23.120 主服务器
192.168.23.121 静态节点
192.168.23.122 动态节点
主服务器部署nginx并配置动静分离规则
vim /etc/nginx/nginx.conf
location ~ html$ {
proxy_pass http://192.168.23.121;
}
location ~ php$ {
proxy_pass http://192.168.23.122;
}
检测并重启nginx
nginx -t
systemctl restart nginx
配置静态服务器
例如:
echo "11111" >> /var/www/html/index.html
配置动态服务器
yum install php httpd -y
vim /var/www/html/index.php
<?php
phpinfo();
?>
浏览器访问
第一步:编辑nginx文件,编辑规则
location /admin{
proxy_pass http://192.168.64.5;
}
第二步:检测并重启
[root@ken html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ken html]# systemctl restart nginx
第三步:浏览器访问
访问不到
第四步:客户端建立文件
[root@ken html]# mkdir admin
[root@ken html]# echo “56566” > admin/index.html
第五步:浏览器再次访问
访问成功
总结:不彻底代理的话location后面定义的访问uri会自动填补到IP地址的后面,location /admin{
proxy_pass http://192.168.64.5;
}
好比上面这个规则,意思就是http://192.168.64.5/admin
第一步:编辑nginx文件,编辑规则
location /admin{
proxy_pass http://192.168.64.5/;
}
第二步:检测并重启
[root@ken html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ken html]# systemctl restart nginx
第三步:浏览器访问
能够访问
总结:彻底代理的话location后面定义的访问uri不会自动填补到IP地址的后面,location /admin{
proxy_pass http://192.168.64.5/;
}
好比上面这个规则,意思就是直接http://192.168.64.5
语法规则: 【= | ^~ | ~ | ~* | / | /uri 】
location = /uri = 表示精确匹配,只有彻底匹配上才能生效,若找到,中止搜索;
location ^~ /uri ^~开头表示对URL路径进行前缀匹配(一般是个目录),而且在正则匹配以前,若找到,中止搜索;
location ~ pattern ~开头表示区分大小写的正则匹配,按配置文件顺序匹配;
location ~* pattern ~*开头表示不区分大小写的正则匹配,按配置文件顺序匹配;
location /uri 不带任何修饰符,表示前缀匹配,在正则匹配以后;
location / 通用匹配,任何未匹配到其余location的请求都会匹配到,至关于default;
多个location配置的状况匹配顺序为
调度器:分发用户的请求到一个后端节点
上游服务器(真实服务器):每一个真正用来处理用户请求的节点都是一个上游服务器
CIP:客户端的IP地址
RIP:真实服务器的IP地址
VIP:虚拟IP,用户所看到的是也是虚拟IP
配置nginx文件(不要在server下配置)
upstream song{
server 192.168.23.121 weight=6 max_fails=2 fail_timeout=2;
server 192.168.23.122 weight=6 max_fails=2 fail_timeout=2;
}
......
location / {
procy_pass http://song
}
检测并重启
nginx -t
systemctl restart nginx
访问浏览器
第一步:下载相关的软件包
[root@ken html]# yum install nginx php php-mysql mariadb-server php-fpm -y
第二步:编辑php匹配规则
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/html;
index index.php index.html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
location ~ php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}
第三步:检测并重启
[root@ken html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ken html]# systemctl restart nginx
第四步:启动php-fpm
[centos]
name=centos base
enabled=1
gpgcheck=0
baseurl=http://mirrors.163.com/centos/7/os/x86_64/
[root@ken yum.repos.d]# systemctl restart php-fpm
第五步:重启数据库
[root@ken yum.repos.d]# systemctl restart mariadb
第六步:建立数据库添加用户
MariaDB [(none)]> create database ken;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on ken.* to ken@’localhost’ identified by ‘123’;
Query OK, 0 rows affected (0.00 sec)
第七步:上传wordpress安装包至nginx网站根目录下/usr/share/nginx/html 并解压
[root@ken html]# yum install unzip -y
[root@ken html]# unzip wordpress-3.3.1-zh_CN.zip
第八步:配置数据库文件
[root@ken html]# cp wp-config-sample.php wp-config.php
[root@ken html]# vim wp-config.php
define(‘DB_NAME’, ‘ken’);
/** MySQL 数据库用户名 */
define(‘DB_USER’, ‘ken’);
/** MySQL 数据库密码 */
define(‘DB_PASSWORD’, ‘123’);
第九步:浏览器访问