1.nginx模块:限制连接 limit_conn

2.nginx模块:状态监控 stub_status

3.nginx模块:Location

4.用nginx+php跑wordpress项目

5.用nginx+php跑edusoho项目

6.用nginx+php跑kodcloud项目

7.扩展一台应用节点

8.拆分数据库到独立的服务器

限制连接limit_conn主要用在下载,也就是可以限制同时下载的数量

  1. [root@oldboy-pythonedu mirror]# cat /etc/nginx/conf.d/mirror.oldboyedu.com.conf
  2. limit_conn_zone $binary_remote_addr zone=addr:10m; # 定义限制的key, 分配区域大小
  3. server {
  4. listen 80;
  5. server_name mirror.oldboyedu.com;
  6. charset utf8;
  7. limit_conn addr 1; # 调用区域限制,限制key只可以出现1次, 相当于限制来源客户端IP的连接数为1
  8. limit_conn_status 500; # 限制成功后,会返回500的错误状态码,默认返回503
  9. limit_rate_after 200m; # 全速下载200m资源
  10. limit_rate 300k; # 达到200m以后,限制300k的速度
  11. error_page 500 = @testerror; # 如果 出现500错误,则让其跳转到内部的 @testerror
  12. location @testerror { # 定义 @testerror, 返回具体的动作
  13. default_type text/html;
  14. return 200 '$remote_addr 你超过了最大连接限制, 请充值VIP解封!';
  15. }
  16. location / {
  17. root /code/mirror;
  18. autoindex on;
  19. autoindex_exact_size off;
  20. autoindex_localtime on;
  21. }
  22. }
  1. location = /status {
  2. stub_status;
  3. }

一共有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状态了。

作用: 控制用户请求 uri 的具体路径

用法: location [ = | ~ | ~* | ^~ ] uri { … }

(多个location时会用上, 但多个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)

  1. server {
  2. listen 80;
  3. server_name location2.oldxu.com;
  4. # 通用匹配,任何请求都会匹配到
  5. location / {
  6. root html;
  7. index index.html;
  8. }
  9. # 精准匹配,必须请求的uri是/nginx_status
  10. location = /nginx_status {
  11. stub_status;
  12. }
  13. # 严格区分大小写,匹配以.php结尾的都走这个location
  14. location ~ \.php$ {
  15. default_type text/html;
  16. return 200 'php访问成功';
  17. }
  18. # 严格区分大小写,匹配以.jsp结尾的都走这个location
  19. location ~ \.jsp$ {
  20. default_type text/html;
  21. return 200 'jsp访问成功';
  22. }
  23. # 不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条location
  24. location ~* \.(jpg|gif|png|js|css)$ {
  25. return 403;
  26. }
  27. # 不区分大小写匹配
  28. location ~* \.(sql|bak|tgz|tar.gz|.git)$ {
  29. deny all;
  30. }
  31. }

L = Linux

N = Nginx

M ~= MySQL | Mariadb

P ~= PHP | Python

  1. # 安装php:
  2. [root@oldboy-pythonedu ~]# rpm -e $(rpm -qa |grep php) #卸载php5版本
  3. [root@oldboy-pythonedu ~]# wget http://cdn.xuliangwei.com/php.zip
  4. [root@oldboy-pythonedu ~]# unzip php.zip
  5. [root@oldboy-pythonedu ~]# yum localinstall php/*.rpm -y
  1. # 修改进程运行的身份
  2. [root@oldboy-pythonedu ~]# sed -i 's#user = apache#user = nginx#g' /etc/php-fpm.d/www.conf
  3. [root@oldboy-pythonedu ~]# sed -i 's#group = apache#group = nginx#g' /etc/php-fpm.d/www.conf
  1. # 启动php-fpm
  2. [root@oldboy-pythonedu ~]# systemctl enable php-fpm
  3. [root@oldboy-pythonedu ~]# systemctl start php-fpm
  1. [root@oldboy-pythonedu ~]# cat /code/index.php
  2. <?php
  3. phpinfo();
  4. ?>
  1. # nginx+ php 检查:
  2. [root@oldboy-pythonedu ~]# cat /etc/nginx/conf.d/php.oldboyedu.com.conf
  3. server {
  4. listen 80;
  5. server_name php.oldboyedu.com;
  6. root /code;
  7. location / {
  8. index index.php;
  9. }
  10. location ~ \.php$ {
  11. fastcgi_pass 127.0.0.1:9000;
  12. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  13. include fastcgi_params;
  14. }
  15. }

