location的写法
一、location的写法
作用: 匹配客户端响应
location [ = | ~ | ~* | ^~ ] uri { … }
优先级: =, ^~, ~, ~*, location /
1、= 精确匹配
location = /upload {
}
http://x.x.x.x/upload
2、~ 以正则表达式匹配请求,区分大小写
location ~ /test {
}
http://x.x.x.x/upload/test
http://x.x.x.x/test/a/b/c
location ~ \.(jpg|jpeg|gif|png)$ {
}
location ~ \.php$ {
}
3、~* 以正则匹配请求,不区分大小写
location ~* /download {
}
http://x.x.x.x/test/download/
http://x.x.x.x/DownLoad/test
4、^~ 不以正则的方式匹配请求
location ^~ /test {
}
http://x.x.x.x/test
二、stub_status模块显示工作状态
location /status {
stub_status;
access_log off;
allow 192.168.140.1;
deny all;
}
Active connections: 1 // 当前的活跃连接数
server accepts handled requests
2809 2809 2937
Reading: 0 Writing: 1 Waiting: 0
accepts:接收的连接数
handlerd:处理的连接
requests:请求数
三、url地址重写 rewrite模块
让服务器接收到请求后,根据需求改写地址,以改写的地址给客户端响应
1、语法
rewrite 旧uri地址 新uri地址;
注意:
1、旧uri匹配请求时,是不包含请求的参数,不包括?后面的内容
2、旧uri地址支持正则表达式
3、避免循环匹配,否则会产生5xx的错误
2、针对项目结构有变化
rewrite ^/audio/ http://music.linux.com/mp3/;
rewrite ^/audio/(.*) http://music.linux.com/mp3/$1;
3、网站改名的情况
rewrite ^/ https://www.jd.com;
4、https自动跳转
if ($host = secure.linux.com) {
rewrite ^/(.*) https://secure.linux.com/$1;
}
四、LNMP平台
1、核心区别
将httpd换为nginx,高并发、高性能
nginx通过fastCGI机制调用PHP,处理动态资源
php以fpm的方式运行,有自己独立的进程、服务
2、部署LNMP平台
2.1 安装平台所需的软件
[root@localhost ~]# yum install -y mariadb-server php72w php72w-cli php72w-fpm php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml
2.2 启动php-fpm、数据库
[root@localhost ~]# systemctl enable --now mariadb
[root@localhost ~]# systemctl enable --now php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
[root@localhost ~]#
[root@localhost ~]# netstat -tunlp | grep php
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 7986/php-fpm: maste
2.3 将nginx和php融合
[root@localhost conf.d]# vim vedio.conf
location ~ \.php$ {
root /vedio;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
[root@localhost conf.d]# /usr/local/nginx/sbin/nginx -t
[root@localhost conf.d]# /usr/local/nginx/sbin/nginx -s reload
2.4 测试
<?php
phpinfo();
?>
<?php
$link=mysqli_connect("localhost", "root", "");
if($link)
echo "ok";
else
echo "error";
?>