Nginx安装与升级

Nginx安装

Nginx简介

  • Nginx(”engine x”)
  • 是俄罗斯人编写的十分轻量级的HTTP服务器
  • 是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP代理服务器
  • 官方网站:http://nginx.org/

Nginx安装

1
2
3
4
5
6
7
8
9
10
[root@nginx ~]# yum install gcc pcre-devel openssl-devel -y
[root@nginx ~]# useradd nginx
[root@nginx ~]# tar xf nginx-1.14.2.tar.gz
[root@nginx ~]# cd nginx-1.14.2/
[root@nginx nginx-1.14.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module
[root@nginx nginx-1.14.2]# make && make install

Nginx配置文件及目录

  • /usr/local/nginx/ 安装目录
  • conf/nginx.conf 主配置文件
  • html 网页目录
  • logs 日志文件
  • sbin/nginx 启动脚本

启动Nginx服务

1
[root@nginx ~]# /usr/local/nginx/sbin/nginx

常用选项

  • -V:查看编译参数
  • -c:指定配置文件,启动服务
  • -t:测试配置文件语法是否错误

查看服务进程及端口信息

1
2
3
4
5
6
7
8
[root@nginx ~]# ps aux |grep nginx
root 3900 0.0 0.0 45924 1116 ? Ss 09:10 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 3901 0.0 0.0 46372 1892 ? S 09:10 0:00 nginx: worker process
root 15962 0.0 0.0 112704 972 pts/0 S+ 09:18 0:00 grep --color=auto nginx

[root@nginx ~]# ss -luntp | grep nginx
tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=3901,fd=6),("nginx",pid=3900,fd=6))

清空防火墙、设置selinux

1
2
[root@nginx ~]# iptables -F
[root@nginx ~]# setenforce 0

升级Nginx

平滑升级Nginx服务

  • 源码编译新版Nginx
1
2
3
4
[root@nginx ~]# tar xf nginx-1.22.1.tar.gz 
[root@nginx ~]# cd nginx-1.22.1/
[root@nginx nginx-1.22.1]# ./configure --with-http_ssl_module
[root@nginx ~]# make

使用新版Nginx替换旧版Nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
###旧版
[root@nginx nginx-1.22.1]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module

[root@nginx nginx-1.22.1]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
[root@nginx nginx-1.22.1]# cp objs/nginx /usr/local/nginx/sbin/
[root@nginx nginx-1.22.1]# make upgrade


###新版
[root@nginx nginx-1.22.1]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.22.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --with-http_ssl_module

Nginx基础配置

配置文件

配置文件结构

  • /usr/local/nginx/conf/nginx.conf
1
2
3
4
5
6
7
8
9
10
11
#全局配置
http {
······
server {
······
location / {
······
}
}
}

用户认证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
server {
listen 80;
server_name localhost;
auth_basic "auth-domain";
auth_basic_user_file /usr/local/nginx/pass;
}

[root@nginx ~]# yum install httpd-tools.x86_64 -y

#创建认证用户(因为没有pass文件,-c创建)
[root@nginx ~]# htpasswd -c /usr/local/nginx/pass admin
New password: 000000
Re-type new password: 000000
Adding password for user admin

#追加认证用户
[root@nginx ~]# htpasswd /usr/local/nginx/pass wsq
New password:
Re-type new password:
Adding password for user wsq

[root@nginx ~]# cat /usr/local/nginx/pass
admin:$apr1$2V1yB1nx$SzOINxqw7/LH2LC8Hz94C0
wsq:$apr1$HxXLWbFQ$U53CJVGSl0.yjyD.wzufW1

可以通过浏览器访问本机IP验证

重新加载配置

  • 因为修改了配置文件,所以需要reload重新加载配置
1
[root@nginx ~]# /usr/local/nginx/sbin/nginx -s reload

虚拟主机

每一个server就是一台虚拟主机

基于域名的虚拟主机

