Nginx学习1------部署LNMP、构建LNMP平台、地址重写

部署LNMP

源码安装Nginx,使用RPM包安装MariaDB、PHP、PHP-FPM软件。
操作过程中需要安装的软件列表如下:
nginx
mariadb、mariadb-server、mariadb-devel
php、php-fpm、php-mysql
备注:mariadb(数据库客户端软件)、mariadb-server(数据库服务器软件)、mariadb-devel(其他客户端软件的依赖包)、php(解释器)、php-fpm(进程管理器服务)、php-mysql(PHP的数据库扩展包)
一)安装依赖包
yum -y install gcc openssl-devel pcre-devel
二)源码安装Nginx
yum -y install gcc pcre-devel openssl-devel
useradd -s /sbin/nologin nginx
tar -xf nginx-1.10.3.tar.gz
cd nginx-1.10.3
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
make && make install
三)安装MariaDB
yum -y install mariadb mariadb-server mariadb-devel
四)安装PHP、php-frm
yum -y install php php-mysql php-fpm

编写一个PHP测试页面
vim /usr/local/nginx/html/test.php

<?php $i=1024; echo $i; ?>

php /usr/local/nginx/html/test.php ####运行脚本,屏幕输出1024

启Nginx服务:
启mariadb服务:
systemctl start mariadb
systemctl status mariadb
systemctl enable mariadb
启php-fpm服务
systemctl start php-fpm
systemctl status php-fpm
systemctl enable php-fpm

构建LNMP平台

查看php-fpm配置文件(实验中不需要修改该文件)
1.[root@proxy etc]# vim /etc/php-fpm.d/www.conf
2.[www]
3.listen = 127.0.0.1:9000 //PHP端口号
4.pm.max_children = 32 //最大进程数量,1个进程大概30M,根据内存的大小来设置
5.pm.start_servers = 15 //最小进程数量
6.pm.min_spare_servers = 5 //最少需要几个空闲着的进程
pm.max_spare_servers = 32 //最多允许几个进程处于空闲状态

了解location,一个server可以有多个location
server {
listen 80;
server_name www.a.com
location /{
allow all;
}
location /test {
allow 1.1.1.1;
deny all;
}
location /abc {
deny all;
}

}

只考虑语法,不考虑合理性
firefox http://www.a.com ###匹配location /
firefox http://www.a.com/test ###匹配location /test,如果你是1.1.1.1,访问,其他人访问不了
firefox http://www.a.com/abc ###匹配location /abc谁都打不开
firefox http://www.a.com/ttttt ###优先精确匹配,找不到,区配/,/的优先级最低

动静分离:
location匹配用户的地址栏,支持正则
server {
listen 80;
server_name www.a.com
location /{
root html;
}
location ~ .php${ ###转义"."字符为普通的“.”,使用正则,前面加“~”,模糊匹配
root html;
fastcgi_pass 127.0.0.1:9000; //转发给9000
}
}
firefox http://www.a.com/a.jpg
电脑去/usr/local/nginx/html/a.jpg

firefox http://www.a.com/test.php
电脑去/usr/local/nginx/html/test.php
把脚本给9000(PHP)去执行一遍

实际操作
vim /usr/local/nginx/conf/nginx.conf
66 location ~ .php$ {
67 root html;
68 fastcgi_pass 127.0.0.1:9000;
69 fastcgi_index index.php;
70 fastcgi_param SCRIPT_FILENAME /scriptsKaTeX parse error: Expected 'EOF', got '}' at position 81: …s; 72 }̲ 根据情况而定,把这段#号去掉… {
67 root html;
68 fastcgi_pass 127.0.0.1:9000;
69 fastcgi_index index.php;
70 include fastcgi.conf; ##include调fastcgi.conf这个文件,删除了fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;这一行
71 }
访问:www.a.com/test.php

如果想让客户输入www.a.com访问test.php网页,只需要默认路径那添 加
location / {
root html;
index test.php index.html index.htm;
}

