首页
友链
统计
留言
更多
直播
壁纸
推荐
我的毛线
哔哔点啥
院长科技
Search
1
本站官方群:894703859------|诚邀各位大佬的入驻!
580 阅读
2
pxe 自动化安装系统
570 阅读
3
软件安装
434 阅读
4
新款螺旋帽子编织#夏凉帽#合股线夏凉帽编织
379 阅读
5
10 个Linux Awk文本处理经典案例
372 阅读
linux
yaml
iptables
shell
ansible
ssl
awk
sed
pxe
prometheus
Nginx
k8s
fish
dev
go占位符
clickhouse
html标签
vue基础
html表格
vue项目
vscode
css基础
css定位
css精灵图
code
html5
project
js
jQuery
面向对象
编织
编织视频
常用工具
微软
登录
/
注册
Search
标签搜索
基础
js
Nginx
css
webapi
jQuery
面向对象
command
项目
ansible
用户权限
go
html
文件管理
命令
k8s
shell
pxe
awk
vscode
JustDoIt
累计撰写
114
篇文章
累计收到
4
条评论
首页
栏目
linux
yaml
iptables
shell
ansible
ssl
awk
sed
pxe
prometheus
Nginx
k8s
fish
dev
go占位符
clickhouse
html标签
vue基础
html表格
vue项目
vscode
css基础
css定位
css精灵图
code
html5
project
js
jQuery
面向对象
编织
编织视频
常用工具
微软
页面
友链
统计
留言
直播
壁纸
推荐
我的毛线
哔哔点啥
院长科技
搜索到
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日
33 阅读
0 评论
1 点赞
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日
57 阅读
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日
34 阅读
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日
41 阅读
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日
51 阅读
0 评论
2 点赞
2022-05-11
nginx location配置规则
1.匹配模式及顺序举例 location = /uri = 开头表示精确匹配,只有完全匹配上才能生效 location ^~ /uri ^~ 开头对 URL 路径进行前缀匹配,并且在正则之前 location ~ pattern ~ 开头表示区分大小写的正则匹配 location /uri 不带任何修饰符,也表示前缀匹配,但是在正则匹配之后,如果没有正则命中,命中最长的规则 location / 通用匹配,任何未匹配到其它 location 的请求都会匹配到,相当于 switch 中的 default 2.location 是否以“/”结尾在 ngnix 中 location 进行的是模糊匹配 没有“/”结尾时,location /abc/def 可以匹配 /abc/defghi 请求,也可以匹配 /abc/def/ghi 等 而有“/”结尾时,location /abc/def/ 不能匹配 /abc/defghi 请求,只能匹配 /abc/def/anything 这样的请求 二、proxy_pass配置规则(1)配置 proxy_pass 时,当在后面的 url 加上了 /,相当于是绝对路径,则 Nginx 不会把 location 中匹配的路径部分加入代理 uri。 (2)如果配置 proxy_pass 时,后面没有 /,Nginx 则会把匹配的路径部分加入代理 uri。 例如:server { listen 8081; server_name localhost; location / { root html; index index.html index.htm; } #情景1:proxy_pass后有/ ,表绝对路径,不把匹配部分加入最终代理路径(location 和proxy_pass结尾一致) #访问地址:http://localhost:8081/WCP.Service/wcp/modeladapter/download/asc.shtml #最终代理:http://10.194.171.7:13082/modeladapter/download/asc.shtml location /WCP.Service/wcp/modeladapter/download/ { proxy_pass http://10.194.171.7:13082/modeladapter/download/; } #访问地址:http://localhost:8081/model/asc.shtml #最终代理:http://127.0.0.1:8082/model/asc.shtml location /model/ { proxy_pass http://127.0.0.1:8082/model/; } #情景2:proxy_pass后有/ ,表绝对路径,不把匹配部分加入最终代理路径(location 和proxy_pass结尾不一致) #访问地址:http://localhost:8081/model/asc.shtml #最终代理:http://127.0.0.1:8082/asc.shtml location /model/ { proxy_pass http://127.0.0.1:8082/; } #情景3:proxy_pass后没有 / ,Nginx会把匹配部分带到代理的url #访问地址:http://localhost:8081/model/asc.shtml #最终代理:http://127.0.0.1:8082/model/asc.shtml location /model/ { proxy_pass http://127.0.0.1:8082; } #情景4 #访问地址:http://localhost:8081/model/asc.shtml #最终代理:http://127.0.0.1:8082/AAAmodel/asc.shtml location /model/ { proxy_pass http://127.0.0.1:8082/AAA; } #情景5 #访问地址:http://localhost:8081/model/asc.shtml #最终代理:http://127.0.0.1:8082/asc.shtml location /model { proxy_pass http://127.0.0.1:8082/; } #情景6 #访问地址:http://localhost:8081/modelBBB/asc.shtml #最终代理:http://127.0.0.1:8082/asc.shtml location /model { proxy_pass http://127.0.0.1:8082/; } location /opus-front-sso { proxy_pass http://10.194.170.94/opus-front-sso; } location /awater { proxy_pass http://10.194.170.94/awater; } }
2022年05月11日
166 阅读
0 评论
2 点赞
2022-03-15
什么是Nginx
Nginx是一个 轻量级/高性能的反向代理Web服务器,用于 HTTP、HTTPS、SMTP、POP3 和 IMAP 协议。他实现非常高效的反向代理、负载平衡,他可以处理2-3万并发连接数,官方监测能支持5万并发,现在中国使用nginx网站用户有很多,例如:新浪、网易、 腾讯等。Nginx 有哪些优点? 跨平台、配置简单。 非阻塞、高并发连接:处理 2-3 万并发连接数,官方监测能支持 5 万并发。 内存消耗小:开启 10 个 Nginx 才占 150M 内存。 成本低廉,且开源。 稳定性高,宕机的概率非常小。 内置的健康检查功能:如果有一个服务器宕机,会做一个健康检查,再发送的请求就不会发送到宕机的服务器了。重新将请求提交到其他的节点上 Nginx应用场景? http服务器。Nginx是一个http服务可以独立提供http服务。可以做网页静态服务器。 虚拟主机。可以实现在一台服务器虚拟出多个网站,例如个人网站使用的虚拟机。 反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会应为某台服务器负载高宕机而某台服务器闲置的情况。 nginz 中也可以配置安全管理、比如可以使用Nginx搭建API接口网关,对每个接口服务进行拦截。 Nginx怎么处理请求的?server { # 第一个Server区块开始,表示一个独立的虚拟主机站点 listen 80; # 提供服务的端口,默认80 server_name localhost; # 提供服务的域名主机名 location / { # 第一个location区块开始 root html; # 站点的根目录,相当于Nginx的安装目录 index index.html index.html; # 默认的首页文件,多个用空格分开 } 首先,Nginx 在启动时,会解析配置文件,得到需要监听的端口与 IP 地址,然后在 Nginx 的 Master 进程里面先初始化好这个监控的Socket(创建 S ocket,设置 addr、reuse 等选项,绑定到指定的 ip 地址端口,再 listen 监听)。 然后,再 fork(一个现有进程可以调用 fork 函数创建一个新进程。由 fork 创建的新进程被称为子进程 )出多个子进程出来。 之后,子进程会竞争 accept 新的连接。此时,客户端就可以向 nginx 发起连接了。当客户端与nginx进行三次握手,与 nginx 建立好一个连接后。此时,某一个子进程会 accept 成功,得到这个建立好的连接的 Socket ,然后创建 nginx 对连接的封装,即 ngx_connection_t 结构体。 接着,设置读写事件处理函数,并添加读写事件来与客户端进行数据的交换。 最后,Nginx 或客户端来主动关掉连接,到此,一个连接就寿终正寝了。 Nginx 是如何实现高并发的?如果一个 server 采用一个进程(或者线程)负责一个request的方式,那么进程数就是并发数。那么显而易见的,就是会有很多进程在等待中。等什么?最多的应该是等待网络传输。而 Nginx 的异步非阻塞工作方式正是利用了这点等待的时间。在需要等待的时候,这些进程就空闲出来待命了。因此表现为少数几个进程就解决了大量的并发问题。Nginx是如何利用的呢,简单来说:同样的 4 个进程,如果采用一个进程负责一个 request 的方式,那么,同时进来 4 个 request 之后,每个进程就负责其中一个,直至会话关闭。期间,如果有第 5 个request进来了。就无法及时反应了,因为 4 个进程都没干完活呢,因此,一般有个调度进程,每当新进来了一个 request ,就新开个进程来处理。回想下,BIO 是不是存在酱紫的问题?Nginx 不这样,每进来一个 request ,会有一个 worker 进程去处理。但不是全程的处理,处理到什么程度呢?处理到可能发生阻塞的地方,比如向上游(后端)服务器转发 request ,并等待请求返回。那么,这个处理的 worker 不会这么傻等着,他会在发送完请求后,注册一个事件:如果 upstream 返回了,告诉我一声,我再接着干。于是他就休息去了。此时,如果再有 request 进来,他就可以很快再按这种方式处理。而一旦上游服务器返回了,就会触发这个事件,worker 才会来接手,这个 request 才会接着往下走。这就是为什么说,Nginx 基于事件模型。由于 web server 的工作性质决定了每个 request 的大部份生命都是在网络传输中,实际上花费在 server 机器上的时间片不多。这是几个进程就解决高并发的秘密所在。即:webserver 刚好属于网络 IO 密集型应用,不算是计算密集型。异步,非阻塞,使用 epoll ,和大量细节处的优化。也正是 Nginx 之所以然的技术基石。什么是正向代理?一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。客户端才能使用正向代理。正向代理总结就一句话:代理端代理的是客户端。例如说:我们使用的OpenVPN 等等。什么是反向代理?反向代理(Reverse Proxy)方式,是指以代理服务器来接受 Internet上的连接请求,然后将请求,发给内部网络上的服务器并将从服务器上得到的结果返回给 Internet 上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。反向代理总结就一句话:代理端代理的是服务端。“反向代理服务器的优点是什么?反向代理服务器可以隐藏源服务器的存在和特征。它充当互联网云和web服务器之间的中间层。这对于安全方面来说是很好的,特别是当您使用web托管服务时。Nginx目录结构有哪些?[root@localhost ~]# tree /usr/local/nginx /usr/local/nginx ├── client_body_temp ├── conf # Nginx所有配置文件的目录 │ ├── fastcgi.conf # fastcgi相关参数的配置文件 │ ├── fastcgi.conf.default # fastcgi.conf的原始备份文件 │ ├── fastcgi_params # fastcgi的参数文件 │ ├── fastcgi_params.default │ ├── koi-utf │ ├── koi-win │ ├── mime.types # 媒体类型 │ ├── mime.types.default │ ├── nginx.conf # Nginx主配置文件 │ ├── nginx.conf.default │ ├── scgi_params # scgi相关参数文件 │ ├── scgi_params.default │ ├── uwsgi_params # uwsgi相关参数文件 │ ├── uwsgi_params.default │ └── win-utf ├── fastcgi_temp # fastcgi临时数据目录 ├── html # Nginx默认站点目录 │ ├── 50x.html # 错误页面优雅替代显示文件,例如当出现502错误时会调用此页面 │ └── index.html # 默认的首页文件 ├── logs # Nginx日志目录 │ ├── access.log # 访问日志文件 │ ├── error.log # 错误日志文件 │ └── nginx.pid # pid文件,Nginx进程启动后,会把所有进程的ID号写到此文件 ├── proxy_temp # 临时目录 ├── sbin # Nginx命令目录 │ └── nginx # Nginx的启动命令 ├── scgi_temp # 临时目录 └── uwsgi_temp # 临时目录 Nginx配置文件nginx.conf有哪些属性模块?worker_processes 1; # worker进程的数量 events { # 事件区块开始 worker_connections 1024; # 每个worker进程支持的最大连接数 } # 事件区块结束 http { # HTTP区块开始 include mime.types; # Nginx支持的媒体类型库文件 default_type application/octet-stream; # 默认的媒体类型 sendfile on; # 开启高效传输模式 keepalive_timeout 65; # 连接超时 server { # 第一个Server区块开始,表示一个独立的虚拟主机站点 listen 80; # 提供服务的端口,默认80 server_name localhost; # 提供服务的域名主机名 location / { # 第一个location区块开始 root html; # 站点的根目录,相当于Nginx的安装目录 index index.html index.htm; # 默认的首页文件,多个用空格分开 } # 第一个location区块结果 error_page 500502503504 /50x.html; # 出现对应的http状态码时,使用50x.html回应客户 location = /50x.html { # location区块开始,访问50x.html root html; # 指定对应的站点目录为html } } ...... cookie和session区别? 共同: 存放用户信息。存放的形式:key-value格式 变量和变量内容键值对。 区别: cookie 存放在客户端浏览器 每个域名对应一个cookie,不能跨跃域名访问其他cookie 用户可以查看或修改cookie http响应报文里面给你浏览器设置 钥匙(用于打开浏览器上锁头) session: 存放在服务器(文件,数据库,redis) 存放敏感信息 锁头 为什么 Nginx 不使用多线程?Apache: 创建多个进程或线程,而每个进程或线程都会为其分配 cpu 和内存(线程要比进程小的多,所以 worker 支持比 perfork 高的并发),并发过大会榨干服务器资源。Nginx: 采用单线程来异步非阻塞处理请求(管理员可以配置 Nginx 主进程的工作进程的数量)(epoll),不会为每个请求分配 cpu 和内存资源,节省了大量资源,同时也减少了大量的 CPU 的上下文切换。所以才使得 Nginx 支持更高的并发。nginx和apache的区别轻量级,同样起web服务,比apache占用更少的内存和资源。抗并发,nginx处理请求是异步非阻塞的,而apache则是阻塞性的,在高并发下nginx能保持低资源,低消耗高性能。高度模块化的设计,编写模块相对简单。最核心的区别在于apache是同步多进程模型,一个连接对应一个进程,nginx是异步的,多个连接可以对应一个进程。什么是动态资源、静态资源分离?动态资源、静态资源分离,是让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了拆分以后我们就可以根据静态资源的特点将其做缓存操作,这就是网站静态化处理的核心思路。动态资源、静态资源分离简单的概括是:动态文件与静态文件的分离。为什么要做动、静分离?在我们的软件开发中,有些请求是需要后台处理的(如:.jsp,.do 等等),有些请求是不需要经过后台处理的(如:css、html、jpg、js 等等文件),这些不需要经过后台处理的文件称为静态文件,否则动态文件。因此我们后台处理忽略静态文件。这会有人又说那我后台忽略静态文件不就完了吗?当然这是可以的,但是这样后台的请求次数就明显增多了。在我们对资源的响应速度有要求的时候,我们应该使用这种动静分离的策略去解决动、静分离将网站静态资源(HTML,JavaScript,CSS,img等文件)与后台应用分开部署,提高用户访问静态代码的速度,降低对后台应用访问这里我们将静态资源放到 Nginx 中,动态资源转发到 Tomcat 服务器中去。当然,因为现在七牛、阿里云等 CDN 服务已经很成熟,主流的做法,是把静态资源缓存到 CDN 服务中,从而提升访问速度。相比本地的 Nginx 来说,CDN 服务器由于在国内有更多的节点,可以实现用户的就近访问。并且,CDN 服务可以提供更大的带宽,不像我们自己的应用服务,提供的带宽是有限的。什么叫 CDN 服务?CDN ,即内容分发网络。其目的是,通过在现有的 Internet中 增加一层新的网络架构,将网站的内容发布到最接近用户的网络边缘,使用户可就近取得所需的内容,提高用户访问网站的速度。一般来说,因为现在 CDN 服务比较大众,所以基本所有公司都会使用 CDN 服务。Nginx怎么做的动静分离?只需要指定路径对应的目录。location/可以使用正则表达式匹配。并指定对应的硬盘中的目录。如下:(操作都是在Linux上)location /image/ { root /usr/local/static/; autoindex on; } 步骤:# 创建目录 mkdir /usr/local/static/image # 进入目录 cd /usr/local/static/image # 上传照片 photo.jpg # 重启nginx sudo nginx -s reload 打开浏览器 输入 server_name/image/1.jpg 就可以访问该静态图片了Nginx负载均衡的算法怎么实现的?策略有哪些?为了避免服务器崩溃,大家会通过负载均衡的方式来分担服务器压力。将对台服务器组成一个集群,当用户访问时,先访问到一个转发服务器,再由转发服务器将访问分发到压力更小的服务器。Nginx负载均衡实现的策略有以下五种: 1 .轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端某个服务器宕机,能自动剔除故障系统。 upstream backserver { server 1112; server 1113; } 2.权重 weight的值越大,分配到的访问概率越高,主要用于后端每台服务器性能不均衡的情况下。其次是为在主从的情况下设置不同的权值,达到合理有效的地利用主机资源。 # 权重越高,在被访问的概率越大,如上例,分别是20%,80%。 upstream backserver { server 1112 weight=2; server 1113 weight=8; } 3.ip_hash( IP绑定) 每个请求按访问IP的哈希结果分配,使来自同一个IP的访客固定访问一台后端服务器,并且可以有效解决动态网页存在的session共享问题 upstream backserver { ip_hash; server 1112:88; server 1113:80; } fair(第三方插件)必须安装upstream_fair模块。 对比 weight、ip_hash更加智能的负载均衡算法,fair算法可以根据页面大小和加载时间长短智能地进行负载均衡,响应时间短的优先分配。# 哪个服务器的响应速度快,就将请求分配到那个服务器上。 upstream backserver { server server1; server server2; fair; } 5.url_hash(第三方插件)必须安装Nginx的hash软件包 按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,可以进一步提高后端缓存服务器的效率。upstream backserver { server squid1:3128; server squid2:3128; hash $request_uri; hash_method crc32; } 如何用Nginx解决前端跨域问题?使用Nginx转发请求。把跨域的接口写成调本域的接口,然后将这些接口转发到真正的请求地址。Nginx虚拟主机怎么配置?`1、基于域名的虚拟主机,通过域名来区分虚拟主机——应用:外部网站2、基于端口的虚拟主机,通过端口来区分虚拟主机——应用:公司内部网站,外部网站的管理后台3、基于ip的虚拟主机。基于虚拟主机配置域名需要建立/data/www /data/bbs目录,windows本地hosts添加虚拟机ip地址对应的域名解析;对应域名网站目录下新增index.html文件; # 当客户端访问www.lijie.com,监听端口号为80,直接跳转到data/www目录下文件 server { listen 80; server_name www.lijie.com; location / { root data/www; index index.html index.htm; } } # 当客户端访问www.lijie.com,监听端口号为80,直接跳转到data/bbs目录下文件 server { listen 80; server_name bbs.lijie.com; location / { root data/bbs; index index.html index.htm; } } 基于端口的虚拟主机使用端口来区分,浏览器使用域名或ip地址:端口号 访问# 当客户端访问www.lijie.com,监听端口号为8080,直接跳转到data/www目录下文件 server { listen 8080; server_name 8080.lijie.com; location / { root data/www; index index.html index.htm; } } # 当客户端访问www.lijie.com,监听端口号为80直接跳转到真实ip服务器地址 127.0.0.1:8080 server { listen 80; server_name www.lijie.com; location / { proxy_pass http://127.0.0.1:8080; index index.html index.htm; } } location的作用是什么?location指令的作用是根据用户请求的URI来执行不同的应用,也就是根据用户请求的网站URL进行匹配,匹配成功即进行相关的操作。location的语法能说出来吗?注意:~ 代表自己输入的英文字母Location正则案例# 优先级1,精确匹配,根路径 location =/ { return 400; } # 优先级2,以某个字符串开头,以av开头的,优先匹配这里,区分大小写 location ^~ /av { root /data/av/; } # 优先级3,区分大小写的正则匹配,匹配/media*****路径 location ~ /media { alias /data/static/; } # 优先级4 ,不区分大小写的正则匹配,所有的****.jpg|gif|png 都走这里 location ~* .*\.(jpg|gif|png|js|css)$ { root /data/av/; } # 优先7,通用匹配 location / { return 403; } 限流怎么做的?Nginx限流就是限制用户请求速度,防止服务器受不了限流有3种 正常限制访问频率(正常流量) 突发限制访问频率(突发流量) 限制并发连接数Nginx的限流都是基于漏桶流算法 实现三种限流算法 1、正常限制访问频率(正常流量):限制一个用户发送的请求,我Nginx多久接收一个请求。 Nginx中使用ngx_http_limit_req_module模块来限制的访问频率,限制的原理实质是基于漏桶算法原理来实现的。在nginx.conf配置文件中可以使用limit_req_zone命令及limit_req命令限制单个IP的请求处理频率。# 定义限流维度,一个用户一分钟一个请求进来,多余的全部漏掉 limit_req_zone $binary_remote_addr zone=one:10m rate=1r/m; # 绑定限流维度 server{ location /seckill.html{ limit_req zone=zone; proxy_pass http://lj_seckill; } } 1r/s代表1秒一个请求,1r/m一分钟接收一个请求, 如果Nginx这时还有别人的请求没有处理完,Nginx就会拒绝处理该用户请求。 2、突发限制访问频率(突发流量):限制一个用户发送的请求,我Nginx多久接收一个。 上面的配置一定程度可以限制访问频率,但是也存在着一个问题:如果突发流量超出请求被拒绝处理,无法处理活动时候的突发流量,这时候应该如何进一步处理呢?Nginx提供burst参数结合nodelay参数可以解决流量突发的问题,可以设置能处理的超过设置的请求数外能额外处理的请求数。我们可以将之前的例子添加burst参数以及nodelay参数:# 定义限流维度,一个用户一分钟一个请求进来,多余的全部漏掉 limit_req_zone $binary_remote_addr zone=one:10m rate=1r/m; # 绑定限流维度 server{ location/seckill.html{ limit_req zone=zone burst=5 nodelay; proxy_pass http://lj_seckill; } } 为什么就多了一个 burst=5 nodelay; 呢,多了这个可以代表Nginx对于一个用户的请求会立即处理前五个,多余的就慢慢来落,没有其他用户的请求我就处理你的,有其他的请求的话我Nginx就漏掉不接受你的请求 3、 限制并发连接数Nginx中的 ngx_http_limit_conn_module模块提供了限制并发连接数的功能,可以使用limit_conn_zone指令以及limit_conn执行进行配置。接下来我们可以通过一个简单的例子来看下:http { limit_conn_zone $binary_remote_addr zone=myip:10m; limit_conn_zone $server_name zone=myServerName:10m; } server { location / { limit_conn myip 10; limit_conn myServerName 100; rewrite / http://www.rumenz.net permanent; } } 上面配置了单个IP同时并发连接数最多只能10个连接,并且设置了整个虚拟服务器同时最大并发数最多只能100个链接。当然,只有当请求的header被服务器处理后,虚拟服务器的连接数才会计数。刚才有提到过Nginx是基于漏桶算法原理实现的,实际上限流一般都是基于漏桶算法和令牌桶算法实现的。漏桶流算法和令牌桶算法知道?一文搞定,手撸Springboot + aop + Lua分布式限流的最佳实践_vincent-CSDN博客_springboot如何限流漏桶算法:漏桶算法思路很简单,我们把水比作是请求,漏桶比作是系统处理能力极限,水先进入到漏桶里,漏桶里的水按一定速率流出,当流出的速率小于流入的速率时,由于漏桶容量有限,后续进入的水直接溢出(拒绝请求),以此实现限流。springboot + aop + Lua分布式限流的最佳实践令牌桶算法:令牌桶算法的原理也比较简单,我们可以理解成医院的挂号看病,只有拿到号以后才可以进行诊病。系统会维护一个令牌(token)桶,以一个恒定的速度往桶里放入令牌(token),这时如果有请求进来想要被处理,则需要先从桶里获取一个令牌(token),当桶里没有令牌(token)可取时,则该请求将被拒绝服务。令牌桶算法通过控制桶的容量、发放令牌的速率,来达到对请求的限制。springboot + aop + Lua分布式限流的最佳实践Nginx配置高可用性怎么配置?当上游服务器(真实访问服务器),一旦出现故障或者是没有及时相应的话,应该直接轮训到下一台服务器,保证服务器的高可用Nginx配置代码:server { listen 80; server_name www.rumenz.com; location / { ### 指定上游服务器负载均衡服务器 proxy_pass http://backServer; ###nginx与上游服务器(真实访问的服务器)超时时间 后端服务器连接的超时时间_发起握手等候响应超时时间 proxy_connect_timeout 1s; ###nginx发送给上游服务器(真实访问的服务器)超时时间 proxy_send_timeout 1s; ### nginx接受上游服务器(真实访问的服务器)超时时间 proxy_read_timeout 1s; index index.html index.htm; } } Nginx怎么判断别IP不可访问?# 如果访问的ip地址为11115,则返回403 if ($remote_addr = 11115) { return 403; } 在nginx中,如何使用未定义的服务器名称来阻止处理请求?只需将请求删除的服务器就可以定义为:服务器名被保留一个空字符串,他在没有主机头字段的情况下匹配请求,而一个特殊的nginx的非标准代码被返回,从而终止连接。怎么限制浏览器访问?## 不允许谷歌浏览器访问 如果是谷歌浏览器返回500 if ($http_user_agent ~ Chrome) { return 500; } Rewrite全局变量是什么?$remote_addr //获取客户端ip $binary_remote_addr //客户端ip(二进制) $remote_port //客户端port,如:50472 $remote_user //已经经过Auth Basic Module验证的用户名 $host //请求主机头字段,否则为服务器名称,如:blog.sakmon.com $request //用户请求信息,如:GET ?a=1&b=2 HTTP/1.1 $request_filename //当前请求的文件的路径名,由root或alias和URI request组合而成,如:/2013/81.html $status //请求的响应状态码,如:200 $body_bytes_sent // 响应时送出的body字节数数量。即使连接中断,这个数据也是精确的,如:40 $content_length // 等于请求行的“Content_Length”的值 $content_type // 等于请求行的“Content_Type”的值 $http_referer // 引用地址 $http_user_agent // 客户端agent信息,如:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36 $args //与$query_string相同 等于当中URL的参数(GET),如a=1&b=2 $document_uri //与$uri相同 这个变量指当前的请求URI,不包括任何参数(见$args) 如:/2013/81.html $document_root //针对当前请求的根路径设置值 $hostname //如:centos53.localdomain $http_cookie //客户端cookie信息 $cookie_COOKIE //cookie COOKIE变量的值 $is_args //如果有$args参数,这个变量等于”?”,否则等于”",空值,如? $limit_rate //这个变量可以限制连接速率,0表示不限速 $query_string // 与$args相同 等于当中URL的参数(GET),如a=1&b=2 $request_body // 记录POST过来的数据信息 $request_body_file //客户端请求主体信息的临时文件名 $request_method //客户端请求的动作,通常为GET或POST,如:GET $request_uri //包含请求参数的原始URI,不包含主机名,如:/2013/81.html?a=1&b=2 $scheme //HTTP方法(如http,https),如:http $uri //这个变量指当前的请求URI,不包括任何参数(见$args) 如:/2013/81.html $request_completion //如果请求结束,设置为OK. 当请求未结束或如果该请求不是请求链串的最后一个时,为空(Empty),如:OK $server_protocol //请求使用的协议,通常是HTTP/1.0或HTTP/1.1,如:HTTP/1.1 $server_addr //服务器IP地址,在完成一次系统调用后可以确定这个值 $server_name //服务器名称,如:blog.sakmon.com $server_port //请求到达服务器的端口号,如:80 Nginx 如何实现后端服务的健康检查?方式一,利用 nginx 自带模块 ngx_http_proxy_module 和 ngx_http_upstream_module 对后端节点做健康检查。方式二(推荐),利用nginx_upstream_check_module 模块对后端节点做健康检查。Nginx 如何开启压缩?开启nginx gzip压缩后,网页、css、js等静态资源的大小会大大的减少,从而可以节约大量的带宽,提高传输效率,给用户快的体验。虽然会消耗cpu资源,但是为了给用户更好的体验是值得的。开启的配置如下:将以上配置放到nginx.conf的http{ … }节点中。http { # 开启gzip gzip on; # 启用gzip压缩的最小文件;小于设置值的文件将不会被压缩 gzip_min_length 1k; # gzip 压缩级别 1-10 gzip_comp_level 2; # 进行压缩的文件类型。 gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; # 是否在http header中添加Vary: Accept-Encoding,建议开启 gzip_vary on; } 保存并重启nginx,刷新页面(为了避免缓存,请强制刷新)就能看到效果了。以谷歌浏览器为例,通过F12看请求的响应头部:我们可以先来对比下,如果我们没有开启zip压缩之前,我们的对应的文件大小,如下所示:现在我们开启了gzip进行压缩后的文件的大小,可以看到如下所示:gzip压缩前后效果对比:jquery原大小90kb,压缩后只有30kb。gzip虽然好用,但是以下类型的资源不建议启用。 1、图片类型原因:图片如jpg、png本身就会有压缩,所以就算开启gzip后,压缩前和压缩后大小没有多大区别,所以开启了反而会白白的浪费资源。(Tips:可以试试将一张jpg图片压缩为zip,观察大小并没有多大的变化。虽然zip和gzip算法不一样,但是可以看出压缩图片的价值并不大) 2、大文件原因:会消耗大量的cpu资源,且不一定有明显的效果。 ngx_http_upstream_module的作用是什么?ngx_http_upstream_module用于定义可通过fastcgi传递、proxy传递、uwsgi传递、memcached传递和scgi传递指令来引用的服务器组。什么是C10K问题?C10K问题是指无法同时处理大量客户端(10,000)的网络套接字。Nginx是否支持将请求压缩到上游?您可以使用Nginx模块gunzip将请求压缩到上游。gunzip模块是一个过滤器,它可以对不支持gzip编码方法的客户机或服务器使用内容编码:gzip来解压缩响应。如何在Nginx中获得当前的时间?要获得Nginx的当前时间,必须使用SSI模块、和date_local的变量。Proxy_set_header THE-TIME $date_gmt;用Nginx服务器解释-s的目的是什么?用于运行Nginx -s参数的可执行文件。如何在Nginx服务器上添加模块?在编译过程中,必须选择Nginx模块,因为Nginx不支持模块的运行时间选择。生产中如何设置worker进程的数量呢?在有多个cpu的情况下,可以设置多个worker,worker进程的数量可以设置到和cpu的核心数一样多,如果在单个cpu上起多个worker进程,那么操作系统会在多个worker之间进行调度,这种情况会降低系统性能,如果只有一个cpu,那么只启动一个worker进程就可以了。nginx状态码 499:服务端处理时间过长,客户端主动关闭了连接。 502 服务器错误下面是502的一些可能性 (1).FastCGI进程是否已经启动(2).FastCGI worker进程数是否不够(3).FastCGI执行时间过长fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; (4).FastCGI Buffer不够,nginx和apache一样,有前端缓冲限制,可以调整缓冲参数 fastcgi_buffer_size 32k; fastcgi_buffers 8 32k; (5). Proxy Buffer不够,如果你用了Proxying,调整 proxy_buffer_size 16k; proxy_buffers 4 16k 转载引用出处https://www.toutiao.com/i7074868404070662693/?tt_from=weixin&utm_campaign=client_share&wxshare_count=1×tamp=1647313048&app=news_article&utm_source=weixin&utm_medium=toutiao_android&use_new_style=1&req_id=2022031510572701021004914907E6D22A&share_token=0ff6a301-6b03-4ebe-ae13-2bb074b93cf1&group_id=7074868404070662693
2022年03月15日
161 阅读
0 评论
0 点赞