首页
友链
统计
留言
更多
直播
壁纸
推荐
我的毛线
院长科技
Search
1
本站官方群:894703859------|诚邀各位大佬的入驻!
418 阅读
2
pxe 自动化安装系统
348 阅读
3
新款螺旋帽子编织#夏凉帽#合股线夏凉帽编织
308 阅读
4
10 个Linux Awk文本处理经典案例
306 阅读
5
软件安装
293 阅读
Linux
yaml
iptables
shell
ansible
ssl
命令
文件管理
用户权限
综合集群架构
三剑客
awk
sed
自动化
pxe
编织
编织视频
监控
prometheus
go
go占位符
vue
vue基础
vue项目
web
Nginx
html
vscode
html标签
html表格
css基础
css定位
css精灵图
code
html5
项目
js
jQuery
面向对象
kubernetes
k8s命令
k8s
k8s搭建
database
clickhouse
常用工具
微软
登录
/
注册
Search
标签搜索
基础
js
Nginx
css
webapi
jQuery
面向对象
command
项目
ansible
用户权限
go
html
文件管理
命令
综合集群架构
k8s
pxe
awk
vscode
JustDoIt
累计撰写
112
篇文章
累计收到
4
条评论
首页
栏目
Linux
yaml
iptables
shell
ansible
ssl
命令
文件管理
用户权限
综合集群架构
三剑客
awk
sed
自动化
pxe
编织
编织视频
监控
prometheus
go
go占位符
vue
vue基础
vue项目
web
Nginx
html
vscode
html标签
html表格
css基础
css定位
css精灵图
code
html5
项目
js
jQuery
面向对象
kubernetes
k8s命令
k8s
k8s搭建
database
clickhouse
常用工具
微软
页面
友链
统计
留言
直播
壁纸
推荐
我的毛线
院长科技
搜索到
7
篇与
的结果
2023-11-03
Nginx负载均衡
1.Nginx代理服务代理我们往往并不陌生, 该服务我们常常用到如(代理租房、代理收货等等)那么在互联网请求里面, 客户端无法直接向服务端发起请求, 那么就需要用到代理服务, 来实现客户端和服务通信Nginx作为代理服务可以实现很多的协议代理, 我们主要以http代理为主正向代理(内部上网) 客户端<-->代理->服务端反向代理 客户端->代理<-->服务端正向与反向代理的区别区别在于代理的对象不一样正向代理代理的对象是客户端反向代理代理的对象是服务端1.1 Nginx反向代理配置语法1.Nginx代理配置语法Syntax: proxy_pass URL; Default: — Context: location, if in location, limit_except http: http: http: 2.url跳转修改返回Location[不常用]参考URLSyntax: proxy_redirect default; proxy_redirect off;proxy_redirect redirect replacement; Default: proxy_redirect default; Context: http, server, location 3.添加发往后端服务器的请求头信息, 如图Syntax: proxy_set_header field value; Default: proxy_set_header Host $proxy_host; proxy_set_header Connection close; Context: http, server, location proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 4.代理到后端的TCP连接、响应、返回等超时时间, 如图 Syntax: proxy_connect_timeout time; Default: proxy_connect_timeout 60s; Context: http, server, location Syntax: proxy_read_timeout time; Default: proxy_read_timeout 60s; Context: http, server, location Syntax: proxy_send_timeout time; Default: proxy_send_timeout 60s; Context: http, server, location 5.proxy_buffer代理缓冲区, 如图 Syntax: proxy_buffering on | off; Default: proxy_buffering on; Context: http, server, location Syntax: proxy_buffer_size size; Default: proxy_buffer_size 4k|8k; Context: http, server, location Syntax: proxy_buffers number size; Default: proxy_buffers 8 4k|8k; Context: http, server, location 6.Proxy代理网站常用优化配置如下,将配置写入新文件,调用时使用include引用即可[root@Nginx ~] proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 30; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffering on; proxy_buffer_size 32k; proxy_buffers 4 128k; 7.代理配置location时调用, 方便后续多个Location重复使用location / { proxy_pass http://127.0.0.1:8080; include proxy_params; } 1.2 Nginx反向代理配置场景Nginx反向代理配置实例1.环境准备 角色 外网IP(NAT) 内网IP(LAN) 主机名 Proxy eth0:10.0.0.5 eth1:172.16.1.5 lb01 web01 eth0:10.0.0.7 eth1:172.16.1.7 web01 2.web01服务器, 配置一个网站,监听在8080,仅运行172网段能访问[root@web01 ~] [root@web01 conf.d] server { listen 8080; server_name 172.16.1.7; location / { root /code_8080; index index.html; deny 10.0.0.0/24; allow all; } } [root@web01 conf.d] [root@web01 conf.d] [root@web01 conf.d] 2.proxy代理服务器, 配置监听在80,让10.0.0.1客户端,能够通过代理访问到后端的172.16.1.7网站[root@lb01 ~] [root@lb01 conf.d] server { listen 80; server_name nginx.oldboy.com; location / { proxy_pass http://172.16.1.7:8080; include proxy_params; } } [root@lb01 conf.d] [root@lb01 conf.d] 2.Nginx负载均衡Web服务器,直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台WEB服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,实现负载的分发。那么会大大提升系统的吞吐率、请求性能、高容灾Nginx是一个典型的SLB(Server Load Balance)网络负载均衡器2.1 Nginx负载均衡按层划分负载均衡按层划分应用场景: 四层负载均衡负载均衡按层划分应用场景: 七层负载均衡, Nginx最常用2.2 Nginx负载均衡配置场景Nginx实现负载均衡需要用到proxy_pass代理模块配置.Nginx负载均衡是将客户端请求代理转发至一组upstream虚拟服务池Nginx upstream虚拟配置语法Syntax: upstream name { ... } Default: - Context: http upstream backend { server backend1.example.com weight=5; server backend2.example.com:8080; server unix:/tmp/backend3; server backup1.example.com:8080 backup; } server { location / { proxy_pass http: } } 0.环境规划 角色 外网IP(NAT) 内网IP(LAN) 主机名 LB01 eth0:10.0.0.5 eth1:172.16.1.5 lb01 web01 eth0:10.0.0.7 eth1:172.16.1.7 web01 web02 eth0:10.0.0.8 eth1:172.16.1.8 web02 1.Web01服务器上配置nginx, 并创建对应html文件[root@web01 ~] [root@web01 conf.d] server { listen 80; server_name node.oldboy.com; location / { root /node; index index.html; } } [root@web01 conf.d] [root@web01 conf.d] [root@web01 conf.d] 2.Web02服务器上配置nginx, 并创建对应html文件[root@web02 ~] [root@web02 conf.d] server { listen 80; server_name node.oldboy.com; location / { root /node; index index.html; } } [root@web02 conf.d] [root@web02 conf.d] [root@web02 conf.d] 3.配置Nginx负载均衡[root@lb01 ~] [root@lb01 conf.d] upstream node { server 172.16.1.7:80; server 172.16.1.8:80; } server { listen 80; server_name node.oldboy.com; location / { proxy_pass http://node; include proxy_params; } } [root@lb01 conf.d] 4.准备Nginx负载均衡调度使用的proxy_params[root@Nginx ~] proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 30; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffering on; proxy_buffer_size 32k; proxy_buffers 4 128k; 5.使用浏览器访问node.oldboy.com, 然后不断刷新测试2.3 Nginx负载均衡后端状态后端Web服务器在前端Nginx负载均衡调度中的状态 状态 概述 down 当前的server暂时不参与负载均衡 backup 预留的备份服务器 max_fails 允许请求失败的次数 fail_timeout 经过max_fails失败后, 服务暂停时间 max_conns 限制最大的接收连接数 1.测试down状态, 测试该Server不参与负载均衡的调度upstream load_pass { server 10.0.0.7:80 down; } 2.测试backup以及down状态upstream load_pass { server 10.0.0.7:80 down; server 10.0.0.8:80 backup; server 10.0.0.9:80 max_fails=1 fail_timeout=10s; } location / { proxy_pass http://load_pass; include proxy_params; } 3.测试max_fails失败次数和fail_timeout多少时间内失败多少次则标记downupstream load_pass { server 10.0.0.7:80; server 10.0.0.8:80 max_fails=2 fail_timeout=10s; } 4.测试max_conns最大TCP连接数upstream load_pass { server 10.0.0.7:80; server 10.0.0.8:80 max_conns=1; } 2.4 Nginx负载均衡调度算法 调度算法 概述 轮询 按时间顺序逐一分配到不同的后端服务器(默认) weight 加权轮询,weight值越大,分配到的访问几率越高 ip_hash 每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器 url_hash 按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器 least_conn 最少链接数,那个机器链接数少就分发 1.Nginx负载均衡[wrr]轮询具体配置upstream load_pass { server 10.0.0.7:80; server 10.0.0.8:80; } 2.Nginx负载均衡[weight]权重轮询具体配置upstream load_pass { server 10.0.0.7:80 weight=5; server 10.0.0.8:80; } 3.Nginx负载均衡ip_hash具体配置, 不能和weight一起使用。//如果客户端都走相同代理, 会导致某一台服务器连接过多 upstream load_pass { ip_hash; server 10.0.0.7:80 weight=5; server 10.0.0.8:80; } 2.5 Nginx负载均衡TCP实践配置Nginx4层负载均衡实现如下需求1.通过访问负载均衡的5555端口,实际是后端的web01的22端口在提供服务。2.通过访问负载均衡的6666端口,实际是后端的mysql的3306端口在提供服务。1.Nginx四层负载均衡配置语法stream { upstream backend { hash $remote_addr consistent; server backend1.example.com:12345 weight=5; server 127.0.0.1:12345 max_fails=3 fail_timeout=30s; server unix:/tmp/backend3; } server { listen 12345; proxy_connect_timeout 1s; proxy_timeout 3s; proxy_pass backend; } } 2.Nginx四层负载均衡实战[root@lb01 ~] [root@lb01 ~] include /etc/nginx/conf.c/*.conf; [root@lb01 ~] [root@lb01 conf.c] stream { upstream ssh { server 172.16.1.7:22; } upstream mysql { server 172.16.1.51:3306; } server { listen 5555; proxy_connect_timeout 1s; proxy_timeout 300s; proxy_pass ssh; } server { listen 6666; proxy_connect_timeout 1s; proxy_timeout 300s; proxy_pass mysql; } } [root@lb01 conf.c] 3.Nginx四层负载均衡应用场景3.Nginx动静分离动静分离, 通过中间件将动态请求和静态请求进行分离, 分离资源, 减少不必要的请求消耗, 减少请求延时。好处: 动静分离后, 即使动态服务不可用, 但静态资源不会受到影响通过中间件将动态请求和静态请求分离3.1 Nginx动静分离应用案例0.环境准备 系统 服务 服务 地址 CentOS7.5 负载均衡 Nginx Proxy 10.0.0.5 CentOS7.5 静态资源 Nginx Static 10.0.0.7 CentOS7.5 动态资源 Tomcat Server 10.0.0.8 1.在10.0.0.7服务器上配置静态资源[root@web01 conf.d] server{ listen 80; server_name ds.oldboy.com; root /soft/code; index index.html; location ~* .*\.(png|jpg|gif)$ { root /soft/code/images; } } [root@web01 ~] [root@web01 ~] [root@web01 ~] 2.在10.0.0.8服务器上配置动态资源[root@web01 ~] [root@web01 ~] [root@web01 ~] [root@web01 ~] <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <HTML> <HEAD> <TITLE>JSP Test Page</TITLE> </HEAD> <BODY> <% Random rand = new Random(); out.println("<h1>Random number:</h1>"); out.println(rand.nextInt(99)+100); %> </BODY> </HTML> 3.在负载均衡10.0.0.5上配置调度, 实现访问jsp和pngroot@lb01 conf.d]# cat ds_proxy.conf upstream static { server 10.0.0.7:80; } upstream java { server 10.0.0.8:8080; } server { listen 80; server_name ds.oldboy.com; location / { root /soft/code; index index.html; } location ~ .*\.(png|jpg|gif)$ { proxy_pass http: include proxy_params; } location ~ .*\.jsp$ { proxy_pass http: include proxy_params; } } [root@lb01 conf.d]# systemctl restart nginx 4.通过负载测试访问静态资源5.通过负载测试访问动态资源6.在负载均衡10.0.0.5上整合动态和静态资源的html文件[root@lb01 ~]# mkdir /soft/code -p [root@lb01 ~]# cat /soft/code/index.html <html lang="en"> <head> <meta charset="UTF-8" /> <title>测试ajax和跨域访问</title> <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script> </head> <script type="text/javascript"> $(document).ready(function(){ $.ajax({ type: "GET", url: "http://ds.oldboy.com/java_test.jsp", success: function(data) { $("#get_data").html(data) }, error: function() { alert("fail!!,请刷新再试!"); } }); }); </script> <body> <h1>测试动静分离</h1> <img src="http://ds.oldboy.com/nginx.png"> <div id="get_data"></div> </body> </html> 7.测试动态和静态资源是否能正常加载在一个html文件中8.当使用systemctl stop nginx停止Nginx后, 会发现静态内容无法访问, 动态内容依旧运行正常9.当使用systemctl stop tomcat停止tomcat后, 静态内容依旧能正常访问, 动态内容将不会被请求到3.2 Nginx通过来源设备拆分2.根据不同的浏览器, 以及不同的手机, 访问的效果都将不一样。 http { ... upstream firefox { server 172.31.57.133:80; } upstream chrome { server 172.31.57.133:8080; } upstream iphone { server 172.31.57.134:8080; } upstream android { server 172.31.57.134:8081; } upstream default { server 172.31.57.134:80; } ... } server { listen 80; server_name www.xuliangwei.com; location / { if ($http_user_agent ~* "Safari"){ proxy_pass http: } if ($http_user_agent ~* "Firefox"){ proxy_pass http: } if ($http_user_agent ~* "Chrome"){ proxy_pass http: } if ($http_user_agent ~* "iphone"){ proxy_pass http: } if ($http_user_agent ~* "android"){ proxy_pass http: } proxy_pass http: include proxy.conf; } } } 3.根据访问不同目录, 代理不同的服务器 upstream static_pools { server 10.0.0.9:80 weight=1; } upstream upload_pools { server 10.0.0.10:80 weight=1; } upstream default_pools { server 10.0.0.9:8080 weight=1; } server { listen 80; server_name www.xuliangwei.com; #url: http: location / { proxy_pass http: include proxy.conf; } #url: http: location /static/ { proxy_pass http: include proxy.conf; } #url: http: location /upload/ { proxy_pass http: include proxy.conf; } } if ($request_uri ~* "^/static/(.*)$") { proxy_pass http: } if ($request_uri ~* "^/upload/(.*)$") { proxy_pass http: } location / { proxy_pass http: include proxy.conf; } 转 xuliangwei.com
2023年11月03日
18 阅读
0 评论
0 点赞
2023-11-03
LNMP架构概述
1.LNMP架构概述LNMP就是Linux+Nginx+MySQL+PHP,Linux作为服务器的操作系统,Nginx作为Web服务器、PHP作为解析动态脚本语言、MySQL即为数据库。Linux作为服务器的操作系统。Nginx作为WebServer服务器。PHP 作为动态解析服务(php)。MySQL作为后端存储数据库服务。Nginx服务本身不能处理PHP的请求,那么当用户发起PHP动态请求, Nginx又是如何进行处理的。用户-->http协议-->Nginx-->fastcgi协议-->php-fpm注意: fatcgi是nginx连接php-fpm之间的协议。Nginx与Fast-CGI详细工作流程如下:1.用户通过http协议发起请求,请求会先抵达LNMP架构中的Nginx2.Nginx会根据用户的请求进行判断,这个判断是有Location进行完成3.判断用户请求的是静态页面,Nginx直接进行处理4.判断用户请求的是动态页面,Nginx会将该请求交给fastcgi协议下发5.fastgi会将请求交给php-fpm管理进程, php-fpm管理进程接收到后会调用具体的工作线程warrap6.warrap线程会调用php进行解析,如果只是解析代码php直接返回7.如果有查询数据库操作,则由php连接数据库(用户 密码 IP)然后发起查询的操作8.最终数据由mysql->php->php-fpm->fastcgi->nginx->http->user2.安装LNMP架构1) 使用官方仓库安装Nginx[root@nginx ~] [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1 2) 安装Nginx 并启动Nginx 加入开机自启[root@nginx ~] [root@nginx ~] [root@nginx ~] 3) 使用第三方扩展源安装php7.1[root@nginx ~] [root@nginx ~] [root@nginx ~] [root@nginx ~] 3) 配置php-fpm 启动php-fpm 并加入开机自启[root@nginx ~] [root@nginx ~] [root@nginx ~] [root@nginx ~] 4) 使用MySQL官方仓库安装MySQL5.7数据库[root@nginx ~] [root@nginx ~] 5) 启动数据库, 并加入开机自动[root@nginx ~] [root@nginx ~] 6) mysql5.7默认配置了密码, 需要提取temporary password中的密码, 登陆对应数据库[root@nginx ~]# grep "temporary password" /var/log/mysqld.log [root@nginx ~]# mysql -uroot -p$(awk '/temporary password/{print $NF}' /var/log/mysqld.log) 7) 重新初始化 MySQL 的密码mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Bgx123.com'; 2.配置LNMP架构1) 为了验证Nginx能否正常解析php动态请求, 需配置如下locaiton[root@nginx ~] server { server_name www.bgx.com; listen 80; root /code; index index.php index.html; location ~ \.php$ { root /code; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } 2) 新增.php后缀的文件名, 使用phpinfo函数测试php能否正常解析[root@nginx ~] <?php phpinfo(); ?> 3) 验证php是否能正常连接mysql数据库服务[root@nginx ~]# cat /code/mysqli.php <?php $servername = "localhost"; $username = "root"; $password = "Bgx123.com"; $conn = mysqli_connect($servername, $username, $password); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "php连接MySQL数据库成功"; ?> 4) 通过浏览器访问info.php文件, 如能出现php相关的信息, 则表示nginx与php能正常工作5) 访问mysqli.php验证php-mysqli模块是否正常工作4.部署博客产品Wordpress1) 配置Nginx虚拟主机站点,域名为blog.bgx.com [root@nginx ~] server { listen 80; server_name blog.bgx.com; root /code/wordpress; index index.php index.html; location ~ \.php$ { root /code/wordpress; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } 2) 重启nginx服务[root@nginx ~] 3) 获取wordpress产品,解压并部署wordress[root@nginx ~] [root@nginx ~] [root@nginx code] [root@nginx ~] [root@nginx ~] 4) 由于wordpress产品需要依赖数据库, 所以需要手动建立数据库[root@nginx ~] mysql> create database wordpress; mysql> exit 5) 通过浏览器访问wordpress, 并部署该产品5.部署知乎产品Wecenter1.配置Nginx虚拟主机站点,域名为zh.bgx.com [root@http-server ~] server { listen 80; server_name zh.bgx.com; root /code/zh; index index.php index.html; location ~ \.php$ { root /code/zh; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } [root@http-server ~] 2.下载Wecenter产品,部署Wecenter并授权[root@web02 ~] [root@web02 ~] [root@web02 ~] [root@web02 ~] 3.由于wecenter产品需要依赖数据库, 所以需要手动建立数据库 [root@http-server ~] MariaDB [(none)]> create database zh; MariaDB [(none)]> exit 3.通过浏览器访问网站6.部署网校产品Edusohu1.配置Nginx虚拟主机站点,域名为edu.bgx.com [root@http-server ~] server { listen 80; server_name edu.bgx.com; root /code/edu; index index.php index.html; location ~ \.php$ { root /code/edu; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } [root@http-server ~] 2.下载edusohu产品,部署edusohu并授权 //获取wordpress代码 [root@http-server ~] [root@http-server /soft/src]# wget http://download.edusoho.com/edusoho-8.2.17.tar.gz //解压软件网站源码文件, 并授权站点目录,不然会导致无法安装 [root@http-server /soft/src] [root@http-server /soft/src] [root@http-server ~] [root@http-server ~] //由于edusohu会自动创建数据库, 所以无需创建数据库 3.通过浏览器访问网站7.迁移数据至独立服务器拆分LNMP的数据库到独立的数据库服务器步骤1.老服务器操作1) 指定导出对应的数据库文件(Bgx123.com是数据库密码)[root@web02 ~]# mysqldump -uroot -p'Bgx123.com' --all-databases --single-transaction > `date +%F%H`-mysql-all.sql 2) 拷贝备份数据库文件至新的数据库服务器上[root@web02 zh] 2.新服务器操作1) 导入旧的数据库至新的数据库中[root@db01 ~] 2) 登录新的数据库, 并检查数据库已被导入成功[root@db01 ~] mysql> show databases; 3) 在新数据库上授权, 允许所有网段, 通过all账户连接数据库 mysql> grant all on *.* to all@'%' identified by 'Bgx123.com'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) 4) 修改Wordpress产品代码连接数据库的配置文件[root@web01 ~] define('DB_NAME', 'wordpress'); define('DB_USER', 'all'); define('DB_PASSWORD', 'Bgx123.com'); define('DB_HOST', '172.16.1.51'); 5) 修改wecenter产品代码连接数据库的配置文件[root@web01 zh] system/config/database.php: 'password' => 'Bgx123.com', [root@web01 zh] 'host' => '172.16.1.51', 'username' => 'all', 'password' => 'Bgx123.com', 'dbname' => 'zh', 6) 修改edusoho产品代码连接数据库的配置文件[root@web01 edu] app/config/parameters.yml: database_password: 'Bgx123.com' [root@web01 edu] parameters: database_driver: pdo_mysql database_host: 172.16.1.51 database_port: 3306 database_name: edu database_user: all database_password: 'Bgx123.com' [root@web01 edu] 8.迁移图片至独立服务器1.nfs-server服务端操作1) 配置nfs共享的目录[root@nfs01 ~] /data/blog 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666) /data/edu 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666) /data/zh 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666) 2) 创建对应共享的目录[root@nfs01 ~] [root@nfs01 ~] [root@nfs01 ~] 2.web01端操作1) WEB客户端验证NFS是否安装成功[root@web01 ~] [root@web01 ~] Export list for 172.16.1.31: /data/zh 172.16.1.0/24 /data/edu 172.16.1.0/24 /data/blog 172.16.1.0/24 2) 获取Wordpress产品的附件和图片存放的位置浏览器->右键->检查->Network->选择按钮->点击一下图片 3) 备份web服务器上的Wordpress图片和附件[root@web01 wp-content] [root@web01 wp-content] 4) 客户端执行挂载操作[Wordpress][root@web01 wp-content] [root@web01 wp-content] 5) 将挂载信息加入开机自启[root@web01 wp-content] 172.16.1.31:/data/blog /code/wordpress/wp-content/uploads nfs defaults 0 0 [root@web01 wp-content] 3.web02端操作1) 客户端挂载nfs服务端存储[root@web02 ~] 2) 将挂载信息加入开机自启[root@web02 ~] 172.16.1.31:/data/blog /code/wordpress/wp-content/uploads nfs defaults 0 0 [root@web02 wp-content] 9.扩展相同的Web服务器快速的扩展一台web服务器10.0.0.9, 数据库使用10.0.0.51,图片与附件使用10.0.0.311) 创建www用户[root@web03 ~] [root@web03 ~] 2) 安装LNP[root@web03 ~] [root@web03 ~] [root@web03 ~] [root@web03 ~] 3) 将web01的nginx配置文件导入到web03[root@web03 ~] 4) 将web01的php配置文件导入到web03[root@web03 ~] 5) 将web01的产品代码导到web03,在web1上线进行打包操作[root@web01 ~] [root@web03 ~] [root@web03 ~] 6) 启动nginx与php-fpm服务[root@web03 ~] [root@web03 ~] 7) 在web03上进行挂载[root@web03 ~] 转 xuliangwei.com
2023年11月03日
20 阅读
0 评论
0 点赞
2023-11-03
Nginx基础应用
1.Nginx目录索引1.Nginx默认是不允许列出整个目录浏览下载。Syntax: autoindex on | off; Default: autoindex off; Context: http, server, location autoindex_exact_size off; 默认为on, 显示出文件的确切大小,单位是bytes。 修改为off,显示出文件的大概大小,单位是kB或者MB或者GB。 autoindex_localtime on; 默认为off,显示的文件时间为GMT时间。 修改为on, 显示的文件时间为文件的服务器时间。 charset utf-8,gbk; 默认中文目录乱码,添加上解决乱码。 2.配置站点目录实现浏览功能server { listen 80; server_name module.bgx.com; location / { root /code/down; charset utf-8,gbk; autoindex on; autoindex_localtime on; autoindex_exact_size off; } } 2.Nginx状态监控1.ngx_http_stub_status_module用于展示Nginx连接状态信息, 需要--with-http_stub_status_module模块支持Syntax: stub_status; Default: — Context: server, location 2.配置Nginx status示例server { listen 80; server_name module.bgx.com; access_log off; location /nginx_status { stub_status; } } 3.使用浏览器访问http://IP/nginx_status访问后得到的结果4.Nginx 状态详细信息如下Active connections accepts handled requests Reading Writing Waiting keepalive_timeout 0; keepalive_timeout 65; 3.Nginx访问控制基于IP的访问控制 http_access_module基于用户登陆认证 http_auth_basic_module1.Nginx基于IP的访问控制 Syntax: allow address | CIDR | unix: | all; Default: — Context: http, server, location, limit_except Syntax: deny address | CIDR | unix: | all; Default: — Context: http, server, location, limit_except 1) 访问控制配置示例, 拒绝指定的IP, 其他全部允许server { listen 80; server_name module.bgx.com; access_log off; location /nginx_status { stub_status; deny 10.0.0.1; allow all; } } 2) 访问控制配置示例, 只允许谁能访问, 其它全部拒绝server { listen 80; server_name module.bgx.com; access_log off; location /nginx_status { stub_status; allow 10.0.0.0/24; allow 127.0.0.1; deny all; } } 2.Nginx基于用户登陆认证1) 基于用户登陆认证配置语法 Syntax: auth_basic string| off; Default: auth_basic off; Context: http, server, location, limit_except Syntax: auth_basic_user_file file; Default: - Context: http, server, location, limit_except 2) 基于用户登陆认证配置实践 [root@xuliangwei ~] [root@xuliangwei ~] server { listen 80; server_name module.bgx.com; access_log off; location /nginx_status { stub_status; auth_basic "Auth access Blog Input your Passwd!"; auth_basic_user_file /etc/nginx/auth_conf; } } 4.Nginx访问限制经常会遇到这种情况,服务器流量异常,负载过大等等。对于大流量恶意的攻击访问, 会带来带宽的浪费,服务器压力,影响业务,往往考虑对同一个ip的连接数,并发数进行限制。ngx_http_limit_conn_module模块可以根据定义的key来限制每个键值的连接数,如同一个IP来源的连接数。limit_conn_module 连接频率限制limit_req_module 请求频率限制http协议的连接与请求HTTP是建立在TCP, 在完成HTTP请求需要先建立TCP三次握手(称为TCP连接),在连接的基础上在HTTP请求。HTTP请求建立在一次TCP连接基础上,一次TCP请求至少产生一次HTTP请求注:客户端的IP地址作为键。$remote_addr 变量的长度为7字节到15字节$binary_remote_addr 变量的长度是固定的4字节1.Nginx连接限制配置实战1)Nginx连接限制配置语法Syntax: limit_conn_zone key zone=name:size; Default: — Context: http Syntax: limit_conn zone number; Default: — Context: http, server, location 2)Nginx连接限制配置实践 limit_conn_zone $binary_remote_addr zone=conn_zone:10m; server { limit_conn conn_zone 1; location / { root /code; index index.html; } 3).使用ab工具进行压力测试[root@xuliangwei ~] [root@xuliangwei ~] 4).nginx日志结果2018/10/24 18:04:49 [error] 28656#28656: *1148 limiting connections by zone "conn_zone", client: 123.66.146.123, server: www.xuliangwei.com, request: "GET / HTTP/1.0", host: "www.xuliangwei.com" 2018/10/24 18:04:49 [error] 28656#28656: *1155 limiting connections by zone "conn_zone", client: 123.66.146.123, server: www.xuliangwei.com, request: "GET / HTTP/1.0", host: "www.xuliangwei.com" 2018/10/24 18:04:49 [error] 28656#28656: *1156 limiting connections by zone "conn_zone", client: 123.66.146.123, server: www.xuliangwei.com, request: "GET / HTTP/1.0", host: "www.xuliangwei.com" 2.Nginx请求限制配置实战1)Nginx请求限制配置语法Syntax: limit_req_zone key zone=name:size rate=rate; Default: — Context: http Syntax: limit_conn zone number [burst=number] [nodelay]; Default: — Context: http, server, location 2)Nginx请求限制配置实战 limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s; server { listen 80; server_name module.bgx.com; limit_req zone=req_zone; limit_req zone=req_zone burst=3 nodelay; location / { root /code; index index.html; } } 3).使用ab工具进行压力测试[root@xuliangwei ~] [root@xuliangwei ~] 4).nginx日志结果2018/10/24 07:38:53 [error] 81020#0: *8 limiting requests, excess: 3.998 by zone "req_zone", client: 10.0.0.10, server: module.bgx.com, request: "GET /index.html HTTP/1.0", host: "10.0.0.10" 2018/10/24 07:38:53 [error] 81020#0: *9 limiting requests, excess: 3.998 by zone "req_zone", client: 10.0.0.10, server: module.bgx.com, request: "GET /index.html HTTP/1.0", host: "10.0.0.10" 2018/10/24 07:38:53 [error] 81020#0: *10 limiting requests, excess: 3.998 by zone "req_zone", client: 10.0.0.10, server: module.bgx.com, request: "GET /index.html HTTP/1.0", host: "10.0.0.10" 3.Nginx连接限制没有请求限制有效?我们前面说过, 多个请求可以建立在一次的TCP连接之上, 那么我们对请求的精度限制,当然比对一个连接的限制会更加的有效因为同一时刻只允许一个连接请求进入, 但是同一时刻多个请求可以通过一个连接进入。所以请求限制才是比较优的解决方案。5.Nginx虚拟站点所谓虚拟主机,及在一台服务器上配置多个网站如: 公司主页、博客、论坛看似三个网站, 实则可以运行在一台服务器上。1.基于域名虚拟主机配置实战1.创建web站点目录[root@bgx ~] [root@bgx ~] [root@bgx ~] 2.配置不同域名的虚拟主机[root@bgx ~] server { listen 80; server_name www.xuliangwei.com; root /soft/code/www; index index.html; ... } [root@bgx ~] server { ... listen 80; server_name bbs.xuliangwei.com; root /soft/code/bbs; index index.html; } 2.基于端口虚拟主机配置实战//仅修改listen监听端口即可, 但不能和系统端口出现冲突 server { ... listen 8001; ... } server { ... listen 8002; ... } 3.基于虚拟主机别名配置实战实现用户访问多个域名对应同一个网站, 比如用户访问www.xuliangwei.com和访问xuliangwei.com内容一致 [root@bgx ~] server { listen 80; server_name www.bgx.com; } server { listen 80; server_name bgx.com; } [root@bgx ~] server { listen 80; server_name www.bgx.com bgx.com; ... } [root@bgx ~] Go [root@bgx ~] Go 6.Nginx日志配置在学习日志之前, 我们需要先回顾下HTTP请求和返回Nginx有非常灵活的日志记录模式。每个级别的配置可以有各自独立的访问日志。日志格式 通过log_format命令定义格式。1.log_format定义日志格式语法# 配置语法: 包括: error.log access.log Syntax: log_format name [escape=default|json] string ...; Default: log_format combined "..."; Context: http 2.默认Nginx定义语法格式如下log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; 3.Nginx日志格式允许包含的内置变量$remote_addr $remote_user $time_local $time_iso8601 $request $status $body_bytes_sent $bytes_sent $msec $http_referer $http_user_agent $http_x_forwarded_for $request_length $request_time 4.access_log日志配置语法Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]; access_log off; Default: access_log logs/access.log combined; Context: http, server, location, if in location, limit_except 5.Nginx Access日志配置实践 server { access_log /var/log/nginx/www.bgx.com.log main; } location / { access_log /var/log/nginx/www.bgx.com.log main; } access_log off; 7.Nginx Location使用Nginx Location可以控制访问网站的路径, 但一个server可以有多个location配置, 多个location的优先级该如何区分1.Location语法示例location [=|^~|~|~*|!~|!~*|/] /uri/ { ... } 2.Location语法优先级排列 匹配符 匹配规则 优先级 = 精确匹配 1 ^~ 以某个字符串开头 2 ~ 区分大小写的正则匹配 3 ~* 不区分大小写的正则匹配 4 !~ 区分大小写不匹配的正则 5 !~* 不区分大小写不匹配的正则 6 / 通用匹配,任何请求都会匹配到 7 3.配置网站验证Location优先级[root@Nginx conf.d] server { listen 80; server_name bgx.com; location / { default_type text/html; return 200 "location /"; } location =/ { default_type text/html; return 200 "location =/"; } location ~ / { default_type text/html; return 200 "location ~/"; } } 4.测试Location效果 [root@Nginx conf.d] location =/ [root@Nginx ~] location ~/ [root@Nginx ~] location / 5.Locaiton应用场景 location / { } location ~ \.php$ { fastcgi_pass http://127.0.0.1:9000; } location ~ \.jsp$ { proxy_pass http://127.0.0.1:8080; } location ~* .*\.(jpg|gif|png|js|css)$ { rewrite (.*) http://cdn.xuliangwei.com$request_uri; } location ~* "\.(sql|bak|tgz|tar.gz|.git)$" { default_type text/html; return 403 "启用访问控制成功"; } 转 xuliangwei.com
2023年11月03日
8 阅读
0 评论
0 点赞
2023-11-03
nginx基础
1.Nginx基本简述Nginx是一个开源且高性能、可靠的HttpWeb服务、代理服务。开源: 直接获取源代码高性能: 支持海量并发可靠: 服务稳定1.常见的 HTTP Web服务Httpd 由Apache基金会IIS 微软服务器版GWS Google开发Openrestry 基于nginx+luaTengline 淘宝基于Nginx开发2.为什么选择 Nginx1.Nginx非常轻量1.功能模块少(源代码仅保留http与核心模块代码,其余不够核心代码会作为插件来安装)2.代码模块化(易读,便于二次开发,对于开发人员非常友好)2.互联网公司都选择Nginx1.Nginx技术成熟, 国内公司基本大规模使用2.适合当前主流架构趋势, 微服务、云架构、中间层3.统一技术栈, 降低维护成本, 降低技术更新成本。3.Nginx采用Epool网络模型, Apache采用Select模型Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。Epool: 当用户发起请求,epool模型会直接进行处理,效率高效,并无连接限制。3.Nginx 应用场景2.Nginx快速安装Nginx软件安装的方式有很多种1.源码编译=>Nginx (1.版本随意 2.安装复杂 3.升级繁琐)2.epel仓库=>Nginx (1.版本较低 2.安装简单 3.配置不易读)3.官方仓库=>Nginx (1.版本较新 2.安装简单 3.配置易读 √)1.安装Nginx软件所需依赖 [root@web ~] pcre pcre-devel make automake wget httpd-tools vim tree 2.配置nginx yum源[必须使用官方源][root@web ~] [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1 3.安装Nginx服务[root@web ~] 4.检查Nginx软件版本[root@web ~] nginx version: nginx/1.14.0 5.为了让大家更清晰的了解Nginx软件的全貌,可使用rpm -ql nginx查看整体的目录结构及对应的功能如下表格对Nginx安装目录做详细概述 路径 类型 作用 /etc/nginx /etc/nginx/nginx.conf /etc/nginx/conf.d /etc/nginx/conf.d/default.conf 配置文件 Nginx主配置文件 /etc/nginx/fastcgi_params /etc/nginx/scgi_params /etc/nginx/uwsgi_params 配置文件 Cgi、Fastcgi、Uwcgi配置文件 /etc/nginx/win-utf /etc/nginx/koi-utf /etc/nginx/koi-win 配置文件 Nginx编码转换映射文件 /etc/nginx/mime.types 配置文件 http协议的Content-Type与扩展名 /usr/lib/systemd/system/nginx.service 配置文件 配置系统守护进程管理器 /etc/logrotate.d/nginx 配置文件 Nginx日志轮询,日志切割 /usr/sbin/nginx /usr/sbin/nginx-debug 命令 Nginx终端管理命令 /etc/nginx/modules /usr/lib64/nginx /usr/lib64/nginx/modules 目录 Nginx模块目录 /usr/share/nginx /usr/share/nginx/html /usr/share/nginx/html/50x.html /usr/share/nginx/html/index.html 目录 Nginx默认站点目录 /usr/share/doc/nginx-1.12.2 /usr/share/man/man8/nginx.8.gz 目录 Nginx的帮助手册 /var/cache/nginx 目录 Nginx的缓存目录 /var/log/nginx 目录 Nginx的日志目录 6.通过nginx -v查看Nginx编译选项 编译选项 作用 --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock 程序安装目录和路径 --http-client-body-temp-path=/var/cache/nginx/client_tem --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp 临时缓存文件 --user=nginx --group=nginx 设定Nginx进程启动用户和组(安全) --with-cc-opt 设置额外的参数将被添加到CFLAGS变量 --with-ld-opt 设置附加的参数, 链接系统库 3.Nginx配置文件Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束。1.CoreModule 核心模块2.EventModule 事件驱动模块3.HttpCoreModule http内核模块需了解扩展项CoreModule层下可以有Event、HTTPHTTP模块层允许有多个Server层, Server主要用于配置多个网站Server层又允许有多个Location, Location主要用于定义网站访问路径CoreModule核心模块user www; worker_processes 1; error_log /log/nginx/error.log pid /var/run/nginx.pid events事件模块events { worker_connections use epool; } http内核模块 http { ... 'server' { listen 80; server_name localhost; access_log host.access.log 'location' / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; } ... 'server' { ... } include /etc/nginx/conf.d/*.conf; } 4.Nginx配置网站1.新增nginx配置文件[root@web01 conf.d] server { listen 80; server_name game.wangmanyuan.com; location / { root /wangmanyuan_code; index index.html; } } 2.放置对应的源代码文件至指定的目录[root@web01 conf.d] [root@web01 conf.d] [root@web01 wangmanyuan_code] [root@web01 wangmanyuan_code] [root@web01 wangmanyuan_code] ceshi game html5.zip img index.html readme.txt 3.检查nginx的语法是否存在错误[root@web01 wangmanyuan_code] nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful 4.重载Nginx [reload|restart][root@web01 wangmanyuan_code] [root@web01 wangmanyuan_code] 5.如何访问对应的网站1.通过服务器的IP直接访问(不推荐) 2.通过假域名方式访问(推荐方式) Windows修改 C:\Windows\System32\drivers\etc\hosts 10.0.0.7 game.wangmanyuan.com 3.使用ping命令测试域名解析是否正常 转 xuliangwei.com
2023年11月03日
10 阅读
0 评论
0 点赞
2023-08-24
Nginx中的break和last
两个指令用法相同,但含义不同,需要放到rewrite规则的末尾,用来控制重写后的链接是否继续被nginx配置执行(主要是rewrite、return指令)。 示例1(连续两条rewrite规则):server{ listen 80; server_name test.com; root /tmp/123.com; rewrite /1.html /2.html ; rewrite /2.html /3.html ; } 当我们请求1.html时,最终访问到的是3.html,两条rewrite规则先后执行。 break和last在location {}外部 格式:rewrite xxxxx break; 示例2(增加break):server{ listen 80; server_name test.com; root /tmp/123.com; rewrite /1.html /2.html break; rewrite /2.html /3.html; } 当我们请求1.html时,最终访问到的是2.html 说明break在此示例中,作用是不再执行break以下的rewrite规则。 但,当配置文件中有location时,它还会去执行location{}段的配置(请求要匹配该location)。 示例3(break后面还有location段):server{ listen 80; server_name test.com; root /tmp/123.com; rewrite /1.html /2.html break; rewrite /2.html /3.html; location /2.html { return 403; } } 当请求1.html时,最终会返回403状态码,说明它去匹配了break后面的location{}配置。 以上2个示例中,可以把break替换为last,它们两者起到的效果一模一样。 当break和last在location{}里面 示例4(什么都不加):server{ listen 80; server_name test.com; root /tmp/123.com; location / { rewrite /1.html /2.html; rewrite /2.html /3.html; } location /2.html { rewrite /2.html /a.html; } location /3.html { rewrite /3.html /b.html; } } 当请求/1.html,最终将会访问/b.html,连续执行location /下的两次rewrite,跳转到了/3.html,然后又匹配location /3.html 示例5(增加break):server{ listen 80; server_name test.com; root /tmp/123.com; location / { rewrite /1.html /2.html break; rewrite /2.html /3.html; } location /2.html { rewrite /2.html /a.html; } location /3.html { rewrite /3.html /b.html; } } 当请求/1.html,最终会访问/2.html 在location{}内部,遇到break,本location{}内以及后面的所有location{}内的所有指令都不再执行。 示例6(增加last):server{ listen 80; server_name test.com; root /tmp/123.com; location / { rewrite /1.html /2.html last; rewrite /2.html /3.html; } location /2.html { rewrite /2.html /a.html; } location /3.html { rewrite /3.html /b.html; } } 当请求/1.html,最终会访问/a.html 在location{}内部,遇到last,本location{}内后续指令不再执行,而重写后的url再次从头开始,从头到尾匹配一遍规则。 结论当rewrite规则在location{}外,break和last作用一样,遇到break或last后,其后续的rewrite/return语句不再执行。但后续有location{}的话,还会近一步执行location{}里面的语句,当然前提是请求必须要匹配该location。 当rewrite规则在location{}里,遇到break后,本location{}与其他location{}的所有rewrite/return规则都不再执行。 当rewrite规则在location{}里,遇到last后,本location{}里后续rewrite/return规则不执行,但重写后的url再次从头开始执行所有规则,哪个匹配执行哪个。
2023年08月24日
19 阅读
0 评论
2 点赞
1
2