常见报错:
错误日志:
/usr/local/nginx/logs/error.log
/var/log/php-fpm/www-error.log
1.提示An error occurred,这不是报错,需要查看错误日志,可以用tailf来看,它打开查看文件不退出,tail查看直接退出
2.没写location[下载]
3.什么也没有,可能是php脚本错,可以查看/var/log/php-fpm/www-error.log

PV UV
wc -l access.log === PV(page view网页看了多少次)

UV (user view)并发量
awk ‘{print $1}’ /usr/local/nginx/logs/access.log | sort -r |uniq -c | wc -l

编写PHP脚本连接数据库
cd /usr/local/nginx/html
vim mysql.php

<?php
$mysqli = new mysqli('localhost','root','','mysql');
if (mysqli_connect_errno()){
        die('Unable to connect!'). mysqli_connect_error();
}
$sql = "select * from user";
$result = $mysqli->query($sql);
while($row = $result->fetch_array()){
        printf("Host:%s",$row[0]);
        printf("</br>");
        printf("Name:%s",$row[1]);
        printf("</br>");
}
?>

curl www.a.com/mysql.php
mysal
grant all on . to abc@‘localhost’ identified by ‘123’;
再访问,发现就有变化

地址重写

格式:
rewrite regex replacement flag
rewrite 旧地址 新地址 [选项]

1.所有访问a.html的请求,重定向到b.htm
这种方法:地址栏显示不改变,但是访问的网页改变了
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.a.com;
rewrite /a.html /b.html; //写在server里面就行,不分前后
location / {
root html;
index test.php index.html index.htm;
}

[root@localhost html]# echo “我是B” > b.html 做一个b.html
[root@localhost html]# curl www.a.com/a.html
我是B
rewrite /a.html /b.html redirect;///地址栏发生变化

2.访问www.a.com跳转到www.baidu.com
server {
listen 80;
server_name www.a.com;
rewrite ^/ http://www.baidu.com; ###“^/”用的是正则,只要是以www.a.com开头,不管后面跟的什么,都是访问www.baidu.com
}
扩展:
基本正则:{n,m} ():
扩展正则:{} ()
perl正则:\w
posix正则:[[:alpha:]] [[:num:]]
rewriet /(.) http://www.a.com/$1; ///把()里面东西粘过来
根据上面的案例:
访问www.a.com下面子页面,重定向至www.baidu.com/下相同的页面)
**rewrite ^/(.
) http://www.baidu.com/$1;**
火狐地址输入:www.a.com/aaaaa
地址栏改变:https://www.baidu.com/aaaaa

3.实现curl和火狐访问相同链接返回的页面不同
echo “woshishui” > /usr/local/nginx/html/test.html
mkdir /usr/local/nginx/html/firefox/
echo “woshifirefox” > /usr/local/nginx/html/firefox/test.html
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.a.com;
if ($http_user_agent ~ firefox){ ///~表示模糊匹配,不区分大小写
**rewrite /(.
) /firefox/$1;**
}
location / {
root html;
index test.php index.html index.htm;
}

h t t p u s e r a g e n t 的 值 为 客 户 端 信 息 " M o z i l l a / 5.0 ( W i n d o w s N T 10.0 ; W i n 64 ; x 64 ; r v : 72.0 ) G e c k o / 20100101 F i r e f o x / 72.0 " 也 可 以 根 据 操 作 系 统 统 来 设 置 : i f ( http_user_agent的值为客户端信息 "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0" 也可以根据操作系统统来设置: if ( httpuseragent"Mozilla/5.0(WindowsNT10.0;Win64;x64;rv:72.0)Gecko/20100101Firefox/72.0"if(http_user_agent ~* windows|android){

地址重写格式【总结】
rewrite 旧地址 新地址 [选项];
last 不再读其他rewrite
break 不再读其他语句,结束请求
redirect 临时重定向
permament 永久重定向

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值