友情提示:nginx的前三章节,可以参考上篇博客噢~~~
4、nginx高级配置
4.1 网页的状态页
location /nginx_status {
stub_status; //状态页
auth_basic "auth login"; //身份验证
auth_basic_user_file
示例:
[root@localhost html]# vim /apps/nginx/conf.d/gjpz.conf
[root@localhost conf.d]# vim gjpz.conf
location /nginx_status {
stub_status;
}
nginx -t
nginx -s reload
对面机器验证:
[root@7-3 ~]# curl 192.168.125.140/nginx_status/
Active connections: 3
server accepts handled requests
22 22 21
Reading: 0 Writing: 1 Waiting: 2
拆分:
Active connections:目前在活动的
accepts:总计
handled:自启动
requests:Nginx自启动后客户端发来的总的请求数。
Reading:请求连接
Writing:正在连接
Waiting:结束连接或空闲连接
小拓展:用脚本提取目前的活动连接?
curl 192.168.125.140/nginx_status/ 2>/dev/null |awk '/Active/{print $3}' //以空格为分隔符,提取包含Active字段的第3列
4.2 echo 第三方模块
echo 模块
location /ip {
default_type text/html;
echo "welcome, your ip addr: ";
echo $remote_addr;
}
重新编译安装:
- 关闭nginx、加载安装包、解压、注意路径!
[root@localhost nginx]# cd /opt
[root@localhost opt]# nginx -s stop
同时:加入echo压缩包!!!
[root@localhost opt]# unzip echo-nginx-module-master.zip
[root@localhost opt]# cd nginx-1.18.0 //找到nginx-1.18.0的位置,一定要注意安装时候的路径!!!!!
- 更新编译
./configure --prefix=/apps/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--add-module=/opt/echo-nginx-module-master //添加模块选项,并指明模块的位置/opt。
make -j2 && make install
systemctl start nginx
4.3 变量
4.3.1 内置变量
变量 | 含义 |
---|---|
$remote_addr | 客户端远程地址 |
$proxy_add_x_forwarded_for | 穿透 |
$args | 参数 |
$document_root | 服务器的页面主站点 |
$document_uri | 客户端输入在浏览器中的url |
$host | 服务器地址 |
$remote_port | 客户端端口 |
$remote_user | 虚拟用户 |
$request_filename | 你访问的资源在系统上的绝对路径(真实路径) |
$request_uri | 包含参数的url 除了主机名和主机名前面的都包括 |
$scheme | 协议 http或https |
$server_protocol | 协议版本 |
$server_addr | 服务器地址 |
$server_port | 服务器端口 |
4.3.1.1 客户端远程地址
$remote_addr;
#存放了客户端的地址,注意是客户端的公网IP 谁来访问我
4.3.1.2 穿透
$proxy_add_x_forwarded_for //有用!!!,可以记录所有访问我的客户端ip地址,累加
临时定义:$proxy_add_x_forwarded_for= ipa
4.3.1.3 $args参数
参数作用:形成一条语句,去对接数据库。
如:http://www.kgc.org/main/index.do?id=20190221&partner=search
协议 主机名 url ? 参数(指的是?后面的部分)
4.3.1.4 服务器的页面主站点
nginx软件的根目录,在系统中的真正位置。
$document_root;
#保存了针对当前资源的请求的系统根目录,例如:/apps/nginx/html
示例:
location /ip {
root /opt;
echo $document_root; //对应的是主站点,也就是/opt
}
验证:
curl 192.168.125.140/ip
/opt
4.3.1.5 客户端输入在浏览器中的url
$document_uri;
#保存了当前请求中不包含参数的URI,注意是不包含请求的指令
示例:
location /ip {
root /opt;
echo $document_root;
echo $document_uri;
}
验证:
curl 192.168.125.140/ip
/opt
/ip //访问的连接是谁
4.3.1.6 服务器主机名
$host;
#存放了请求的host名称
示例:
location /ip {
root /opt;
echo $document_root;
echo $document_uri;
echo $host;
}
验证:
[root@7-3 ~]# curl 192.168.125.140/ip
/opt
/ip
192.168.125.140
4.3.1.7 限速
limit_rate 10240;
echo $limit_rate;
#如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没有设置, 则显示0
4.3.1.8 客户端 端口
注意是客户端,服务端基本都是80端口
$remote_port;
#客户端请求Nginx服务器时随机打开的端口,这是每个客户端自己的端口
示例:
location /ip {
root /opt;
echo $document_root;
echo $document_uri;
echo $host;
echo $remote_port;
}
验证:
curl 192.168.125.140/ip
/opt
/ip
192.168.125.140
52550
4.3.1.9 虚拟用户
$remote_user;
#已经经过Auth Basic Module验证的用户名
4.3.1.10 请求方法
方法有 GET下载 / PUT上传 / DELETE删除 等
$request_method;
#请求资源的方式
4.3.1.11 追加
相当于是追加,alias是置换。
$request_filename; //你访问的资源在系统上的绝对路径(真实路径)
4.3.1.12 包含参数的url 除了主机名和主机名前面的都包括
$request_uri; https:// www.baidu.com/main/index.do?id=20190221&partner=search
#包含请求参数的原始URI,不包含主机名,相当于:$document_uri?$args,例如:/main/index.do?id=20190221&partner=search
4.3.1.13 $scheme 协议
$scheme;
#请求的协议,例如:http,https,ftp等
4.3.1.14 协议版本
$server_protocol;
#保存了客户端请求资源使用的协议的版本,例如:HTTP/1.0,HTTP/1.1,HTTP/2.0等
4.3.1.15 服务器地址
$server_addr;
#保存了服务器的IP地址,纯ip
4.3.1.16 服务器主机名
$server_name;
#请求的服务器的主机名
4.3.1.17 服务器端口
$server_port; 443 https
#请求的服务器的端口号
实际操作:
location /main { //注意:验证要验证main
index index.html;
default_type text/html;
echo "hello world,main-->";
echo $remote_addr;
echo $args;
echo $arg_user
echo $document_root;
echo $document_uri;
echo $host;
echo $http_user_agent;
echo $http_cookie;
echo $request_filename;
echo $scheme;
echo $scheme://$host$document_uri?$args;
}
加入此段后 去主机2上进行测试
curl http://www.pc.com/main
curl 'http://www.pc.com/main?user=zhou&title=cto'
curl -b uid=100 'http://www.pc.com/main?user=zhou&title=cto'
-b 加上cookie
注意:不要忘了安装echo
4.3.2 自定义变量
set $name wxj; //$name定义为wxj
echo $name;
示例:
location /test {
set $name wxj;
echo $name;
}
对面机器验证:
[root@7-3 ~]# curl 192.168.125.140/test
wxj
4.4 自定义访问日志
4.4.1 日志的格式 可以自由指定
log_format指令
注意:
- 日志格式一定要在 include 之前 否则会不生效。
- 必须在主配置中进行设置:
access_log logs/pc-access.log test;
4.5 nginx压缩功能
文件传输之前,用cpu进行压缩
#启用或禁用gzip压缩,默认关闭
gzip on | off;
#压缩比由低到高从1到9,默认为1
gzip_comp_level level;
#禁用IE6 gzip功能
gzip_disable "MSIE [1-6].";
#gzip压缩的最小文件,小于设置值的文件将不会压缩,小于1k就不压缩
gzip_min_length 1k;
#启用压缩功能时,协议的最小版本,默认HTTP/1.1
gzip_http_version 1.0 | 1.1;
#指定Nginx服务需要向服务器申请的缓存空间的个数和大小,平台不同,默认:32 4k或者16 8k;
gzip_buffers number size;
#指明仅对哪些类型的资源执行压缩操作;默认为gzip_types text/html,不用显示指定,否则出错!!!!!
gzip_types mime-type ...;
#如果启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding”,一般建议打开
gzip_vary on | off;
#预压缩,先压缩好,不用临时压缩,消耗cpu
gzip_static on | off;
小tips:实验有点小问题,是因为默认只能压缩html或text格式,如果想压缩别的格式,需要设置一下
gzip_types image/jpg image/png;
// vim /etc/mime.types
4.6 https ⭐⭐⭐⭐⭐
证书相当于一个网站的身份证。Web网站的登录页面都是使用https加密传输的。
ssl on | off;
#为指定的虚拟主机配置是否启用ssl功能,此功能在1.15.0废弃,使用listen [ssl]替代
listen 443 ssl;
ssl_certificate /path/to/file;
#指向包含当前虚拟主机和CA的两个证书信息的文件,一般是crt文件
ssl_certificate_key /path/to/file;
#当前虚拟主机使用的私钥文件,一般是key文件
apache:CA证书、自己域名证书、密钥共3个 nginx:CA证书和自己域名证书合成的一个文件、密钥共2个
自签名证书实验:
- 生成证书和密钥
[root@7-1 ~]# cd /etc/pki/tls/certs //证书指定存放位置
[root@7-1 certs]# ls
ca-bundle.crt ca-bundle.trust.crt make-dummy-cert Makefile renew-dummy-cert
[root@7-1 certs]# vim Makefile
57 #/usr/bin/openssl genrsa -aes128 $(KEYLEN) > $@ //57行注释
58 /usr/bin/openssl genrsa $(KEYLEN) > $@ //57行整行复制下来,注意前面不是空格,而是制表符!!!把-aes128的算法去掉。
[root@localhost certs]# make www.pc.com.crt //make命令直接生成密钥
umask 77 ; \
#/usr/bin/openssl genrsa -aes128 2048 > www.pc.com.key
/usr/bin/openssl genrsa 2048 > www.pc.com.key
Generating RSA private key, 2048 bit long modulus
..................................................................................................................................+++
.........................................................................................+++
e is 65537 (0x10001)
umask 77 ; \
/usr/bin/openssl req -utf8 -new -key www.pc.com.key -x509 -days 365 -out www.pc.com.crt
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]:cn
State or Province Name (full name) []:js
Locality Name (eg, city) [Default City]:nj
Organization Name (eg, company) [Default Company Ltd]:pc
Organizational Unit Name (eg, section) []:it
Common Name (eg, your name or your server's hostname) []:www.pc.com
Email Address []:
[root@7-1 certs]# ls
ca-bundle.crt make-dummy-cert renew-dummy-cert www.pc.com.key//密钥
ca-bundle.trust.crt Makefile www.pc.com.crt//证书
[root@7-1 certs]# cp www.* /opt
[root@7-1 certs]# cd /opt
- 编辑配置文件
nginx 的https 功能基于模块ngx_http_ssl_module实现,yum安装的nginx是默认开启的,编译安装的需要设置开启。
vim /apps/nginx/conf.d/pc.conf
server {
listen 80;
listen 443 ssl;
ssl_certificate /opt/www.pc.com.crt; //注意修改路径
ssl_certificate_key /opt/www.pc.com.key;
server_name www.pc.com;
root /data/pc/;
}
[root@7-1 conf.d]# nginx -t
[root@7-1 conf.d]# nginx -s reload
- 真机验证
https://192.168.125.100
小拓展:nginx软件有问题了,怎么修复?
升级(重新编译)openssl ⭐⭐
- 首先加载openssl-1.1.1k.tar.gz 压缩包
- 在之前编译的基础上,增加下面的内容
--with-openssl=/data/openssl-1.1.1k //加这个选项就行了,注意上面的记得加 \
5、rewrite 重写功能
5.1 ngx_http_rewrite_module模块指令
5.1.1 if指令
if指令放在server、location里面
语法:
if (条件匹配) {
action
}
= #比较变量和字符串是否相等,相等时if指令认为该条件为true,反之为false
!= #比较变量和字符串是否不相等,不相等时if指令认为条件为true,反之为false
~ #区分大小写字符,可以通过正则表达式匹配,满足匹配条件为真,不满足匹配条件为假
!~ #区分大小写字符,判断是否匹配,不满足匹配条件为真,满足匹配条件为假
~* #不区分大小写字符,可以通过正则表达式匹配,满足匹配条件为真,不满足匹配条件为假
!~* #不区分大小字符,判断是否匹配,满足匹配条件为假,不满足匹配条件为真
!!!!注意:
#如果$变量的值为空字符串或0,则if指令认为该条件为false,其他条件为true。 变量值为假,就不执行
<!---->
location /test {
index index.html;
default_type text/html;
if ( $scheme = https ){ //注意:上面https实验做好才能做这个,不然只能用http
echo "if-----> $scheme";
}
}
真机验证:
https://192.168.125.140/test
5.1.2 return指令
语法:
return code [text]
命令 状态码 文本
示例一:
location /test {
index index.html;
default_type text/html;
if ( $scheme = http ){
return 301 https://192.168.125.140;
} //如果访问http,就重定向到https://192.168.125.140
真机验证:
https://192.168.125.140/test
-I //加-I可以查看状态码666
示例二:
location /test {
index index.html;
default_type text/html;
return 666 "hello world"; //自定义
}
}
真机验证:
https://192.168.125.140/test
虚拟机验证:
[root@7-1 conf.d]# curl 192.168.125.140/test -I
HTTP/1.1 666
Server: nginx/1.18.0
Date: Wed, 05 Jun 2024 07:38:41 GMT
Content-Type: text/html
Content-Length: 11
Connection: keep-alive
重定向:
- 301(永久重定向) :服务器不需要每次向 客户提供新的url ,客户访问过后,会记录在自己的缓存中,即使nginx服务器死机,客户在一定时间内 还可以继续跳转
- 302(临时重定向) :没有缓存 服务器断开无法重定向
5.1.3 set指令
语法:
set $变量名 变量值
5.1.4 break 中断
用于中断 当前相同location中的其他Nginx配置,位于它前面的配置生效,位于后面的指令就不再执行,为空。
如果在同一location中的break指令下面,出现以下同级别的rewrite指令,都不执行:
- if
- return
- rewrite
- set
示例:
- 设置配置文件
[root@localhost conf.d]# vim pc.conf
server {
listen 80;
server_name www.pc.com;
root /data/pc/;
location / {
set $name wyf;
echo $name;
break; //设置break,后面rewrite同级别的指令,将验证为空
set $work idol;
echo $work;
echo $host; //不是rewrite同级别的指令,所以可以正常执行!
}
}
- 启配置
[root@localhost conf.d]# nginx -t
[root@localhost conf.d]# nginx -s reload
- 验证
5.2 rewrite 重写指令⭐⭐⭐⭐⭐⭐
rewrite 可以通过正则表达式匹配 来改变URI。 rewrite 将用户请求的URI基于regex所描述的模式进行检查,匹配到时 将其替换为表达式指定的新的URI。
rewrite可以配置在 server、location、if 中。
语法格式:
rewrite regex replacement(www.baidu.com) flag;
命令 正则 替代的url 标志
5.2.1 正则
. #匹配除换行符以外的任意字符
\w #匹配字母或数字或下划线或汉字
\s #匹配任意的空白符
\d #匹配数字 [0-9]
\b #匹配单词的开始或结束
^ #匹配字付串的开始
$ #匹配字符串的结束
* #匹配重复零次或更多次
+ #匹配重复一次或更多次
? #匹配重复零次或一次
(n) #匹配重复n次
{n,} #匹配重复n次或更多次
{n,m} #匹配重复n到m次
*? #匹配重复任意次,但尽可能少重复
+? #匹配重复1次或更多次,但尽可能少重复
?? #匹配重复0次或1次,但尽可能少重复
{n,m}? #匹配重复n到m次,但尽可能少重复
{n,}? #匹配重复n次以上,但尽可能少重复
\W #匹配任意不是字母,数字,下划线,汉字的字符
\S #匹配任意不是空白符的字符
\D #匹配任意非数字的字符
\B #匹配不是单词开头或结束的位置
[^x] #匹配除了x以外的任意字符
[^kgc] #匹配除了kgc 这几个字母以外的任意字符
location /break {
root /data/html/;
rewrite ^/break/(.*) /test/$1 break; //最后的break表示立即匹配
}
5.2.2 flag标志
- 301:premanent 永久重定向
- 302:redirect 临时重定向
- break:是标志,并非break指令!!!立即执行,下面的就不看了,也不用执行
- last:防止死循环,不建议在location中使用
5.2.2.1 实验一 目的:把 hn 替换成 henan
将 www.hn.com 替换成 www.henan.com
方法一:
[root@localhost conf.d]# vim pc.conf
server {
root /data/html/;
location /hn {
rewrite ^/hn/(.*) /henan/$1 permanent; //以hn开头的,第一列替换成henan,永久生效
}
}
[root@localhost conf.d]# mkdir /data/html/henan -p //必须要有henan的文件夹
[root@localhost conf.d]# cd /data/html/henan
[root@localhost henan]# echo "666">index.html
[root@localhost henan]# nginx -s reload
真机验证:
192.168.125.140/hn/
方法二:
[root@localhost conf.d]# vim pc.conf
server {
root /data/html/;
location /hn {
rewrite ^/hn/(.*) /henan/$1 permanent;
location /henan {
root /data/html/; //指定henan下的主站点,那么location下的配置优于server先生效。
rewrite ^/hn/(.*) /henan/$1 permanent;
}
}
}
5.2.2.2 实验二 目的:将 www.pc.com 替换成 www.m.com
[root@localhost conf.d]# vim /apps/nginx/conf.d/pc.conf
server {
listen 80;
server_name www.pc.com;
root /data/html/;
location / {
rewrite / http://www.m.com/ permanent; //把www.pc.com 替换成www.m.com
}
}
server {
listen 80;
server_name www.m.com; //新建www.m.com的虚拟主机
root /opt;
}
[root@localhost conf.d]# nginx -s reload
[root@localhost conf.d]# cd /opt
[root@localhost opt]# echo "m">index.html
5.2.2.3 实验三 目的:访问http 自动跳转为https
[root@localhost ~]# vim /apps/nginx/conf.d/pc.conf
server {
listen 80;
server_name www.pc.com;
root /data/html/;
location / {
if ( $scheme = http) {
rewrite ^/(.*)/$ https://www.pc.com/$1 permanent;
}
}
}
[root@localhost ~]# nginx -s reload
[root@localhost ~]# cd /data/html
[root@localhost html]# echo "cxk" >index.html
`对面服务器验证:`
[root@7-1 ~]# vim /etc/hosts
192.168.125.140 www.pc.com
[root@7-1 ~]# curl http://www.pc.com
cxk
真机验证:注意要修改真机的hosts域名!!!!
小拓展: 真机下的hosts文件(windows)
C:\Windows\System32\drivers\etc
// 注意:用管理员方式打开,如果 没有权限打开,就先复制到桌面,改好后再替换覆盖!!!
curl选项 | 含义 |
---|---|
-L | 跳转 |
-v | 显示过程 |
-I(大i) | 只输出响应的头部信息 |
- break:是立即终止匹配 使用该url 进行访问
- last:停止本location中的匹配,开启下一个location
location 匹配
location /break {
root /data/;
rewrite ^/break/(.*) /test1/$1 break;
rewrite ^/test1/(.*) /break/$1 break;
}
location /last {
root /data/;
rewrite ^/last/(.*) /test1/$1 last;
rewrite ^/test1/(.*) /test2/$1 last;
}
location /test1 {
echo "new test1";
}
location /test2 {
echo "new test2";
}
mkdir /data/test1
mkdir /data/test2
echo /data/test1 > /data/test1/index.html
echo /data/test2 > /data/test2/index.html
curl 192.168.125.100/break/index.html
/data/test1
curl 192.168.125.100/last/index.html
new test1
5.3 防盗链(优化)⭐⭐⭐⭐
有专门的模块 referer
Module ngx_http_referer_module
5.3.1 盗链:编译安装
实验环境:
盗图狗:192.168.125.150
被盗:192.168.125.140
- 被盗:设置子配置、并加载图片
[root@7-4 ~]# systemctl stop firewalld
[root@7-4 ~]# setenforce 0
`设置子配置和主站点:`
[root@7-4 ~]# vim /apps/nginx/conf.d/pc.conf //主站点一定一定一定要定义好!!!
server {
listen 80;
server_name www.pc.com;
root /data/html/;
}
[root@7-4 ~]# nginx -s reload
`加载图片:`
[root@7-4 ~]# mkdir -p /data/html
[root@7-4 ~]# cd /data/html
************加载图片a.jpg************
[root@7-4 html]# ls
a.jpg dtg.png
- 盗图狗:设置html语言
[root@7-5 ~]# systemctl start nginx
`创建主站点:` //盗图狗和被盗都尽量定义子配置和主站点,不然效果容易出问题。
[root@7-5 ~]# vim /apps/nginx/conf.d/pc.conf
server {
listen 80;
server_name www.pc.com;
root /data/html/;
}
`新建html页面:`
[root@7-5 ~]# mkdir -p /data/html
[root@7-5 ~]# cd /data/html //如果是yum安装,使用:cd /usr/share/nginx/html/⭐
[root@7-5 html]# vim index.html
<html>
<body>
<h1>this is nginx </h1>
<img src="http://192.168.125.140/a.jpg"/>
</body>
</html>
[root@7-5 html]# systemctl restart nginx
- 真机验证:
192.168.125.150 //访问150,就能看到图片,但是150本身没有图片,实际看的是140的图
5.3.2 防盗链
- valid_referers 合法
- invalid_referers 非法
// referer 就是你从哪里跳转过来
none:空 请求报文首部没有referer首部,比如用户直接在浏览器输入域名访问web网站,就没有referer信息。
blocked:无效值 请求报文有referer首部,但无有效值,比如为空。
被盗的设置:
[root@7-5 ~]# cd /data/html
******可以加入一张dtg.png********
[root@7-5 ~]# vim /apps/nginx/conf.d/pc.conf
server {
listen 80;
server_name www.pc.com;
root /data/html/;
location ~* .(jpg|gif|swf|jpeg|bmp)$ {
valid_referers none blocked *.pc.com pc.com; //允许合法访问的情况:none、blocked...
if ( $invalid_referer ) { //非法访问,返回403
rewrite ^/ http://www.pc.com/dtg.png; //准备盗图狗的图片,注意后缀不要在上述(jpg|gif|swf|jpeg|bmp)范围里面,不然会形成死循环。
#return 403;
}
}
}
[root@7-5 ~]# nginx -t
[root@7-5 ~]# nginx -s reload
注意:排错时,也可以加上DNS域名,查看什么原因
6、反向代理
proxy_pass 反向代理,可以放置的位置:location、if in location、limit_except(限流模块)
语法格式:
proxy_pass url
示例:
location {
proxy_pass http://192.168.125.130; //如果访问本机的主站点,那么就访问130的主站点
}
- 正向代理:代理客户端, 加快访问速度
- 反向代理:代理服务端 负载均衡 高并发
反向代理软件 按照级别划分 从大到小:
- lvs 百万级 只支持四层,与keepalive配合使用
- haproxy 十万级 支持四+七层
- nginx 万级 支持四+七层
nginx支持 同构和异构:
- 同构代理:前端和后端用的是一个协议
- 异构代理:前端和后端用的不是一个协议
6.1 单台反向代理⭐:
实验环境:
7-2:客户机 192.168.125.10
7-1:代理服务器 192.168.125.100
7-3:web服务器 192.168.125.130
实验目的:7-2发请求,给7-1代理服务器,7-1再转发给7-3。也就是7-2发请求,实际看到的是7-3的网页
`7-1:`
[root@7-1 ~]# vim /apps/nginx/conf.d/pc.conf
server {
listen 80;
server_name www.pc.com;
root /data/html/;
location / {
proxy_pass http://192.168.125.130; //七层代理,因为http协议是在7层
}
}
[root@7-1 ~]# nginx -s reload
`7-3:`
[root@localhost ~]# yum install httpd -y
[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# cd /var/www/html; //apache默认网页存放位置
[root@7-3 html]# echo "this is 33333" >index.html
`7-2:`
[root@7-2 ~]# curl 192.168.125.100
this is 33333
小拓展:
yum安装的主配置文件位置:/etc/nginx/nginx.conf
yum安装的网页,默认存放位置:/usr/share/nginx/html
apache网页默认存放位置:/var/www/html
6.2 加不加 /斜杠 的问题
加/是置换,不加是追加。
环境:代理服务器192.168.125.100
真实服务器192.168.125.130
root /data/html;
location /test { ##后面的url是test
proxy_pass http://192.168.125.130; ## 注意: /斜杠是加在130后面!!!
前提是 代理服务器 和 真实服务器 都有文件夹:/data/html/test
- 追加: 访问 192.168.125.100/test/ 等于访问 192.168.125.130/test/
- 置换: 访问 192.168.125.100/test/ 等于访问 192.168.125.130/的主站点 /data/html/test文件 置换成了 /data/html/
小拓展1: location ~* /test {
开启正则,后面就不能加 / 。因为匹配范围太广泛了,不合常理。
小拓展2: 添加监听端口
proxy_pass http://192.168.125.130:8080; //8080端口不是默认的监听端口,可以在主配置中进行添加。
vim /etc/httpd/conf/httpd.conf //以apache的主配置为例
listen 80 //默认的
listen 8080 //自行添加
systemctl restart httpd //修改完后,重启
小拓展3: 抓包命令
tcpdump -i ens33 src host 192.168.91.100 and dst host 192.168.91.101 -nn
拆分:
tcpdump: 抓包工具的命令。
-i ens33: 指定网络接口 ens33。
src host 192.168.91.100: 指定源地址为 192.168.91.100。
dst host 192.168.91.101: 指定目的地址为 192.168.91.101。
-nn 参数确保了输出中不进行任何名称解析,即地址和端口号将直接以数字形式显示。
小拓展4: 清除缓存
echo 3 > /proc/sys/vm/drop_caches //内核参数,用于清除系统缓存。
通过向/proc/sys/vm/drop_caches文件写入数字,可以清除不同类型的缓存:
1:清除页缓存(page cache)和目录项缓存(dentries)。
2:清除slab缓存,这是内核数据结构的缓存,比如文件对象和inode对象。
3:清除上述所有类型的缓存,即页缓存、目录项缓存和slab缓存。
通常用于性能测试中,以确保测试结果不受现有缓存的影响。然而,请注意,这将清除所有缓存,可能会导致系统性能暂时下降,因为系统需要重新加载数据到缓存中。
小拓展5: 状态码:502 和 504
- 502:如果很快回复502的话,代表后台服务器坏了,这样不太好。可以设置防火墙规则:
iptables -A INPUT -s 192.168.91.100 -j DROP //已读不回
- 504:后台服务器处理时间过长,超时
6.3 动静分离⭐
nginx不擅长处理动态资源。
实验环境:
7-1:代理服务器 192.168.125.100
7-2:静态服务器 192.168.125.120
7-3:动态服务器 192.168.125.130
7-4:客户端 192.168.125.140
实验目的:7-4客户端访问7-1代理服务器,7-1根据需求 分配给不同的web服务器,如果客户端访问的是静态资源,如以.jpg或者.html或者.png等结尾的,就分配给7-2静态服务器处理;如果是动态资源,就转发给7-3处理。
7-1:
[root@7-1 ~]# vim /apps/nginx/conf.d/pc.conf
server {
listen 80;
server_name www.pc.com;
root /data/html/;
location /api { //动态
proxy_pass http://192.168.125.130/; //后面加斜杠,代表置换
}
location ~* .(jpg|html|png)$ { //静态;开启正则;.是转义的点,前面不要加根
proxy_pass http://192.168.125.120; //开启正则,就不能加斜杠,不然会报错
}
}
[root@7-1 ~]# nginx -t
[root@7-1 ~]# nginx -s reload
7-2:
[root@7-2 ~]# yum install httpd -y
[root@7-2 ~]# systemctl start httpd
[root@7-2 ~]# cd /var/www/html
[root@7-2 html]# ls
zcm.jpg //静态图片
7-3:
[root@7-3 ~]# yum install httpd -y
[root@7-3 ~]# systemctl start httpd
[root@7-3 ~]# cd /var/www/html
[root@7-3 html]# echo "7-3 动态" > index.html
[root@7-3 html]# cat index.html
7-3 动态
7-4:验证动态资源
[root@7-4 ~]# curl 192.168.125.100/api
7-3 动态
[root@7-4 ~]# curl 192.168.125.100/zcm.jpg //图片验证是乱码
真机:验证静态资源
6.4 缓存功能
正常的缓存是在内核里的缓存。但是,下面的实验可以缓存在 代理服务器的硬盘中。
好处: 客户端访问时,如果代理服务器缓存中有该文件,就可以自己直接回复客户端,而不需要在转发给真实服务器处理,节省了时间和资源。
知识点:
proxy_cache_path 指定缓存的路径,这个指令必须放在http语句块中。
/data/nginx/proxycache 指明存放的具体位置⭐
levels=1:1:1 生成三级目录,1代表16位
keys_zone=proxycache:20m 指内存中缓存的大小,key用来分辨、寻找缓存, 20m是20分钟
inactive=120s 缓存有效时间是120秒
max_size=10g; 最大磁盘占用空间,磁盘存入文件内容的缓存空间最大值
示例:
server {
listen 80;
proxy_cache proxycache; //开启缓存,并把缓存放到/data/nginx/proxycache中,默认是off关闭。
proxy_cache_key $request_uri; //加密算法
#proxy_cache_key $host$uri$is_args$args; //另一种加密算法
proxy_cache_valid 200 302 301 10m; //合法的状态码,缓存10分钟
proxy_cache_valid any 5m; //其他合法的状态码,缓存5分钟
}
6.4.1 缓存实验
- 实验前,先测试
在未开启缓存前可以测试下载速度
ab -c100 -n1000 http://192.168.125.100/12.png
拆分:
ab: Apache Bench 的缩写,是一个用于测试Web服务器性能的压力测试工具。
-c100: 表示并发数为100,即同时有100个请求被发送到服务器。
-n1000: 表示总共发送1000个请求。
- 设置主配置文件
[root@7-1 ~]#
vim /apps/nginx/conf/nginx.conf
http{ //必须要在http语句块中
proxy_cache_path /data/nginx/proyxcache/ levels=1:1:1 keys_zone=proxycache:20m inactive=120s max_size=1g; //注意:这个一定要放在 include子配置路径 的前面!!!
}
[root@7-1 ~]# nginx -s reload
- 设置子配置
[root@7-1 ~]# vim /apps/nginx/conf.d/pc.conf
server {
listen 80;
server_name www.pc.com;
root /data/html/;
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 10m;
proxy_cache_valid any 5m; //主要是补充这四行
location /api {
proxy_pass http://192.168.125.130/;
}
location ~* .(jpg|html|png)$ {
proxy_pass http://192.168.125.120;
}
}
[root@7-1 ~]# nginx -s reload
[root@7-1 ~]# mkdir -p /data/nginx/
[root@7-1 nginx]# tree /data/nginx
/data/nginx
└── proyxcache
*******此时进行测试,可以查看图片********
[root@7-1 nginx]# tree /data/nginx
/data/nginx
└── proyxcache
└── 9
└── e
└── 2
└── 35ddeb8160bd39673c08e37c06c8c2e9
4 directories, 1 file //测试完毕后,缓存已经产生!
扩展知识:如何删除(清理)缓存
方法一:直接删掉 proxycache文件
方法二:第三方扩展模块ngx_cache_purge
6.4.2 添加首部字段
可以查看是否命中缓存。
代理服务器发给客户数据的时候,可以自定义
###添加响应报文的自定义首部:
add_header name value
命令 名字 值
示例:
add_header class ky36; //子配置中添加首部字段
[root@7-4 ~]# curl 192.168.125.100/api -I //测试访问
class:ky36 //效果
###判断是否命中缓存:
add_header X-Via $server_addr; //当前nginx主机的IP
add_header X-Cache $upstream_cache_status; //是否命中缓存
add_header X-Accel $server_name; //客户访问的FQDN
是否命中缓存:
X-Cache:HIT //命中
X-Cache:MISS //没有命中
小拓展:proxy_hide_header 隐藏首部字段
proxy_hide_header 字段名; //加字段名,可以隐藏该字段。
// 注意:如果是默认字段,则无法隐藏。
6.5 客户端 ip地址透传
真实服务器的日志中,只能看到 代理服务器访问了自己,无法查看客户端的访问信息。
nginx 日志格式的变量:
"$http_x_forwarded_for";
x_forwarded作用是:如果它为空,那么对应就是$remoteaddr;如果有值,就是追加新的$remoteaddr
示例:分为apache和nginx
- apache需要调整下日志格式
- nginx中自带固定的变量格式,不需要自己加
*******apache写法:*******
proxy_set_header X-Real-IP $remote_addr; //添加客户端ip到请求报文头部,转发到真实服务器。X-Real-IP可以自定义,比如改成cxk。
******nginx写法:********
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #添加客户端IP和反向代理服务器IP到请求报文头部。X-Forwarded-For是规定动作,不能自定义修改。
6.5.1 apache透传实验:
实验目的: 使真实服务器apache的日志中,能看到客户端的ip地址。
实验环境:
7-1 代理服务器 192.168.125.100
7-3 真实服务器 192.168.125.130
7-4 客户端 192.168.125.140
7-1:
[root@7-1 nginx]# vim /apps/nginx/conf.d/pc.conf
server {
listen 80;
server_name www.pc.com;
root /data/html/;
proxy_set_header cxk $remote_addr; //自定义名字cxk,$remote_addr是远端的ip地址
location /api {
proxy_pass http://192.168.125.130/;
}
location ~* .(jpg|html|png)$ {
proxy_pass http://192.168.125.120;
}
}
[root@7-1 nginx]# nginx -s reload
7-3:
[root@7-3 html]# vim /etc/httpd/conf/httpd.conf //修改apache主配置文件
196 #LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined //196行注释
197 LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i" "%{cxk}i"" combined //复制196行,并增加 "%{cxk}i",反斜杠代表转义,%代表变量,i代表首部字段。
[root@7-3 html]# systemctl start httpd
7-4:访问验证
[root@7-4 ~]# curl 192.168.125.100/api
7-3 动态
此时,再查看7-3的日志信息:
[root@7-3 html]# tail -f /var/log/httpd/access_log
192.168.125.100 - - [09/Jun/2024:13:49:20 +0800] "GET / HTTP/1.0" 200 11 "-" "curl/7.29.0" "192.168.125.140" //日志里就有了客户端的ip地址,实验成功!
6.5.2 nginx透传实验:
apache适用于单层代理,不适用于多层代理。
nginx适用于单层和多层代理。
X-Forwarded-For (功能非常强大)
- X-Forwarded-For 如果为空,就代表$remote_addr远端的ip地址
- X-Forwarded-For 如果有值,就追加新的$remote_addr远端的ip地址
多层透传实验:
实验目的:真实服务器的日志中,可以看到客户端和所有代理服务器的ip地址。
实验环境:
7-4 客户端 192.168.125.140
7-1 代理服务器 192.168.125.100 nginx
7-2 代理服务器 192.168.125.120 nginx
7-3 真实服务器 192.168.125.130 nginx
7-1:
[root@7-1 nginx]# vim /apps/nginx/conf.d/pc.conf
server {
listen 80;
server_name www.pc.com;
root /data/html/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; //添加变量,规定名称为X-Forwarded-For,不能修改!
location /api {
proxy_pass http://192.168.125.120/; //注意:ip是第二台代理服务器
}
}
[root@7-1 nginx]# nginx -s reload
7-2:
[root@7-2 ~]# vim /etc/nginx/nginx.conf //yum安装的nginx主配置文件
http {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; //可以加在http语句块中
}
location / {
proxy_pass http://192.168.125.130; //注意:ip是真实服务器
}
[root@7-2 ~]# nginx -s reload
7-3:
[root@7-3 ~]# systemctl start nginx
[root@7-3 ~]# cd /usr/share/nginx/html
[root@7-3 html]# echo "this is 7-3" > index.html
7-4:访问
[root@7-4 ~]# curl 192.168.125.100/api
this is 7-3
7-3:查看日志
[root@7-3 ~]# tail -f /var/log/nginx/access.log //yum安装的nginx日志路径
192.168.125.120 - - [09/Jun/2024:16:12:24 +0800] "GET / HTTP/1.0" 200 12 "-" "curl/7.29.0" "192.168.125.140, 192.168.125.100" //可以看到120、100、140访问了我
6.6 七层负载均衡和调度算法
自定义一组服务器,配置在http模块。
代理服务器的调度算法,分为三种:
-
轮询(rr),也是默认的调度算法
-
加权轮询,根据权重分配次数
-
哈希算法:
- ip hash
- url hash
- cookie hash
- 一致性 hash
小拓展: 如果某台真实服务器挂了,代理服务器知道吗?
知道!nginx有后端健康检查,能检测后端是否活着。 用的三次握手
小tips:
真实服务器,也叫做 上游服务器
6.6.1 轮询
实验环境:
7-1 代理服务器 192.168.125.100 nginx
7-2 真实服务器 192.168.125.120 apache
7-3 真实服务器 192.168.125.130 apache
7-4 客户端 192.168.125.140
实验目的: 客户端访问的请求,由7-1代理服务器轮询分配,第一个给7-2,第二个给7-3,第三个给7-2,第四个给7-3….以此类推,类似于平均分配。
7-1:
[root@7-1 ~]# vim /apps/nginx/conf/nginx.conf //注意:upstream要写在http语句块中,所以要写主配置模块里!
http {
upstream web { //web是组名
server 192.168.125.120;
server 192.168.125.130;
} //也可以写域名,但是域名慢,因为域名需要解析!
}
[root@7-1 nginx]# vim /apps/nginx/conf.d/pc.conf //子配置文件设置
location / {
proxy_pass http://web/; //web是组名,以前是单台服务器,现在是服务器组。加/斜杠是置换,不加是追加,本次加不加/无所谓。
}
[root@7-1 ~]# nginx -s reload
7-2:
[root@7-2 ~]# systemctl start httpd
[root@7-2 ~]# cd /var/www/html
[root@7-2 html]# echo "7-2 7-2" > index.html
7-3:
[root@7-3 ~]# systemctl start httpd
[root@7-3 ~]# cd /var/www/html
[root@7-3 html]# echo "33333333" > index.html
7-4:
[root@localhost ~]# curl 192.168.125.100
7-2 7-2
[root@localhost ~]# curl 192.168.125.100
33333333
[root@localhost ~]# curl 192.168.125.100
7-2 7-2
[root@localhost ~]# curl 192.168.125.100
33333333
//轮询,7-2处理完,下一个由7-3处理,7-3处理完,再下一个由7-2处理
6.6.2 加权轮询
weight=number //设置权重
本实验是在 6.6.1轮询的基础上进行调整:
7-1:
[root@7-1 ~]# vim /apps/nginx/conf/nginx.conf //主配置
http {
upstream web {
server 192.168.125.120 weight=2; //添加权重为2
server 192.168.125.130; //不添加默认权重为1
}
}
[root@7-1 ~]# nginx -s reload
7-4:
[root@localhost ~]# curl 192.168.125.100
7-2 7-2
[root@localhost ~]# curl 192.168.125.100
7-2 7-2
[root@localhost ~]# curl 192.168.125.100
33333333
[root@localhost ~]# curl 192.168.125.100
7-2 7-2
[root@localhost ~]# curl 192.168.125.100
7-2 7-2
[root@localhost ~]# curl 192.168.125.100
33333333
//设置权重后,7-2工作2次,7-3工作1次,并不是完全绝对
语法:
backup //设置为备份服务器,当所有后端服务器不可用时,才会启用此备用服务器 sorry server 自己不能转自己
down //标记为down状态,也就是下线,但是没坏掉,就是不工作了。一般用于调试
resolve //当server定义的是域名,一般不用该种写法,写域名会比较麻烦。
backup备份 示例:
7-1:
[root@7-1 ~]# vim /apps/nginx/conf/nginx.conf //主配置
http {
upstream web {
server 192.168.125.120;
server 192.168.125.130;
server 192.168.125.200 backup;
} //200是备胎,当120和130都挂了,200就会接着工作,代理服务器不能设置为备胎!!!
6.6.3 哈希hash算法
6.6.3.1 ip hash
根据ip地址,来决定客户端访问的服务器。
6.6.3.1.1 md5sum算法
可以转成十六进制,示例:
[root@7-1 ~]# cd /opt
[root@7-1 opt]# touch 192.168.125.130
[root@7-1 opt]# md5sum 192.168.125.130
d41d8cd98f00b204e9800998ecf8427e 192.168.125.130 //只要对应的ip地址不变,计算出来的十六进制数据就不会变!
6.6.3.1.2 ip
hash $remote_addr
根据后面的访问地址,比如访问192.168.125.120/test,那么后面再访问该文件,会由同一台服务器进行处理。
ip哈希 示例:
[root@7-1 ~]# vim /apps/nginx/conf/nginx.conf
http {
upstream web { //hash只能放upstream下面!!!
hash $remote_addr; //根据对端客户端的ip地址,一旦确定,就不会改变
server 192.168.125.120;
server 192.168.125.130;
}
[root@7-1 ~]# nginx -s reload
7-4:
[root@localhost ~]# curl 192.168.125.100
33333333
[root@localhost ~]# curl 192.168.125.100
33333333
[root@localhost ~]# curl 192.168.125.100
33333333
[root@localhost ~]# curl 192.168.125.100
//根据hash算法,ip地址一样时,分配给同一台服务器
小拓展1:对 权重总和 进行取模(RANDOM)
echo $[$RANDOM%3] //随机数对3取模,生成的范围就是 0-2
小拓展2:上面的权重总和是什么意思?
例1:
server 192.168.125.120;
server 192.168.125.130;
//上述没有设置权重,那就是默认权重为1,因为有两台服务器,所以权重总和是1+1=2
例2:
server 192.168.125.120 weight=2; //权重为2
server 192.168.125.130 weight=3; //权重为3
//上述例子的权重总和为:2+3=5
小拓展3:权重总和为5,取模范围是多少?又是如何分配服务器的呢?
对权重总和 取模就是RANDOM5,取模范围就是0-4。假设有三台服务器同时工作,那么取模为0分配给第一台,取模为1分配给第二台,后面的2、3、4全部分配给第三台。
小拓展4:取模有什么好处?为什么要取模?
取模的好处,是将ip地址固定下来,从而将某个ip地址 固定的分配给同一个服务器,这样可以提高处理效率
6.6.3.2 url hash
hash $request_uri
根据客户端访问的url 来决定访问的服务器。
格式:
[root@7-1 ~]# vim /apps/nginx/conf/nginx.conf
http {
upstream web { //hash只能放upstream下面!!!
hash $request_uri; //发请求的地址,一旦定下不会轻易改变
server 192.168.125.120;
server 192.168.125.130;
}
[root@7-1 ~]# nginx -s reload
6.6.3.3 cookie 本地缓存
根据cookie的值,来决定访问的服务器
7-1:
http {
upstream web {
hash $cookie_hello; //从报文的cookie当中,筛选出hello
server 192.168.125.120;
server 192.168.125.130;
}
7-4:
curl -b hello=cxk 192.168.125.100 //-b是指定cookie
6.6.4 最小连接算法
least_conn
代理服务器根据 后台服务器的连接数 进行分配,谁的连接数最少,就优先分配给谁。
6.6.5 fair
根据响应时间进行分配。
6.7 四层负载均衡
四层根据协议和端口号。默认是tcp,如果想控制udp协议,监听的时候要写上udp。
stream 注意:stream 和 http语句块 同级别!!!
stream {
upstream...... //upstream在stream模块下。
}
实验目的:通过访问代理服务器的6379端口,去调度redis(redis 内存数据库,以速度快著称)。
- 安装redis
7-2、7-3、7-4:
yum install epel-release.noarch -y
yum install redis -y
- 修改配置文件
7-2和7-3:
vim /etc/redis.conf
bind 0.0.0.0 //将回环地址127.0.0.1改成0.0.0.0
systemctl start redis
7-2:
redis-cli //客户端工具,可以登录自己
127.0.0.1:6379> set name wyf //插入变量name=wyf
OK
127.0.0.1:6379> get name
"wyf"
7-3:
redis-cli //客户端工具,可以登录自己
127.0.0.1:6379> set name cxk //插入变量name=cxk
OK
127.0.0.1:6379> get name
"cxk"
7-1:
vim /apps/nginx/conf/nginx.conf
stream {
upstream redis {
server 192.168.125.120:6379;
server 192.168.125.130:6379;
}
}
server {
listen 6379; //默认是tcp协议,如果想用udp,就是listen 6379 udp;
proxy_pass redis; //调用redis
}
nginx -s reload
ss -natp | grep nginx
- 验证
7-4:
redis-cli -h 192.168.125.100
192.168.125.100:6379> get name
7、LNMP 架构
CGI是临时工,FastCGI是正式工
vim /apps/nginx/conf/nginx.conf //主配置里部分东西都已经写好了,比如下面的默认配置示例:
#location ~ .php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000; //php程序在哪里?本机的9000端口
# fastcgi_index index.php; //默认的动态资源文件
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; //绝对路径,去哪里找后端安装程序
# include fastcgi_params; //配置文件,已经设置好,不用改
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
# deny all;
#}
论坛是个软件程序,是以php开发的。
环境准备 lnmp 需要 安装 nginx mysql php 软件
注意:关闭防火墙和selinux
7.1 编译安装nginx
7.1.1 安装依赖包
[root@7-2 ~]# cd /data
******注意:加载nginx-1.22.0.tar.gz*****
[root@7-2 data]# ls
nginx-1.22.0.tar.gz ssh.log
[root@7-2 data]# tar xf nginx-1.22.0.tar.gz //解压
[root@7-2 data]# cd nginx-1.22.0/
[root@7-2 nginx-1.22.0]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make //安装依赖环境
7.1.2 创建运行用户、组
useradd -M -s /sbin/nologin nginx //Nginx 服务程序默认以 nobody 身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限
7.1.3 编译安装nginx
cd nginx-1.22.0/
./configure \
--prefix=/usr/local/nginx \ //注意:编译安装的路径
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ //软链接
tee /lib/systemd/system/nginx.service <<eof //tee是直接生成文件
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -1 $MAINPID
ExecStop=/bin/kill -3 $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
eof
systemctl daemon-reload
systemctl start nginx
chmod 777 /lib/systemd/system/nginx.service //权限 改不改都可以
7.2 yum安装mysql
有两个源:
第一个清华源:
[root@centos7 ~]# tee /etc/yum.repos.d/mysql.repo <<EOF
[mysql]
name=mysql5.7
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el7-x86_64/
gpgcheck=0
EOF
第二个官方源: 建议使用官方源
[root@centos7 ~]# tee /etc/yum.repos.d/mysql.repo <<EOF
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/
enabled=1
gpgcheck=0
EOF
yum -y install mysql-community-server //社区版,免费的
systemctl start mysqld
grep password /var/log/mysqld.log //查看密码
mysql -u root -p"手动加上密码!!!!" //比如:mysql -u root -p";7(V_.CYay(8"
mysql密码太复杂了,怎么修改密码?
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=1; //前面的mysql> 不要复制
//改之前必须先加 修改密码策略
mysql> alter user root@'localhost' identified by '123123'; //将密码改成123123
[root@7-2 nginx-1.22.0]# mysql -u root -p"123123" //按照新密码重新登录
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.44 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. //登录成功!
7.3 编译安装php
cd /data
******把php的两个压缩包加载进来*******
[root@7-2 data]# tar xf php-7.1.10.tar.bz2
[root@7-2 data]# cd php-7.1.10/
7.3.1 安装环境依赖包
yum -y install gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel
7.3.2 编译安装
./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip
make -j2 && make install
7.3.3 路径优化
ln -s /usr/local/php/bin/* /usr/local/bin/
ln -s /usr/local/php/sbin/* /usr/local/sbin/
//两个软链接都要做!
7.3.4 调整php的三个配置文件
php.ini 主配置文件
php-fpm.conf 进程服务配置文件
www.conf 扩展配置文件
7.3.4.1 调整主配置文件:
cp /data/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini
cd /usr/local/php/lib/
vim php.ini
--1170行--修改 //1170G,可以直接跳转
mysqli.default_socket = /var/lib/mysql/mysql.sock //注意:前面的路径是yum安装的,如果是编译安装mysql 位置是:/usr/local/mysql/mysql.sock /var/lib/mysql/mysql.sock
--939行--修改 //939G
date.timezone = Asia/Shanghai //先取消注释,再修改
7.3.4.2 调整进程服务配置文件:
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
vim php-fpm.conf
--17行--修改
pid = run/php-fpm.pid //取消注释
7.3.4.3 调整扩展配置文件:
cd /usr/local/php/etc/php-fpm.d/
cp -a www.conf.default www.conf
7.3.5 解析
cd /data/php-7.1.10/sapi/fpm
cp /data/php-7.1.10/sapi/fpm/php-fpm.service /usr/lib/systemd/system/php-fpm.service
systemctl daemon-reload
systemctl start php-fpm.service
ss -natp | grep 9000 //查看下9000端口是否开启。
7.4 配置nginx支持php解析
- 编辑配置
vim /usr/local/nginx/conf/nginx.conf
第一处:
--65到71行取消注释-- //先取消注释
65 location ~ .php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; //再修改路径
70 include fastcgi_params;
71 }
第二处:
43 location / {
44 root html;
45 index index.html index.htmi index.php; //新增index.php
46 }
nginx -s reload
cd /usr/local/nginx/
- 测试页面
cd /usr/local/nginx/html/
vim index.php
<?php
phpinfo();
?>
nginx -s reload
192.168.125.120/index.php //真机访问
- 验证数据库工作是否正常
mysql -u root -p"123123"
CREATE DATABASE bbs; //创建数据库名称为bbs
GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'admin123'; //用户名是bbsuser,密码是admin123
GRANT all ON bbs.* TO 'bbsuser'@'localhost' IDENTIFIED BY 'admin123';
flush privileges;
vim /usr/local/nginx/html/index.php //再写个测试脚本,替换原来的测试页内容
<?php
$link=mysqli_connect('192.168.125.120','bbsuser','admin123');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>
浏览器访问: 192.168.125.120/index.php
4. 部署论坛
cd /data
unzip Discuz_X3.4_SC_UTF8.zip
cp -a dir_SC_UTF8/upload/ /usr/local/nginx/html/bbs/
cd /usr/local/nginx/html/bbs/
真机访问:192.168.125.120/bbs
- 论坛权限
如何解决上述不可写的问题?增加权限!!!
chown -R nobody ./config/
chown -R nobody ./data/
chown -R nobody ./uc_client/
chown -R nobody ./uc_server/