1
2
3
4
5
6
7
8
9
10
11
server {
listen 80;
server_name web1.cn; #域名
······
}

server {
listen 80;
server_name web2.cn; #域名
······
}

基于端口的虚拟主机

1
2
3
4
5
6
7
8
9
10
11
server {
listen 8080; #端口
server_name web1.cn; #域名
······
}

server {
listen 8000; #端口
server_name web2cn; #域名
······
}

基于IP的虚拟主机

1
2
3
4
5
6
7
8
9
10
11
server {
listen 192.168.1.10:80; #端口
server_name web1.cn; #域名
······
}

server {
listen 192.168.1.10:81; #端口
server_name web2.cn; #域名
······
}

客户端测试

  • 修改hosts域名解析

    windows:C:\Windows\System32\driver\etc\hosts

    linux:/etc/hosts

HTTPS加密网址

密钥算法

常见密钥算法

  • 对称加密

    AES、DES

  • 非对称加密

    ESA、DSA

SSL虚拟主机

生成密钥

  • ssl加密网站的核心技术是非对称生成密钥

    公钥、私钥、证书

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@nginx conf]# cd /usr/local/nginx/conf/

#生成私钥
[root@nginx conf]# openssl genrsa > cert.key
Generating RSA private key, 2048 bit long modulus
......................................+++
........................................................................................+++
e is 65537 (0x10001)

