day64:nginx模块之限制连接&状态监控&Location/用nginx+php跑项目/扩展应用节点
目录
nginx模块:限制连接 limit_conn
限制连接limit_conn主要用在下载,也就是可以限制同时下载的数量
- [root@oldboy-pythonedu mirror]# cat /etc/nginx/conf.d/mirror.oldboyedu.com.conf
- limit_conn_zone $binary_remote_addr zone=addr:10m; # 定义限制的key, 分配区域大小
- server {
- listen 80;
- server_name mirror.oldboyedu.com;
- charset utf8;
- limit_conn addr 1; # 调用区域限制,限制key只可以出现1次, 相当于限制来源客户端IP的连接数为1
- limit_conn_status 500; # 限制成功后,会返回500的错误状态码,默认返回503
- limit_rate_after 200m; # 全速下载200m资源
- limit_rate 300k; # 达到200m以后,限制300k的速度
- error_page 500 = @testerror; # 如果 出现500错误,则让其跳转到内部的 @testerror
- location @testerror { # 定义 @testerror, 返回具体的动作
- default_type text/html;
- return 200 '$remote_addr 你超过了最大连接限制, 请充值VIP解封!';
- }
- location / {
- root /code/mirror;
- autoindex on;
- autoindex_exact_size off;
- autoindex_localtime on;
- }
- }
nginx模块:状态监控 stub_status
- location = /status {
- stub_status;
- }
一共有7种状态:
Active connections: 2
server accepts handled requests
74 74 104
Reading: 0 Writing: 1 Waiting: 1
关于7个参数的解释
Active connections: 活跃的连接数
accepts: 接受的总TCP连接数
handled: 总处理的TCP连接数
requests: 总的 http 请求数
关于Reading,Writing,Waiting的理解
假设现在有两条船分别为C,S
C船需要S船的一个物品,C船需要S船的1个物品,那么此时C船就要给S船发送一个消息
1.S船收到这个信息就是reading
2.S船将物资发送给C船,这个时候就是writing
3.如果C船需要S船很多个物品,那么需要C船和S船建立起一个物资传送管道,不断的传送物资。这个管道建立起来的时候,就是waiting状态了。
nginx模块:Location
作用: 控制用户请求 uri 的具体路径
用法: location [ = | ~ | ~* | ^~ ] uri { … }
(多个location时会用上, 但多个location会出现优先级的问题)
1.Location优先级
匹配符 | 匹配规则 | 优先级 |
= | 精准匹配 | 1 |
^~ | 以某个字符串开头 | 2 |
~ | 区分大小写的正则匹配 | 3 |
~* | 不区分大小写的正则匹配 | 4 |
/ | 通用匹配,任何请求都会匹配到 | 5 |
当输入下面的URL时,Location会怎么匹配呢?
http://location.oldboyedu.com/index.html location /
http://location.oldboyedu.com/documents/1.html location /documents/
http://location.oldboyedu.com/images/1.gif location ^~ /images/
http://location.oldboyedu.com/documents/1.jpg location ~* \.(gif|jpg|jpeg)
2.Location具体如何使用
- server {
- listen 80;
- server_name location2.oldxu.com;
- # 通用匹配,任何请求都会匹配到
- location / {
- root html;
- index index.html;
- }
- # 精准匹配,必须请求的uri是/nginx_status
- location = /nginx_status {
- stub_status;
- }
- # 严格区分大小写,匹配以.php结尾的都走这个location
- location ~ \.php$ {
- default_type text/html;
- return 200 'php访问成功';
- }
- # 严格区分大小写,匹配以.jsp结尾的都走这个location
- location ~ \.jsp$ {
- default_type text/html;
- return 200 'jsp访问成功';
- }
- # 不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条location
- location ~* \.(jpg|gif|png|js|css)$ {
- return 403;
- }
- # 不区分大小写匹配
- location ~* \.(sql|bak|tgz|tar.gz|.git)$ {
- deny all;
- }
- }
用nginx+php跑wordpress项目
1.LNMP架构
L = Linux
N = Nginx
M ~= MySQL | Mariadb
P ~= PHP | Python
2.LNMP架构安装
1.安装PHP
- # 安装php:
- [root@oldboy-pythonedu ~]# rpm -e $(rpm -qa |grep php) #卸载php5版本
- [root@oldboy-pythonedu ~]# wget http://cdn.xuliangwei.com/php.zip
- [root@oldboy-pythonedu ~]# unzip php.zip
- [root@oldboy-pythonedu ~]# yum localinstall php/*.rpm -y
2.修改PHP进程运行的身份
- # 修改进程运行的身份
- [root@oldboy-pythonedu ~]# sed -i 's#user = apache#user = nginx#g' /etc/php-fpm.d/www.conf
- [root@oldboy-pythonedu ~]# sed -i 's#group = apache#group = nginx#g' /etc/php-fpm.d/www.conf
3.启动php-fpm
- # 启动php-fpm
- [root@oldboy-pythonedu ~]# systemctl enable php-fpm
- [root@oldboy-pythonedu ~]# systemctl start php-fpm
4.编辑index.php–>用来测试nginx+php能否正常运行
- [root@oldboy-pythonedu ~]# cat /code/index.php
- <?php
- phpinfo();
- ?>
5.编辑配置文件php.oldboyedu.com–>用来测试nginx+php能否正常运行
- # nginx+ php 检查:
- [root@oldboy-pythonedu ~]# cat /etc/nginx/conf.d/php.oldboyedu.com.conf
- server {
- listen 80;
- server_name php.oldboyedu.com;
- root /code;
- location / {
- index index.php;
- }
- location ~ \.php$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
- }
通过访问php.oldboyedu.com即可查看nginx+php是否成功
6.安装mysql
- [root@oldboy-pythonedu ~]# yum install mariadb mariadb-server -y
- [root@oldboy-pythonedu ~]# systemctl enable mariadb
- [root@oldboy-pythonedu ~]# systemctl start mariadb
- [root@oldboy-pythonedu ~]# mysqladmin password 'Oldxu.com123'
- [root@oldboy-pythonedu ~]# mysql -uroot -pOldxu.com123
- MariaDB [(none)]>
- MariaDB [(none)]> create database wordpress charset utf8;
7.编辑mysql.php–>用来测试php+mysql是否成功
- [root@oldboy-pythonedu ~]# cat /code/mysql.php
- <?php
- $servername = "localhost";
- $username = "root";
- $password = "Oldxu.com123";
- // 创建连接
- $conn = mysqli_connect($servername, $username, $password);
- // 检测连接
- if (!$conn) {
- die("Connection failed: " . mysqli_connect_error());
- }
- echo "php连接MySQL数据库成功";
- ?>
编辑完代码后,输入如下指令:
- [root@oldboy-pythonedu ~]# php /code/mysql.php
如果出现php连接MySQL数据库成功,则代表成功
3.部署WordPress
1.下载代码,存储至指定位置,变更权限
- [root@oldboy-pythonedu ~]# cd /code/
- [root@oldboy-pythonedu code]# wget https://cn.wordpress.org/latest-zh_CN.tar.gz
- [root@oldboy-pythonedu code]# tar xf latest-zh_CN.tar.gz
- [root@oldboy-pythonedu code]# chown -R nginx.nginx wordpress/
2.编写Nginx配置文件:blog.oldboyedu.com.conf
- [root@oldboy-pythonedu code]# cat /etc/nginx/conf.d/blog.oldboyedu.com.conf
- server {
- listen 80;
- server_name blog.oldboyedu.com;
- root /code/wordpress;
- location / {
- index index.php;
- }
- location ~ \.php$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
- }
3.检查语法,重启nginx服务
- [root@oldboy-pythonedu code]# nginx -t
- [root@oldboy-pythonedu code]# systemctl reload nginx
4.配置域名解析.访问浏览器.安装该产品
修改域名的路径:C:\Windows\System32\drivers\etc\hosts
改成如下格式即可:
用nginx+php跑edusoho项目
前面已经安装好php和mysql环境了,所以接下来直接跑项目就可以了
1.安装EduSoho
- mkdir /code
- cd /code
- rz # 上传文件
- tar xf edusoho-8.2.17.tar.gz
- #注意:我们的进程能够以什么方式去访问一个文件或目录,取决于进程所运行的用户身份对该文件有什么权限
- chown -R nginx.nginx edusoho
2.编写nginx配置文件:edusoho.oldboyedu.com.conf
- vim /etc/nginx/conf.d/edusoho.oldboyedu.conf
- server {
- listen 80;
- server_name edu.oldboyedu.com;
- root /code/edusoho/web;
- client_max_body_size 1024m; #允许上传视频大小限制
- client_body_buffer_size 100m; #缓冲区大小(太小会提示a client request body is buffered to a temporary)
- location / {
- index app.php;
- try_files $uri @rewriteapp;
- }
- location @rewriteapp {
- rewrite ^(.*)$ /app.php/$1 last;
- }
- location ~ ^/udisk {
- internal;
- root /code/edusoho/app/data/;
- }
- location ~ ^/(app|app_dev)\.php(/|$) {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_split_path_info ^(.+\.php)(/.*)$;
- include fastcgi_params;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- fastcgi_param HTTPS off;
- fastcgi_param HTTP_X-Sendfile-Type X-Accel-Redirect;
- fastcgi_param HTTP_X-Accel-Mapping /udisk=/code/edusoho/app/data/udisk;
- fastcgi_buffer_size 128k;
- fastcgi_buffers 8 128k;
- }
- location ~* \.(jpg|jpeg|gif|png|ico|swf)$ {
- expires 3y;
- access_log off;
- gzip off;
- }
- location ~* \.(css|js)$ {
- access_log off;
- expires 3y;
- }
- location ~ ^/files/.*\.(php|php5)$ {
- deny all;
- }
- location ~ \.php$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_split_path_info ^(.+\.php)(/.*)$;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- fastcgi_param HTTPS off;
- include fastcgi_params;
- }
- }
3.检查nginx语法,重启服务
- nginx -t
- systemctl restart nginx
4.修改php.ini 配置文件,调整解析器支持的最大上传限制
- vim /etc/php.ini
- upload_max_filesize = 1024M
- post_max_size = 1024M
- systemctl restart php-fpm
5.网站中的一些功能如何使用
用nginx+php跑kodcloud项目
1.nginx+php环境
…前面已经介绍了,这里不再多bb
2.下载kodcloud代码
- [root@oldboy-pythonedu ~]# cd /code
- [root@oldboy-pythonedu code]# wget http://static.kodcloud.com/update/download/kodbox.1.13.zip
- [root@oldboy-pythonedu code]# mkdir kodcloud
- [root@oldboy-pythonedu code]# unzip kodbox.1.13.zip -d kodcloud/
- [root@oldboy-pythonedu code]# chown -R nginx.nginx /code/kodcloud/
3.修改nginx配置文件:kod.oldboyedu.com.conf
- [root@oldboy-pythonedu code]# cat /etc/nginx/conf.d/kod.oldboyedu.com.conf
- server {
- listen 80;
- server_name kod.oldboyedu.com;
- root /code/kodcloud;
- location / {
- index index.php;
- }
- location ~ \.php$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
- }
4.检查nginx语法–重启nginx服务–域名解析
….这几个指令前面有介绍,这里不在bb
扩展一台应用节点
扩展应用节点,说白了,就是再建立一台虚拟机。。
1.克隆一台全新的虚拟机,并修改IP地址
- sed -i 's#old#new#g' /etc/sysconfig/network-scripts/ifcfg-ens32 # 将10.0.0.200替换成10.0.0.201
- # old: 旧的IP尾号
- # new: 新的IP尾号
- [root@oldboy-pythonedu ~]# hostnamectl set-hostname node2 # 修改主机名称
2.安装各种需要的环境
- # 1.安装Nginx PHP环境
- [root@node2 ~]# yum install vim net-tools unzip wget lrzsz -y # 安装基础工具
- [root@node2 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo # 配置epel源
- # 2.安装Nginx
- [root@node2 ~]# yum install nginx -y
- # 3.安装PHP
- [root@node2 ~]# unzip php.zip
- [root@node2 ~]# yum localinstall php/*.rpm -y
- # 4.拷贝Nginx配置 scp
- [root@node2 ~]# scp root@10.0.0.200:/etc/nginx/nginx.conf /etc/nginx/nginx.conf
- [root@node2 ~]# scp -r root@10.0.0.200:/etc/nginx/conf.d/*.conf /etc/nginx/conf.d/
- # 5.拷贝php配置
- [root@node2 ~]# scp root@10.0.0.200:/etc/php.ini /etc/php.ini
- [root@node2 ~]# scp root@10.0.0.200:/etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf
- # 6.关闭防火墙
- [root@node2 ~]# systemctl disable firewalld
- [root@node2 ~]# systemctl stop firewalld
- [root@node2 ~]# setenforce 0
- [root@node2 ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
- # 7.拷贝所有代码
- [root@node2 ~]# scp -rp root@10.0.0.200:/code /
- [root@node2 ~]# chown -R nginx.nginx /code/
- # 8.启动服务
- [root@node2 ~]# systemctl enable nginx php-fpm
- [root@node2 ~]# systemctl start nginx php-fpm
拆分数据库到独立的服务器
1.准备基础环境, 修改IP地址,修改主机名称,关闭防火墙
- sed -i 's#201#202#g' /etc/sysconfig/network-scripts/ifcfg-ens32
- systemctl restart network
- hostnamectl set-hostname node-mysql
- systemctl stop firewalld
- systemctl disable firewalld
- setenforce 0
- sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
- yum install vim net-tools unzip wget lrzsz -y
- wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
2.安装Mariadb
- [root@oldboy-pythonedu ~]# yum install mariadb mariadb-server -y
- [root@oldboy-pythonedu ~]# systemctl enable mariadb
- [root@oldboy-pythonedu ~]# systemctl start mariadb
- [root@oldboy-pythonedu ~]# mysql
- MariaDB [(none)]> grant all privileges on *.* to 'all'@'%' identified by 'Oldxu.com123';
- MariaDB [(none)]>
3.将10.0.0.200数据库中的数据库备份下来,然后恢复至 10.0.0.202 主机的MySQL上
- [root@oldboy-pythonedu ~]# mysqldump -uroot -pOldxu.com123 -B wordpress edusoho > bak.sql
- [root@oldboy-pythonedu ~]# scp bak.sql root@10.0.0.202:~
4.在10.0.0.202的数据库上恢复数据
- [root@oldboy-pythonedu ~]# mysql < bak.sql
5.在10.0.0.201上修改连接数据库的地址(所有应用节点都需要操作)
- # WordPress:
- [root@node2 ~]# vim /code/wordpress/wp-config.php
- define( 'DB_NAME', 'wordpress' );
- /** MySQL数据库用户名 */
- define( 'DB_USER', 'all' );
- /** MySQL数据库密码 */
- define( 'DB_PASSWORD', 'Oldxu.com123' );
- /** MySQL主机 */
- define( 'DB_HOST', '10.0.0.202' );
- # edusohu:
- [root@node2 ~]# vim /code/edusoho/app/config/parameters.yml
- parameters:
- database_driver: pdo_mysql
- database_host: 10.0.0.202
- database_port: 3306
- database_name: edusoho
- database_user: all
- database_password: 'Oldxu.com123'
6.edusoho存在缓存,需要清除一下
- [root@node2 ~]# rm -rf /code/edusoho/app/cache/*
Tip:目前有三台主机,他们的关系如下所示,后面还会再加上一台。