通过访问php.oldboyedu.com即可查看nginx+php是否成功

  1. [root@oldboy-pythonedu ~]# yum install mariadb mariadb-server -y
  2. [root@oldboy-pythonedu ~]# systemctl enable mariadb
  3. [root@oldboy-pythonedu ~]# systemctl start mariadb
  4. [root@oldboy-pythonedu ~]# mysqladmin password 'Oldxu.com123'
  5. [root@oldboy-pythonedu ~]# mysql -uroot -pOldxu.com123
  6. MariaDB [(none)]>
  7. MariaDB [(none)]> create database wordpress charset utf8;
  1. [root@oldboy-pythonedu ~]# cat /code/mysql.php
  2. <?php
  3. $servername = "localhost";
  4. $username = "root";
  5. $password = "Oldxu.com123";
  6. // 创建连接
  7. $conn = mysqli_connect($servername, $username, $password);
  8. // 检测连接
  9. if (!$conn) {
  10. die("Connection failed: " . mysqli_connect_error());
  11. }
  12. echo "php连接MySQL数据库成功";
  13. ?>

编辑完代码后,输入如下指令:

  1. [root@oldboy-pythonedu ~]# php /code/mysql.php

如果出现php连接MySQL数据库成功,则代表成功

  1. [root@oldboy-pythonedu ~]# cd /code/
  2. [root@oldboy-pythonedu code]# wget https://cn.wordpress.org/latest-zh_CN.tar.gz
  3. [root@oldboy-pythonedu code]# tar xf latest-zh_CN.tar.gz
  4. [root@oldboy-pythonedu code]# chown -R nginx.nginx wordpress/
  1. [root@oldboy-pythonedu code]# cat /etc/nginx/conf.d/blog.oldboyedu.com.conf
  2. server {
  3. listen 80;
  4. server_name blog.oldboyedu.com;
  5. root /code/wordpress;
  6. location / {
  7. index index.php;
  8. }
  9. location ~ \.php$ {
  10. fastcgi_pass 127.0.0.1:9000;
  11. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  12. include fastcgi_params;
  13. }
  14. }
  1. [root@oldboy-pythonedu code]# nginx -t
  2. [root@oldboy-pythonedu code]# systemctl reload nginx

修改域名的路径:C:\Windows\System32\drivers\etc\hosts

改成如下格式即可:

前面已经安装好php和mysql环境了,所以接下来直接跑项目就可以了

  1. mkdir /code
  2. cd /code
  3. rz # 上传文件
  4. tar xf edusoho-8.2.17.tar.gz
  5. #注意:我们的进程能够以什么方式去访问一个文件或目录,取决于进程所运行的用户身份对该文件有什么权限
  6. chown -R nginx.nginx edusoho
  1. vim /etc/nginx/conf.d/edusoho.oldboyedu.conf
  2. server {
  3. listen 80;
  4. server_name edu.oldboyedu.com;
  5. root /code/edusoho/web;
  6. client_max_body_size 1024m; #允许上传视频大小限制
  7. client_body_buffer_size 100m; #缓冲区大小(太小会提示a client request body is buffered to a temporary)
  8. location / {
  9. index app.php;
  10. try_files $uri @rewriteapp;
  11. }
  12. location @rewriteapp {
  13. rewrite ^(.*)$ /app.php/$1 last;
  14. }
  15. location ~ ^/udisk {
  16. internal;
  17. root /code/edusoho/app/data/;
  18. }
  19. location ~ ^/(app|app_dev)\.php(/|$) {
  20. fastcgi_pass 127.0.0.1:9000;
  21. fastcgi_split_path_info ^(.+\.php)(/.*)$;
  22. include fastcgi_params;
  23. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  24. fastcgi_param HTTPS off;
  25. fastcgi_param HTTP_X-Sendfile-Type X-Accel-Redirect;
  26. fastcgi_param HTTP_X-Accel-Mapping /udisk=/code/edusoho/app/data/udisk;
  27. fastcgi_buffer_size 128k;
  28. fastcgi_buffers 8 128k;
  29. }
  30. location ~* \.(jpg|jpeg|gif|png|ico|swf)$ {
  31. expires 3y;
  32. access_log off;
  33. gzip off;
  34. }
  35. location ~* \.(css|js)$ {
  36. access_log off;
  37. expires 3y;
  38. }
  39. location ~ ^/files/.*\.(php|php5)$ {
  40. deny all;
  41. }
  42. location ~ \.php$ {
  43. fastcgi_pass 127.0.0.1:9000;
  44. fastcgi_split_path_info ^(.+\.php)(/.*)$;
  45. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  46. fastcgi_param HTTPS off;
  47. include fastcgi_params;
  48. }
  49. }
  1. nginx -t
  2. systemctl restart nginx
  1. vim /etc/php.ini
  2. upload_max_filesize = 1024M
  3. post_max_size = 1024M
  4. systemctl restart php-fpm