#生成证书
[root@nginx conf]# openssl req -new -x509 -key cert.key > cert.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:ch #国家名
State or Province Name (full name) []:hn #省名
Locality Name (eg, city) [Default City]:ny #城市
Organization Name (eg, company) [Default Company Ltd]:dz #组织/公司
Organizational Unit Name (eg, section) []:dz #组织单位
Common Name (eg, your name or your server's hostname) []:wsq #姓名
Email Address []:123@123.com #电子邮件

SSL加密网站

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
#配置文件中有,取消注释就行
server {
listen 443 ssl;
server_name localhost;
ssl_certificate cert.pem;
ssl_certificate_key cert.key;

location / {
root html;
index index.html index.htm;
}
}

[root@nginx ~]# /usr/local/nginx/sbin/nginx -s reload

客户端测试

浏览器访问https://主机IP/测试

Nginx地址重写

地址重写

应用案例

  • web1.cn/a.html —> web.1.cn/b.html

1
2
3
4
5
6
7
server {
listen 80;
server_name web1.cn;
rewrite "/a.html$" /b.html;
······
/a.html$ 以a.html结尾
[root@nginx ~]# /usr/local/nginx/sbin/nginx -s reload
1
2
3
4
5
6
7
server {
listen 80;
server_name web1.cn;
rewrite ^/ http://www.tmooc.cn;
······

[root@nginx ~]# /usr/local/nginx/sbin/nginx -s reload
1
2
3
4
5
6
7
server {
listen 80;
server_name web1.cn;
rewrite ^/(.*)$ http://www.tmooc.cn/$1;
······
(.*)$ 以任意个字符结尾 $1 引用
[root@nginx ~]# /usr/local/nginx/sbin/nginx -s reload

LNMP动态网站

LNMP

Nginx+FastCGI+PHP-FPM

  • FastCGI是nginx服务器与PHP沟通的一种语言

  • FastCommonGatewayInterrface(快速通用网络接口)

什么是LNMP

主流的企业网站平台之一

  • L:Linux操作系统
  • N:Nginx网站服务软件
  • M:MySQL、MariaDB数据库
  • P:网站开发语言(PHP、Perl、Python)

图片音频视频为静态数据

脚本为动态数据

部署LNMP

安装Nginx

  • 源码编译安装Nginx
1
2
3
4
5
6
7
8
9
10
[root@nginx ~]# yum install gcc pcre-devel openssl-devel -y
[root@nginx ~]# tar xf nginx-1.14.2.tar.gz
[root@nginx ~]# cd nginx-1.14.2/

[root@nginx nginx-1.14.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module
[root@nginx nginx-1.14.2]# make && make install

安装MariaDB

  • 数据库服务器、数据库客户端、依赖包
1
[root@nginx ~]# yum install mariadb mariadb-server mariadb-devel.x86_64 -y

安装PHP

  • php解释器、fastcgi接口、php连接数据库的扩展包
1
[root@nginx ~]# yum install php php-fpm php-mysql -y

启动服务

  • 启动Nginx、MariaDB、PHP-FPM
1
2
3
4
5
6
7
8
9
[root@nginx ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/  

[root@nginx ~]# nginx
[root@nginx ~]# systemctl enable --now mariadb.service
[root@nginx ~]# systemctl enable --now php-fpm

[root@nginx ~]# ss -luntp | grep 80
[root@nginx ~]# ss -luntp | grep 3306
[root@nginx ~]# ss -luntp | grep 9000

配置动静分离

location语法

  • location / 优先级最低
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
location /test {
root html;
index index.html index.htm;
deny all;
}
location /video {
root html;
index index.html index.htm;
allow 192.168.210.57;
deny all;
}
location / {
root html;
index index.html index.htm;
allow all;
}

编辑nginx.conf

1
2
3
4
5
6
7
8
9
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
##取消注释
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf; ###修改
}

编写测试脚本

1
2
3
4
5
6
[root@nginx ~]# vim /usr/local/nginx/html/test.php
<?php
$i=33;
echo $i;
?>

客户端访问测试

浏览器访问主机IP/test.php

Wordpress案例

wordpress简介

  • wordpress是使用PHP语言开发的博客平台,可以支持PHP和MySQL数据库的服务器上架设自己的网站
  • WordPress功能强大、插件众多、主题丰富
  • 官方网址:https://wordpress.org

上线WordPress

数据库操作

  • 创建数据库
  • 创建数据库账户并赋权
1
2
3
4
5
[root@nginx ~]# mysql -uroot -p000000
MariaDB [(none)]> create database wordpress;
MariaDB [(none)]> grant all on wordpress.* to wordpress@'%' identified by "000000";
MariaDB [(none)]> grant all on wordpress.* to wordpress@'localhost' identified by "000000";
MariaDB [(none)]> flush privileges;

上传代码到LNMP平台

1
2
3
4
5
6
7
8
9
10
11
12
[root@nginx ~]# yum install unzip -y
[root@nginx ~]# unzip wordpress-4.7.3-zh_CN.zip
[root@nginx ~]# cp -r wordpress/* /usr/local/nginx/html/

#将index.php设为默认页面
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.php index.html index.htm;
}

[root@nginx ~]# nginx -s reload

客户端测试

浏览器访问本机IP即可打开wordpress安装页面

Nginx七层代理

所谓七层负载均衡,也称为“内容交换”,也就是主要通过报文中的真正有意义的应用层内容,再加上负载均衡设备设置的服务器选择方式,决定最终选择的内部服务器,他走的是http协议

节点规划

1.png

部署web服务

安装软件

  • web1和web2做相同操作
1
2
3
4
5
6
7
8
9
10
11
12
[root@web1 ~]# setenforce 0
[root@web1 ~]# systemctl disable --now firewalld

[root@web1 ~]# yum install gcc pcre-devel openssl-devel mariadb mariadb-devel mariadb-server.x86_64 php php-fpm php-mysql -y && systemctl enable --now mariadb php-fpm

[root@web1 ~]# tar xf nginx-1.22.1.tar.gz
[root@web1 ~]# cd nginx-1.22.1/
[root@web1 nginx-1.22.1]# ./configure --with-http_ssl_module
[root@web1 nginx-1.22.1]# make && make install
[root@web1 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@web1 ~]# nginx
[root@web1 ~]# echo "nginx" >> /etc/rc.local #开机自启

配置动静分离

  • web1和web2做相同操作
1
2
3
4
5
6
7
8
9
[root@web1 ~]# vim /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
[root@web1 ~]# nginx -s reload

编写测试页面

1
2
3
4
5
6
7
8
9
10
11
[root@web1 ~]# vim /usr/local/nginx/html/test.php
<?php
$i=11;
echo $i;
?>

[root@web2 ~]# vim /usr/local/nginx/html/test.php
<?php
$i=12;
echo $i;
?>

部署代理服务器

安装软件

1
2
3
4
5
6
7
8
9
10
11
12
[root@proxy ~]# setenforce 0
[root@proxy ~]# systemctl disable --now firewalld

[root@proxy ~]# yum install gcc pcre-devel openssl-devel mariadb mariadb-devel mariadb-server.x86_64 php php-fpm php-mysql -y && systemctl enable --now mariadb php-fpm

[root@proxy ~]# tar xf nginx-1.22.1.tar.gz
[root@proxy ~]# cd nginx-1.22.1/
[root@proxy nginx-1.22.1]# ./configure --with-http_ssl_module
[root@proxy nginx-1.22.1]# make && make install
[root@proxy ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@proxy ~]# nginx
[root@proxy ~]# echo "nginx" >> /etc/rc.local #开机自启

Nginx代理语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
http {
upstream servers {
server 192.168.1.11:80;
server 192.168.1.12:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://servers;
root html;
index index.php index.html index.htm;
}
}
[root@proxy ~]# nginx -s reload

客户端测试反复访问测试效果

1
http://192.168.1.10/test.php

配置upstarem集群池属性

1
2
3
4
5
6
7
8
9
10
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
upstream servers {
server 192.168.1.11 weight=1 max_fails=1 fail_timeout=30;
server 192.168.1.12 weight=2 max_fails=2 fail_timeout=30;
server 192.168.1.10 down
}
#weight 设置服务器权重值,默认1
#max_fails 设置最大失败次数
#fail_timeout设置失败超时时间,单位为秒
#down标记服务器已关机,不参与调度

wordpress代码数据迁移

配置NFS服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@proxy ~]# yum install nfs-utils rpcbind -y 
#rpcbind端口注册
[root@proxy ~]# mkdir /wordpress
[root@proxy ~]# vim /etc/exports
/wordpress *(rw)
[root@proxy ~]# systemctl enable --now nfs rpcbind

##把wordpress代码复制到nfs目录
[root@proxy ~]# cp -r wordpress/* /wordpress/

##检测
[root@proxy ~]# showmount -e
Export list for proxy:
/wordpress *

web服务器挂载共享存储

  • web1和web2做相同操作
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@web1 ~]# yum install nfs-utils && systemctl enable --now nfs
[root@web1 ~]# vim /etc/fstab
192.168.1.10:/wordpress/ /usr/local/nginx/html/ nfs defualts,_netdev 0 0
[root@web1 ~]# mount -a
[root@web1 ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
location / {
root html;
index index.php index.html index.htm;
#添加index.php
}

客户端测试反复访问测试效果

1
http://192.168.1.10

Nginx四层代理

所谓四层负载均衡,也就是主要通过报文中的目标地址和端口,再加上负载均衡设备设置的服务器选择方式,决定最终选择的内部服务器,它一般走的是tcp,udp协议

模块

  • 需要ngx_stream_core_module模块
  • 使用–with-stream开启模块

安装stream模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@web1 ~]# setenforce 0
[root@web1 ~]# systemctl disable --now firewalld

[root@web1 ~]# yum install gcc pcre-devel openssl-devel mariadb mariadb-devel mariadb-server.x86_64 php php-fpm php-mysql -y && systemctl enable --now mariadb php-fpm

[root@web1 ~]# tar xf nginx-1.22.1.tar.gz
[root@web1 ~]# cd nginx-1.22.1/
[root@web1 nginx-1.22.1]# ./configure \
--with-http_ssl_module \
--with-stream
[root@web1 nginx-1.22.1]# make && make install
[root@web1 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@web1 ~]# nginx
[root@web1 ~]# echo "nginx" >> /etc/rc.local #开机自启

修改Nginx配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
stream {
upstream backend {
server 192.168.1.11:22;
server 192.168.1.12:22;
}
server {
listen 12345;
proxy_pass backend;
}
}

http {
······
}
[root@proxy ~]# nginx -s reload

客户端测试

使用Xshell等远程连接192.168.1.10:12345端口

Nginx优化

自定义错误页面

HTTP常见错误代码表

返回码 描述
200 一切正常
400 请求语法错误
401 访问被拒绝(账户或密码错误)
403 资源不可用,通常由于服务器上文件或目录的权限导致
403 禁止访问:如用户IP地址被拒绝,无法验证密码
404 无法找到指定位置的资源(Not Found)
414 请求URL头部太长
500 服务器内部错误
502 服务器作为网关或代理时,为了完成请求访问下一个服务器,但该服务器返回了非法的应答(Bad Gateway)

Nginx返回错误页面

1
2
3
4
5
6
7
[root@web1 ~]# vim /usr/local/nginx/conf/nginx.conf
http {
server {
error_page 404 /40x.html; #自定义错误页面(图片或html)
······
}
}

客户端访问一个不存在的页面测试

Nginx状态页面

安装status模块

  • with-http_stub_status_module 开启模块功能
  • 可以查看Nginx连接数等信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@web1 ~]# setenforce 0
[root@web1 ~]# systemctl disable --now firewalld

[root@web1 ~]# yum install gcc pcre-devel openssl-devel mariadb mariadb-devel mariadb-server.x86_64 php php-fpm php-mysql -y && systemctl enable --now mariadb php-fpm

[root@web1 ~]# tar xf nginx-1.22.1.tar.gz
[root@web1 ~]# cd nginx-1.22.1/
[root@web1 nginx-1.22.1]# ./configure \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-stream
[root@web1 nginx-1.22.1]# make && make install
[root@web1 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@web1 ~]# nginx
[root@web1 ~]# echo "nginx" >> /etc/rc.local #开机自启

激活status

  • 修改配置文件
1
2
3
4
5
6
[root@web1 ~]# vim /usr/local/nginx/conf/nginx.conf
······
location /status {
stub_status on;
}
······

客户端访问测试

1
http://192.168.1.10/status

状态信息

  • Active connections

    当前活动的连接数量

  • Accepts

    已经接受客户端的连接总数量

  • Handled

    已经处理客户端的连接总数

    一般与accepts一致,除非服务器限制了连接数量

  • Requests

    客户端发送的请求数量

  • Reading

    当前服务器正在读取服务器客户端请求头的数量

  • Writing

    当前服务器正在写响应信息的数量

  • Waiting

    当前多少客户端在等待服务器的响应

优化并发连接数

优化前

  • 安装压力测试软件ab

    对服务器进行压力测试

1
2
3
4
5
[root@web1 ~]# yum install httpd-tools -y
[root@web1 ~]# ab -c 100 -n 100 http://192.168.1.10/
c 人数 n 总访问量
[root@web1 ~]# ab -c 2000 -n 2000 http://192.168.1.10/
socket: Too many open files (24) #报错打开太多网页

优化Nginx配置

  • 优化进程数量
  • 优化并发连接数量
1
2
3
4
5
6
7
[root@web1 ~]# vim /usr/local/nginx/conf/nginx.conf
#修改
worker_processes 4; #与cpu核心数一致
events {
worker_connections 50000; #每个worder最大并发连接数
}
[root@web1 ~]# nginx -s reload

修改linux内核参数

  • 修改最大文件数量
1
2
3
4
5
6
[root@web1 ~]# ulimit -a             #查看内核限制
[root@web1 ~]# ulimit -n 200000 #临时修改内核限制
[root@web1 ~]# vim /etc/security/limits.conf #永久修改
* soft nofile 200000
* hard nofile 200000
#用户或组 软限制或硬限制 项目 值

优化后再做测试

1
[root@web1 ~]# ab -c 2000 -n 2000 http://192.168.1.10/

日志切割

Nginx日志文件随着时间增长会越来越大

编写日志切割脚本

  • 每周5的3点3分自动执行脚本完成切割工作
1
2
3
4
5
6
7
8
9
10
11
[root@web1 ~]# vim /usr/local/nginx/logbak.sh
#!/bin/bash
date=`date +%Y%m%d`
logpath=/usr/local/nginx/logs
mv $logpath/access.log $logpath/access-$date.log
mv $logpath/error.log $logpath/error-$date.log
kill -USR1 $(cat $logpath/nginx.pid)

[root@web1 ~]# crontab -e
03 03 * * 5 /usr/local/nginx/logbak.sh
#分 时 日 月 周

开启gzip压缩

1
2
3
4
5
6
7
8
9
10
11
[root@web1 ~]# vim /usr/local/nginx/conf/nginx.conf
http {
······
gzip on; #开启压缩
gzip_min_length 1000; #小文件不压缩
gzip_comp_level 4; #压缩比率
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
#对特定文件压缩,参考mime.types
······
}
[root@web1 ~]# nginx -s reload

开启文件缓存

  • 使用内存缓存磁盘中的文件
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@web1 ~]# vim /usr/local/nginx/conf/nginx.conf
http {
······
open_file_cache max=2000 inactive=20s;
open_file_cache_valid 60s;
open_file_cache_min_uses 5;
open_file_cache_errors off;
······
#设置最大缓存2000个文件句柄,关闭20秒内无请求的文件句柄
#文件句柄的有效时间是60秒,60秒后过期
#只有访问次数超过5次会缓存
}
[root@web1 ~]# nginx -s reload

Session与Cookie

Session与Cookie概述

  • Session保存在服务器(存储用户登录状态、浏览历史等)

  • Cookie保存在客户端(存储的是SessionID等信息)

    由服务器下发给客户端,保存在客户端的一个文件里

Session共享

  • 将Session保存到同一台共享服务器中共享给所有web服务器

2.png

Memcached概述

  • memcached是高性能的分布式缓存服务器

  • 用来集中缓存数据库查询结果,以提高动态Web应用的相应速度

  • 官方网站:http://memcached.org/

部署memecached

1
2
3
4
5
[root@proxy ~]# yum install memcached -y
[root@proxy ~]# systemctl enable --now memcached.service
[root@proxy ~]# iptables -F
[root@proxy ~]# setenforce 0

查看配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@proxy ~]# cat /etc/sysconfig/memcached 
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""

[root@proxy ~]# cat /usr/lib/systemd/system/memcached.service
[Unit]
Description=Memcached
Before=httpd.service
After=network.target
[Service]
Type=simple
EnvironmentFile=-/etc/sysconfig/memcached
ExecStart=/usr/bin/memcached -u $USER -p $PORT -m $CACHESIZE -c $MAXCONN $OPTIONS
[Install]
WantedBy=multi-user.target

安装PHP扩展

  • 默认php是无法连接memcached数据库
  • 需要安装扩展软件包,才可以让PHP连接memcached数据库
  • 是给web主机安装扩展包
1
2
3
[root@web1 ~]# yum install php-pecl-memcache -y

[root@web2 ~]# yum install php-pecl-memcache -y

修改PHP配置文件

  • 修改2台web服务器,让PHP连接到192.168.1.10这台数据库服务器
  • 实现Session共享到memecached数据库中
1
2
3
4
5
6
7
8
[root@web1 ~]# vim /etc/php-fpm.d/www.conf
#修改前
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
#修改后
php_value[session.save_handler] = memcache
php_value[session.save_path] = "tcp://192.168.1.10:11211"
[root@web1 ~]# systemctl restart php-fpm.service

客户端访问测试