…前面已经介绍了,这里不再多bb

  1. [root@oldboy-pythonedu ~]# cd /code
  2. [root@oldboy-pythonedu code]# wget http://static.kodcloud.com/update/download/kodbox.1.13.zip
  3. [root@oldboy-pythonedu code]# mkdir kodcloud
  4. [root@oldboy-pythonedu code]# unzip kodbox.1.13.zip -d kodcloud/
  5. [root@oldboy-pythonedu code]# chown -R nginx.nginx /code/kodcloud/
  1. [root@oldboy-pythonedu code]# cat /etc/nginx/conf.d/kod.oldboyedu.com.conf
  2. server {
  3. listen 80;
  4. server_name kod.oldboyedu.com;
  5. root /code/kodcloud;
  6. location / {
  7. index index.php;
  8. }
  9. location ~ \.php$ {
  10. fastcgi_pass 127.0.0.1:9000;
  11. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  12. include fastcgi_params;
  13. }
  14. }

….这几个指令前面有介绍,这里不在bb

扩展应用节点,说白了,就是再建立一台虚拟机。。

  1. sed -i 's#old#new#g' /etc/sysconfig/network-scripts/ifcfg-ens32 # 将10.0.0.200替换成10.0.0.201
  2.  
  3. # old: 旧的IP尾号
  4. # new: 新的IP尾号
  5. [root@oldboy-pythonedu ~]# hostnamectl set-hostname node2 # 修改主机名称
  1. # 1.安装Nginx PHP环境
  2. [root@node2 ~]# yum install vim net-tools unzip wget lrzsz -y # 安装基础工具
  3. [root@node2 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo # 配置epel源
  4.  
  5.  
  6. # 2.安装Nginx
  7. [root@node2 ~]# yum install nginx -y
  8.  
  9. # 3.安装PHP
  10. [root@node2 ~]# unzip php.zip
  11. [root@node2 ~]# yum localinstall php/*.rpm -y
  12.  
  13.  
  14. # 4.拷贝Nginx配置 scp
  15. [root@node2 ~]# scp root@10.0.0.200:/etc/nginx/nginx.conf /etc/nginx/nginx.conf
  16. [root@node2 ~]# scp -r root@10.0.0.200:/etc/nginx/conf.d/*.conf /etc/nginx/conf.d/
  17.  
  18. # 5.拷贝php配置
  19. [root@node2 ~]# scp root@10.0.0.200:/etc/php.ini /etc/php.ini
  20. [root@node2 ~]# scp root@10.0.0.200:/etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf
  21.  
  22. # 6.关闭防火墙
  23. [root@node2 ~]# systemctl disable firewalld
  24. [root@node2 ~]# systemctl stop firewalld
  25. [root@node2 ~]# setenforce 0
  26. [root@node2 ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
  27.  
  28.  
  29. # 7.拷贝所有代码
  30. [root@node2 ~]# scp -rp root@10.0.0.200:/code /
  31. [root@node2 ~]# chown -R nginx.nginx /code/
  32. # 8.启动服务
  33. [root@node2 ~]# systemctl enable nginx php-fpm
  34. [root@node2 ~]# systemctl start nginx php-fpm
  1. sed -i 's#201#202#g' /etc/sysconfig/network-scripts/ifcfg-ens32
  2. systemctl restart network
  3. hostnamectl set-hostname node-mysql
  4. systemctl stop firewalld
  5. systemctl disable firewalld
  6. setenforce 0
  7. sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
  8. yum install vim net-tools unzip wget lrzsz -y
  9. wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  1. [root@oldboy-pythonedu ~]# yum install mariadb mariadb-server -y
  2. [root@oldboy-pythonedu ~]# systemctl enable mariadb
  3. [root@oldboy-pythonedu ~]# systemctl start mariadb
  4. [root@oldboy-pythonedu ~]# mysql
  5. MariaDB [(none)]> grant all privileges on *.* to 'all'@'%' identified by 'Oldxu.com123';
  6. MariaDB [(none)]>
  1. [root@oldboy-pythonedu ~]# mysqldump -uroot -pOldxu.com123 -B wordpress edusoho > bak.sql
  2. [root@oldboy-pythonedu ~]# scp bak.sql root@10.0.0.202:~
  1. [root@oldboy-pythonedu ~]# mysql < bak.sql
  1. # WordPress:
  2. [root@node2 ~]# vim /code/wordpress/wp-config.php
  3. define( 'DB_NAME', 'wordpress' );
  4. /** MySQL数据库用户名 */
  5. define( 'DB_USER', 'all' );
  6. /** MySQL数据库密码 */
  7. define( 'DB_PASSWORD', 'Oldxu.com123' );
  8. /** MySQL主机 */
  9. define( 'DB_HOST', '10.0.0.202' );
  10. # edusohu:
  11. [root@node2 ~]# vim /code/edusoho/app/config/parameters.yml
  12. parameters:
  13. database_driver: pdo_mysql
  14. database_host: 10.0.0.202
  15. database_port: 3306
  16. database_name: edusoho
  17. database_user: all
  18. database_password: 'Oldxu.com123'
  1. [root@node2 ~]# rm -rf /code/edusoho/app/cache/*

Tip:目前有三台主机,他们的关系如下所示,后面还会再加上一台。

版权声明:本文为libolun原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/libolun/p/13795745.html