初级后端的疑惑,如何估算接口 qps,以及 redis 占用多少容量, nginx 能抗多少并发

如题,参与公司业务开发中,经常会遇到这样的问题:

这个业务入口会为接口带来多少的 qps 增长?
这个接口能抗住多少 qps ?
这个业务要上缓存的话,预计会带来多少缓存占用?
现有的 redis 能抗多少并发?内存占用是否过高?是否需要增加机器?
现有的 nginx 集群,能抗住多少并发?是否需要增加机器?
业务上线预计会带来 1000qps 的增长,服务器资源(接口,缓存,数据库)是否能扛得住?
这个业务的性能瓶颈在哪里?怎么查出来? 等等
总结的问题就是,大佬们是如何进行业务的容量评估,性能评估,性能排查的?

希望能有大大能逐点解答一下上面的 7 个问题你们在工作中是怎么去分析的,身为菜鸟的我每次遇到这种问题,都头痛半天,然后还是去问大佬怎么怎么弄,但是几次下来也没有总结到套路,都快怀疑自己适不适合干下去了…. 所以想向各位请教下,学习一下大家都是怎么评估和排查问题的,想在这方面有点成长,万分感谢!

第 1 条附言 · 1 小时 30 分钟前
看到有这么多人默默收藏了,就说明了其实还是有很多人关心这些问题的,看到这个问题的大佬们,有经验的希望还是能尽量分享下呀~ 求求了
qps 并发 Nginx Redis22 条回复 • 2021-06-21 16:24:07 +08:00
tachikomachann 1
tachikomachann 3 小时 9 分钟前 via Android
之前看《 java performance definitive guide 》时,作者在*章说过一句话。大概意思是很多人以为 java 调优就是这样一个按钮,我按下去了调优就完成了。实际上不是的,一个服务的性能瓶颈需要具体问题具体分析,需要结合业务场景,压测结果,profile 工具等一点一点地去分析。然后根据自己需要的性能目标和成本去得出一个折中方案。

lz 可以先看看相关的书籍,学习用一些基本工具(大厂甚至都有一整套自己的工具)和大牛们做性能分析的思路。然后在到自己项目里实践看看。
whcoding 2
whcoding 3 小时 7 分钟前
可以买阿里云的压测得到你想要的数据.
waibunleung 3
waibunleung 3 小时 1 分钟前
@tachikomachann 我知道肯定会有人说具体问题具体分析,但是问题都是是有分析方向的,就比如你怎么估计一个在服务首页下面增加一个功能入口,新业务增加多少 qps 呢?如果你通过接口监控,知道了这个首页的平均 qps 是 1000,运营告诉你,页面底部的点击率是 25%,那预计带来的 qps 就大约是 250qps,这个 qps 不算高,接口逻辑不复杂的话就能轻松扛过去等等

上面说的 转化率就是一个评估 qps 的方向。
导致性能瓶颈的问题有很多,但是排查瓶颈肯定是有套路的

至于你说学习大牛们做性能分析的思路,提出这个问题的我,就是希望能再这里收获一点思路。

感谢回复啦~
waibunleung 4
waibunleung 3 小时 0 分钟前
@whcoding 公司自建机房

想要问题分析的思路和套路,阿里云并不能告诉我预计业务会增长多少 qps…
dream4ever 5
dream4ever 2 小时 2 分钟前
之前也思考过这个问题,在*客时间买了几门架构设计方面的课,有些课程会讲到这类数据,比如在硬件配置不是瓶颈的情况下,nginx 能扛住多少并发,redis 能扛住多少并发之类的数据,可以看看。
dream4ever 6
dream4ever 2 小时 2 分钟前
而且也会讲如何排查性能瓶颈的方法。
janxin 7
janxin 1 小时 42 分钟前
估算都不知道瓶颈在哪怎么估算 orz

所以先做压测找到瓶颈
jmtung 8
jmtung 1 小时 35 分钟前
1 、性能:压测
2 、redis 内存占用: http://www.redis.cn/redis_memory
waibunleung 9
waibunleung 1 小时 31 分钟前
@dream4ever 请问买的是哪几门课程呢?
chenqh 10
chenqh 1 小时 28 分钟前
能上 100QPS 都好事了,

waibunleung 11
waibunleung 1 小时 26 分钟前
@jmtung 卧槽,有这个 redis 的预估神器?它估算是怎么估算的?
Maboroshii 12
Maboroshii 1 小时 25 分钟前 via Android
如果不是那种用户量特别大的服务,就随便搞一个配置,先上线,再观察调整也来得及,就能慢慢积累经验了
waibunleung 13
waibunleung 1 小时 23 分钟前
@Maboroshii 就是那种用户量大的服务
zed1018 14
zed1018 1 小时 21 分钟前
jmeter 可以压测
Maboroshii 15
Maboroshii 1 小时 17 分钟前
@waibunleung #13 压测是一个方案,不过具体还是依赖运营数据和现有数据对比,再考虑配置问题。 总有一个人有经验,任何项目也不是一上线就用户量爆炸的。
iyaozhen 16
iyaozhen 1 小时 6 分钟前
这个问题比较复杂。
往往大家只是说做个压测,但压测*难的不是 jmeter 啥的使用,而是压测场景的分析。

这个之前内部写了个文档,需要再重新写个,可能能回答楼主部分问题
waibunleung 17
waibunleung 56 分钟前
@Maboroshii 压测确实是一个方案,但是肯定有分析技巧和套路的
X0ray 18
X0ray 44 分钟前
性能评估看压测,
性能排查要做好 metrics 监控,如果有异常了,一拉图标出来很快就能反映出实际情况。
fantastM 19
fantastM 43 分钟前
1 和 2 应该都是先由产品 /运营给出一个预估的用户量,然后通过应用当前的部署情况(比如负载均衡了多少台机器,单台机器的配置,应用运行时的配置,接口的响应时间)估算出接口的 QPS 。
3 sizeof 可以算占用量,不过和具体缓存的数据有关,#8 提到的网站就挺不错。
4 单机的话,可以用 redis-benchmark 跑下看看。
7 压测时候看下各个调用链路里的耗时(或者更细一点的,可以自己打印 log ),还有外部依赖的监控指标等等,出现问题的话,总能看出一些端倪。
dream4ever 20
dream4ever 35 分钟前
@waibunleung 《架构实战案例解析》,《许式伟的架构课》,《从 0 开始学架构》,但是上面说的内容具体在哪个专栏里现在印象不深了。
waibunleung 21
waibunleung 24 分钟前
@iyaozhen 大佬可以简单回答一下,然后再把写好的文档分享一下~
waibunleung 22
waibunleung 23 分钟前
@fantastM 太棒了,就是需要类似这样的回复!还有请问下,类似于 4k qps mysql 能不能扛得住,会不会报警这种问题,要怎么思考呢?

nginx反向代理模块

nginx的反向代理模块有很多种配置,下面介绍一些常用的配置实例:

1.proxy_pass

语法:   proxy_pass URL
配置块:   location,if
详解:此配置项将当前请求反向代理到URL参数指定的服务器上,URL可以是主机名或者IP地址加端口的形式。例如:

  1. // nginx.conf配置文件
  2. // 配置URL地址
  3. proxy_pass http://www.54rd.net/html/webserver/;
  4. // 也可以配置unix句柄
  5. proxy_pass http://unix:/path/to/backend.sock:/webserver/;
  6. // 也可以把HTTP转换成更安全的HTTPS
  7. proxy_pass https://192.168.0.1;

默认情况下反向代理是不会转发请求中的Host头部。如果需要转发,那么必须加上set_header配置:

  1. proxy_set_header Host $host;

2.proxy_method

语法:proxy_method method;
配置块:http,server,location
详解:此配置项表示转发时的协议方法名,例如:

  1. // 配置后客户端发来的GET请求在转发时方法名也会改为POST
  2. proxy_method POST;

3.proxy_hide_header

语法:proxy_hide_header the_header;
配置块:http,server,location
详解:nginx会将上游服务器的响应转发给客户端,但默认不会转发以下HTTP头部字段:Date,Server,X-Pad和X-Accel-*。使用proxy_hide_header后可以任意指定哪些HTTP头部字段不能被转发。例如:

  1. // 例如不转发缓存控制
  2. proxy_hide_header Cache-Control;

4.proxy_pass_header

语法:proxy_pass_header the_header;
配置块:http,server,location
详解:于proxy_hide_header功能相反,proxy_pass_header会将原来禁止转发的header设置成允许转发。例如:

  1. // 允许重定向
  2. proxy_pass_header X-Accel-Redirect;

5.proxy_pass_request_body

语法:proxy_pass_request_body on|off;
默认:proxy_pass_request_body on;
配置块:http,server,location
详解:作用为确定是否向上游服务器发送HTTP包体部分。

6.proxy_pass_request_headers

语法:proxy_pass_request_headers on|off;
默认:proxy_pass_request_headers on;
配置块:http,server,location
详解:作用为确定是否转发HTTP头部。

7.proxy_redirect

语法:proxy_redirect [default|off|redirect replacement];
默认:proxy_redirect default;
配置块:http,server,location
详解:当上游服务器返回的响应时重定向或者刷新请求(如HTTP响应码是302或者301)时,proxy_redirect可以重设HTTP头部的location或refresh字段,例如:

  1. // 如果上游服务器发出的响应码是302,location字段的URL是http://www.54rd.net/html/webserver/,那么实际会转发到http://www.54rd.net/html/php/
  2. proxy_redirect http://www.54rd.net/html/webserver/ http://www.54rd.net/html/php/;
  3. // 还可以使用ngx-http-core-module提供的变量来设置
  4. proxy_redirect http://www.54rd.net/html/webserver/ http://$host:$server_port/;
  5. // 也可以省略repalcement参数中的主机部分,这时会用虚拟主机名称来填充
  6. proxy_redirect http://www.54rd.net/html/webserver/ /html/php/;

======================

在使用nginx的反向代理功能时,有时会出现重定向的url不是我想要的url,例如下面的例子:前端的Nginx负责把http: www 54rd net yum Server 开头的url反向代理到后端的http: 192 168 1 1 Server 上。对于有完整
在使用nginx的反向代理功能时,有时会出现重定向的url不是我想要的url,例如下面的例子:

前端的Nginx负责把http://www.54rd.net/yum/Server/开头的url反向代理到后端的http://192.168.1.1/Server/上。

对于有完整的路径,如http://www.54rd.net/yum/Server/的代理没有问题,Server对应后台服务器的一个目录。

但当访问  http://www.54rd.net/yum/Server时,后端Nginx会发送一个301到/上,于是返回到前端后URL变成了 http://www.54rd.net/Server/,这个url显然不是我们想要的。

在Apache中有个ProxyPassReverse的参数,用来调整反向代理服务器发送的http应答头的url,可以解决这个问题。

查了Nginx的手册后,终于发现了proxy_redirect这个参数,它实现的功能和ProxyPassReverse类似,例如增加如下配置:

  1. location ^~ /yum
  2. {
  3.             proxy_pass http://192.168.1.1/;
  4.             proxy_redirect http://www.54rd.net/ /yum/;
  5. }

这样,当访问http://www.54rd.net/yum/Server后,就会301到http://www.54rd.net/yum/Server/上了。

====================

8.proxy_next_upstream

语法:proxy_next_upstream [error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off];
默认:proxy_next_upstream error timeout;
配置块:http,server,location
详解:此配置项表示当向一台上游服务器转发请求出现错误时,继续换一台上游服务器处理这个请求,这样可以更好的保证客户端只收到来自一个上游服务器的应答。proxy_next_upstream的参数用来说明在哪些情况下会继续选择下一台上游服务器转发请求:

  1. error:当向上游服务器发起连接,发送请求,读取响应时出错时。
  2. timeout:发送请求或者读取响应发生超时时。
  3. invalid_header:上游服务器发送的响应时不合法时。
  4. http_500:上游服务器返回的HTTP响应码是500时。
  5. http_502:上游服务器返回的HTTP响应码是502时。
  6. http_503:上游服务器返回的HTTP响应码是503时。
  7. http_504:上游服务器返回的HTTP响应码是504时。
  8. http_404:上游服务器返回的HTTP响应码是404时。
  9. off:关闭proxy_next_upstream 功能一出错就选择另一台上游服务器再次转发。

小结:nginx的反向代理使用起来还是很方便的,适当的修改配置即可。

nginx配置调优

一、一般来说nginx 配置文件中对优化比较有作用的为以下几项:

1. worker_processes 8;

nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数 (如,2个四核的cpu计为8)。

2. worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

为每个进程分配cpu,上例中将8 个进程分配到8 个cpu,当然可以写多个,或者将一
个进程分配到多个cpu。

3. worker_rlimit_nofile 65535;

这个指令是指当一个nginx 进程打开的*多文件描述符数目,理论值应该是*多打开文
件数(ulimit -n)与nginx 进程数相除,但是nginx 分配请求并不是那么均匀,所以*好与ulimit -n 的值保持一致。

现在在linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应应该填写65535。

这是因为nginx调度时分配请求到进程并不是那么的均衡,所以假如填写10240,总并发量达到3-4万时就有进程可能超过10240了,这时会返回502错误。

查看linux系统文件描述符的方法:

[root@web001 ~]# sysctl -a | grep fs.file

fs.file-max = 789972

fs.file-nr = 510 0 789972

4. use epoll;

使用epoll 的I/O 模型

(

补充说明:

与apache相类,nginx针对不同的操作系统,有不同的事件模型

A)标准事件模型
Select、poll属于标准事件模型,如果当前系统不存在更有效的方法,nginx会选择select或poll
B)高效事件模型
Kqueue:使用于 FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X. 使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。
Epoll: 使用于Linux内核2.6版本及以后的系统。

 

/dev/poll:使用于 Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。

Eventport:使用于 Solaris 10. 为了防止出现内核崩溃的问题, 有必要安装安全补丁。

)

5. worker_connections 65535;

每个进程允许的*多连接数, 理论上每台nginx 服务器的*大连接数为worker_processes*worker_connections。

6. keepalive_timeout 60;

keepalive 超时时间。

7. client_header_buffer_size 4k;

客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。

分页大小可以用命令getconf PAGESIZE 取得。

[root@web001 ~]# getconf PAGESIZE

4096

但也有client_header_buffer_size超过4k的情况,但是client_header_buffer_size该值必须设置为“系统分页大小”的整倍数。

8. open_file_cache max=65535 inactive=60s;

这个将为打开文件指定缓存,默认是没有启用的,max 指定缓存数量,建议和打开文件数一致,inactive 是指经过多长时间文件没被请求后删除缓存。

9. open_file_cache_valid 80s;

这个是指多长时间检查一次缓存的有效信息。

10. open_file_cache_min_uses 1;

open_file_cache 指令中的inactive 参数时间内文件的*少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive 时间内一次没被使用,它将被移除。

 

二、关于内核参数的优化:

net.ipv4.tcp_max_tw_buckets = 6000

timewait 的数量,默认是180000。

net.ipv4.ip_local_port_range = 1024 65000

允许系统打开的端口范围。

net.ipv4.tcp_tw_recycle = 1

启用timewait 快速回收。

net.ipv4.tcp_tw_reuse = 1

开启重用。允许将TIME-WAIT sockets 重新用于新的TCP 连接。

net.ipv4.tcp_syncookies = 1

开启SYN Cookies,当出现SYN 等待队列溢出时,启用cookies 来处理。

net.core.somaxconn = 262144

web 应用中listen 函数的backlog 默认会给我们内核参数的net.core.somaxconn 限制到128,而nginx 定义的NGX_LISTEN_BACKLOG 默认为511,所以有必要调整这个值。

net.core.netdev_max_backlog = 262144

每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的*大数目。

net.ipv4.tcp_max_orphans = 262144

系统中*多有多少个TCP 套接字不被关联到任何一个用户文件句柄上。如果超过这个数字,孤儿连接将即刻被复位并打印出警告信息。这个限制仅仅是为了防止简单的DoS 攻击,不能过分依靠它或者人为地减小这个值,更应该增加这个值(如果增加了内存之后)。

net.ipv4.tcp_max_syn_backlog = 262144

记录的那些尚未收到客户端确认信息的连接请求的*大值。对于有128M 内存的系统而言,缺省值是1024,小内存的系统则是128。

net.ipv4.tcp_timestamps = 0

时间戳可以避免序列号的卷绕。一个1Gbps 的链路肯定会遇到以前用过的序列号。时间戳能够让内核接受这种“异常”的数据包。这里需要将其关掉。

net.ipv4.tcp_synack_retries = 1

为了打开对端的连接,内核需要发送一个SYN 并附带一个回应前面一个SYN 的ACK。也就是所谓三次握手中的第二次握手。这个设置决定了内核放弃连接之前发送SYN+ACK 包的数量。

net.ipv4.tcp_syn_retries = 1

在内核放弃建立连接之前发送SYN 包的数量。

net.ipv4.tcp_fin_timeout = 1

如 果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2 状态的时间。对端可以出错并永远不关闭连接,甚至意外当机。缺省值是60 秒。2.2 内核的通常值是180 秒,3你可以按这个设置,但要记住的是,即使你的机器是一个轻载的WEB 服务器,也有因为大量的死套接字而内存溢出的风险,FIN- WAIT-2 的危险性比FIN-WAIT-1 要小,因为它*多只能吃掉1.5K 内存,但是它们的生存期长些。

net.ipv4.tcp_keepalive_time = 30

当keepalive 起用的时候,TCP 发送keepalive 消息的频度。缺省是2 小时。

 

三、下面贴一个完整的内核优化设置:

vi /etc/sysctl.conf CentOS5.5中可以将所有内容清空直接替换为如下内容:

net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000

使配置立即生效可使用如下命令:
/sbin/sysctl -p

四、下面是关于系统连接数的优化

linux 默认值 open files 和 max user processes 为 1024

#ulimit -n

1024

#ulimit Cu

1024

问题描述: 说明 server 只允许同时打开 1024 个文件,处理 1024 个用户进程

使用ulimit -a 可以查看当前系统的所有限制值,使用ulimit -n 可以查看当前的*大打开文件数。

新装的linux 默认只有1024 ,当作负载较大的服务器时,很容易遇到error: too many open files 。因此,需要将其改大。

解决方法:

使用 ulimit Cn 65535 可即时修改,但重启后就无效了。(注ulimit -SHn 65535 等效 ulimit -n 65535 ,-S 指soft ,-H 指hard)

有如下三种修改方式:

1. 在/etc/rc.local 中增加一行 ulimit -SHn 65535
2. 在/etc/profile 中增加一行 ulimit -SHn 65535
3. 在/etc/security/limits.conf *后增加:

* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535

具体使用哪种,在 CentOS 中使用第1 种方式无效果,使用第3 种方式有效果,而在Debian 中使用第2 种有效果

# ulimit -n

65535

# ulimit -u

65535

备注:ulimit 命令本身就有分软硬设置,加-H 就是硬,加-S 就是软默认显示的是软限制

soft 限制指的是当前系统生效的设置值。 hard 限制值可以被普通用户降低。但是不能增加。 soft 限制不能设置的比 hard 限制更高。 只有 root 用户才能够增加 hard 限制值。

 

五、下面是一个简单的nginx 配置文件:

user www www;
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000
01000000;
error_log /www/log/nginx_error.log crit;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 204800;
events
{
use epoll;
worker_connections 204800;
}
http
{
include mime.types;
default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 2k;
large_client_header_buffers 4 4k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2
keys_zone=TEST:10m
inactive=5m;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 4k;
fastcgi_buffers 8 4k;
fastcgi_busy_buffers_size 8k;
fastcgi_temp_file_write_size 8k;
fastcgi_cache TEST;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
open_file_cache max=204800 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server
{
listen 8080;
server_name backup.aiju.com;
index index.php index.htm;
root /www/html/;
location /status
{
stub_status on;
}
location ~ .*/.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location ~ .*/.(gif|jpg|jpeg|png|bmp|swf|js|css)$
{
expires 30d;
}
log_format access ‘$remote_addr — $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” $http_x_forwarded_for’;
access_log /www/log/access.log access;
}
}

六、关于FastCGI 的几个指令:

fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10minactive=5m;

这个指令为FastCGI 缓存指定一个路径,目录结构等级,关键字区域存储时间和非活动删除时间。

fastcgi_connect_timeout 300;

指定连接到后端FastCGI 的超时时间。

fastcgi_send_timeout 300;

向FastCGI 传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI 传送请求的超时时间。

fastcgi_read_timeout 300;

接收FastCGI 应答的超时时间,这个值是指已经完成两次握手后接收FastCGI 应答的超时时间。

fastcgi_buffer_size 4k;

指定读取FastCGI 应答*部分需要用多大的缓冲区,一般*部分应答不会超过1k,由于页面大小为4k,所以这里设置为4k。

fastcgi_buffers 8 4k;

指定本地需要用多少和多大的缓冲区来缓冲FastCGI 的应答。

fastcgi_busy_buffers_size 8k;

这个指令我也不知道是做什么用,只知道默认值是fastcgi_buffers 的两倍。

fastcgi_temp_file_write_size 8k;

在写入fastcgi_temp_path 时将用多大的数据块,默认值是fastcgi_buffers 的两倍。

fastcgi_cache TEST

开启FastCGI 缓存并且为其制定一个名称。个人感觉开启缓存非常有用,可以有效降低CPU 负载,并且防止502 错误。

fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;

为指定的应答代码指定缓存时间,如上例中将200,302 应答缓存一小时,301 应答缓存1 天,其他为1 分钟。

fastcgi_cache_min_uses 1;

缓存在fastcgi_cache_path 指令inactive 参数值时间内的*少使用次数,如上例,如果在5 分钟内某文件1 次也没有被使用,那么这个文件将被移除。

fastcgi_cache_use_stale error timeout invalid_header http_500;

不知道这个参数的作用,猜想应该是让nginx 知道哪些类型的缓存是没用的。以上为nginx 中FastCGI 相关参数,另外,FastCGI 自身也有一些配置需要进行优化,如果你使用php-fpm 来管理FastCGI,可以修改配置文件中的以下值:

<value name=”max_children”>60</value>

同时处理的并发请求数,即它将开启*多60 个子线程来处理并发连接。

<value name=”rlimit_files”>102400</value>

*多打开文件数。

<value name=”max_requests”>204800</value>

每个进程在重置之前能够执行的*多请求数。

nginx的几个超时时间

nginx比较强大,可以针对单个域名请求做出单个连接超时的配置.

比如些动态解释和静态解释可以根据业务的需求配置

proxy_connect_timeout :后端服务器连接的超时时间_发起握手等候响应超时时间

proxy_read_timeout:连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)

proxy_send_timeout :后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据

nginx使用proxy模块时,默认的读取超时时间是60s。

 

 

1、请求超时

 

http {
    include       mime.types;
    server_names_hash_bucket_size  512;     
    default_type  application/octet-stream;
    sendfile        on;

    keepalive_timeout  65;  #保持
    tcp_nodelay on;
    client_header_timeout 15;
    client_body_timeout 15;
    send_timeout 25;
    include vhosts/*.conf;
}

复制代码

 

 

2、后端服务器处理请求的时间设置(页面等待服务器响应时间)

location / {
        ...
        proxy_read_timeout 150;  # 秒
        ...
    }

 

 

 

 

 

 

nginx常用的超时配置说明

client_header_timeout

语法 client_header_timeout time
默认值 60s
上下文 http server
说明 指定等待client发送一个请求头的超时时间(例如:GET / HTTP/1.1).仅当在一次read中,没有收到请求头,才会算成超时。如果在超时时间内,client没发送任何东西,nginx返回HTTP状态码408(“Request timed out”)

client_body_timeout 

语法 client_body_timeout time
默认值 60s
上下文 http server location
说明 该指令设置请求体(request body)的读超时时间。仅当在一次readstep中,没有得到请求体,就会设为超时。超时后,nginx返回HTTP状态码408(“Request timed out”)

keepalive_timeout 

语法 keepalive_timeout timeout [ header_timeout ]
默认值 75s
上下文 http server location
说明 *个参数指定了与client的keep-alive连接超时时间。服务器将会在这个时间后关闭连接。可选的第二个参数指定了在响应头Keep-Alive: timeout=time中的time值。这个头能够让一些浏览器主动关闭连接,这样服务器就不必要去关闭连接了。没有这个参数,nginx不会发送Keep-Alive响应头(尽管并不是由这个头来决定连接是否“keep-alive”)
两个参数的值可并不相同

  • 注意不同浏览器怎么处理“keep-alive”头
  • MSIE和Opera忽略掉”Keep-Alive: timeout=<N>” header.
  • MSIE保持连接大约60-65秒,然后发送TCP RST
  • Opera永久保持长连接
  • Mozilla keeps the connection alive for N plus about 1-10 seconds.
  • Konqueror保持长连接N秒

lingering_timeout

语法 lingering_timeout time
默认值 5s
上下文 http server location
说明 lingering_close生效后,在关闭连接前,会检测是否有用户发送的数据到达服务器,如果超过lingering_timeout时间后还没有数据可读,就直接关闭连接;否则,必须在读取完连接缓冲区上的数据并丢弃掉后才会关闭连接。

resolver_timeout

语法 resolver_timeout time
默认值 30s
上下文 http server location
说明 该指令设置DNS解析超时时间

proxy_connect_timeout

语法 proxy_connect_timeout time
默认值 60s
上下文 http server location
说明 该指令设置与upstream server的连接超时时间,有必要记住,这个超时不能超过75秒。
这个不是等待后端返回页面的时间,那是由proxy_read_timeout声明的。如果你的upstream服务器起来了,但是hanging住了(例如,没有足够的线程处理请求,所以把你的请求放到请求池里稍后处理),那么这个声明是没有用的,由于与upstream服务器的连接已经建立了。

proxy_read_timeout

语法 proxy_read_timeout time
默认值 60s
上下文 http server location
说明 该指令设置与代理服务器的读超时时间。它决定了nginx会等待多长时间来获得请求的响应。这个时间不是获得整个response的时间,而是两次reading操作的时间。

proxy_send_timeout

语法 proxy_send_timeout time
默认值 60s
上下文 http server location
说明 这个指定设置了发送请求给upstream服务器的超时时间。超时设置不是为了整个发送期间,而是在两次write操作期间。如果超时后,upstream没有收到新的数据,nginx会关闭连接

proxy_upstream_fail_timeout(fail_timeout)

语法 server address [fail_timeout=30s]
默认值 10s
上下文 upstream
说明 Upstream模块下 server指令的参数,设置了某一个upstream后端失败了指定次数(max_fails)后,该后端不可操作的时间,默认为10秒

nginx静态资源服务器简单配置

传统的web项目,一般都将静态资源存放在 webroot的目录下,这样做很方便获取静态资源,但是如果说web项目很大,用户很多,静态资源也很多时,服务器的性能 或许就会很低下了。这种情况下一般都会需要一个静态资源的服务器。

搭建nginx服务器首先得安装nginx服务,关于nginx服务的安装可以参考我的另一篇博客《nginx服务安装》这里直接介绍静态服务器的配置
进入nginx安装目录的conf目录下,修改nginx.conf文件,在一个server{}中添加 一个location 部分配置代码如下

root@ubuntu:/usr/local/nginx/conf# vi nginx.conf
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /image/ {
root /usr/local/myImage/;
autoindex on;
}

}

从上面的配置可以看出来 端口为80,server_name为localhost(写ip地址也可以)

location /image/ {
root /usr/local/myImage/;
autoindex on;
}

这个配置表示输入 localhost:80/image/ 时会访问本机的/usr/local/myImage/image/ 目录。所以要新建/usr/local/myImage/image/ 目录,同时还要在nginx安装目录的html目录中新建一个 与 location中 image同名的image目录,虽然该目录里面什么也没有,在/usr/local/my Image/image/ 中我们放一张图片1.jpg上去,重启nginx服务,就可以通过 localhost:80/image/1.jpg访问了

root@ubuntu:/usr/local/nginx/html# mkdir image

root@ubuntu:/usr/local/nginx/html# mkdir /usr/local/myImage/image
#放一张照片上去#
root@ubuntu:/usr/local/nginx/html# cd /usr/local/myImage/image
root@ubuntu:/usr/local/myImage/image# ls
1.jpg
root@ubuntu:/usr/local/myImage/image#

重启 nginx

root@ubuntu:/usr/local/nginx/sbin# ./nginx -s reload
root@ubuntu:/usr/local/nginx/sbin#

打开浏览器 输入 server_name:80/image/1.jpg 就可以访问该静态图片了

您也可以移除掉root 和 autoindex 配置,直接在html目录下的image目录下新建一张图片1.jpg。

或者在nginx.conf 配置中在server下配置root 如下

server {
listen 88;
server_name localhost;
root /home/ubuntu/static/;
}

访问 http:hostName:88/dir/file 就会自动访问 服务器/home/ubuntu/static/dir 目录下的文件file

注意nginx的启动用户,确保该用户有权限访问目录下的文件
新版本nginx的启动用户为 www-data!

nginx搭建基于http协议的视频点播服务器

1,于由自己的服务器上已经安装好nginx(具体安装方法见我的另一篇文章,Linux中安装nginx),所以不再安装。

2,下载nginx_mod_h264_streaming-2.2.7.tar.gz(自己在网上搜吧)。

3,安装pcre,先看有没有安装。

 

[root@localhost vsftpd]# rpm -qa pcre
pcre-7.8-7.el6.i686

已经安装(上篇加rtmp模块的时候已经安装了)

4,解压nginx_mod_h264_streaming-2.2.7.tar.gz,添加flv,mp4模块。解压到和nginx源码同级的目录。然后./configure –add-module=../nginx_mod_h264_streaming-2.2.7 –with-http_flv_module –with-http_mp4_module
因为flv 和http是nginx自带的所以不用下载这个模块。

5,make
可能遇到问题1
/root/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c: In function ‘ngx_streaming_handler’:
/root/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c:158: error: ‘ngx_http_request_t’ has no member named ‘zero_in_uri’
make[1]: *** [objs/addon/src/ngx_http_h264_streaming_module.o] Error 1
make[1]: Leaving directory `/root/nnginx-1.5.9′
make: *** [build] Error 2
那么将src/ngx_http_streaming_module.c文件中以下代码删除或者是注释掉就可以了

6, make install

7,配置 nginx.conf
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
worker_connections  1024;
}

#include  nginx.conf-rtmp;

http {

include       mime.types;

default_type  application/octet-stream;

log_format main  ‘$remote_addr – $remote_user [$time_local] ‘

‘”$request” $status $bytes_sent ‘

‘”$http_referer” “$http_user_agent” ‘

‘”$gzip_ratio”‘;

keepalive_timeout  60;

server_names_hash_bucket_size  128;

client_header_buffer_size    32k;

 

large_client_header_buffers  4 32k;

 

access_log off;

gzip on;

gzip_min_length  1100;

gzip_buffers     4 8k;

gzip_types       text/plain;

 

output_buffers   1 32k;

postpone_output  1460;

 

client_header_timeout  3m;

client_body_timeout    3m;

send_timeout           3m;

 

sendfile                on;

tcp_nopush              on;

tcp_nodelay             on;

 

######################################################################

 

server {

listen       8999;

server_name  192.168.1.104;

root    /usr/local/nginx/html/flv_file/;

limit_rate_after 5m;
limit_rate 512k;

index   index.html;

charset utf-8;

location ~ \.flv {

flv;

}

location ~ \.mp4$ {
mp4;
}

error_page   500 502 503 504  /50x.html;

location = /50x.html {

root   html;

}
}

}

8,启动nginx,创建 /usr/local/nginx/html/flv_file/目录。

9,上传mp4视频文件CY.mp4。

10,写个html测试。看之前的文章用jwplayer来测试,但是我这边一直报jwplayer udefined。

<!DOCTYPE html>
<div id=”container”>Loading the player …</div>
<video src=”http://192.168.1.104:8999/CY.mp4″ controls=”controls” width=”50%” height=”50%”>
</video>

将nginx作为视频点播服务器

流媒体服务器
流媒体指以流方式在网络中传送音频、视频和多媒体文件的媒体形式。相对于下载后观看的网络播放形式而言,流媒体的典型特征是把连续的音频和视频信息压缩后放到网络服务器上,用户边下载边观看,而不必等待整个文件下载完毕。由于流媒体技术的优越性,该技术广泛应用于视频点播、视频会议、远程教育、远程医疗和在线直播系统中。作为新一代互联网应用的标志,流媒体技术在近几年得到了飞速的发展。

流媒体播放方式
HTTP方式
这种方式要下载FLV视频文件到本地播放,一旦FLV视频文件下载完成,就不会消耗服务器的资源和带宽,但是拖动功能没有RTMP/RTMP流媒体方式强大,很多视频网站都是用HTTP方式实现的,如:YouTube,土豆,酷6等

RTMP/RTMP流媒体方式
这种方式不用下载FLV视频文件到本地,可以实时的播放flv文件,可以任意拖拽播放进度条,但是比较消耗服务器的资源。

安装
nginx 1.1.3之后已经默认支持mp4,flv模块,无须第三方模块支持。我这里使用的是docker容器:

docker run -d –name nginx -p 80:80 -v ~/opt/local/nginx/logs:/var/log/nginx -v ~/opt/local/nginx:/etc/nginx nginx
1
将容器内的nginx配置目录(即/etc/nginx)挂载到了本机目录方便我们对配置文件进行修改。

配置文件
在~/opt/local/nginx/conf.d下新建一个mp4.conf,加入如下配置

1 server {
2 listen 80 ;
3 server_name localhost;
4 root /etc/nginx/mp4;
5 limit_rate 256k;
6
7 location ~ \.flv$ {
8 flv;
9 }
10
11 location /video/ {
12 rewrite ^/video/(.*)$ /jwplayer-7.10.4/$1 last;
13 }
14
15 location ~ \.mp4$ {
16 mp4;
17 # mp4_buffer_size 1m;
18 # mp4_max_buffer_size 5m;
19 limit_rate_after 5m;
20 limit_rate 100k;
21 # limit_conn perip 1;
22 }
23 }

关于nginx的基本配置不再赘述,(可以参考我的博客nginx进阶-配置文件)将由flv与mp4结尾的uri交给对应的模块。随便找一个mp4或flv格式的视频放在root目录下(配置虚拟容器中的路径/etc/nginx/mp4,映射到真实机器的路径时~/opt/local/nginx/mp4)现在我们搭配jwplayer进行视频的点播。

下载jwplayer,我这里使用的7.10.4的免费版本,解压后得到如下目录
%title插图%num

这里注意两个文件,一个是jwplayer.flash.swf,还有一个就是jwplayer.js文件。官方已经贴心的为我们准备好了demo,打卡demo.html进行一些修改:

<script type=”text/javascript” src=”jwplayer.js”></script>
<script>jwplayer.key=”4sNBpozdkb2Gv+IYeWyd+CID9tW2NXdJE5GeSg==”;//修改为自己的key</script>
<div id=”player”>
Loading…
</div>
<script type=”text/javascript”>
jwplayer(“player”).setup({
“flashplayer”: “http://localhost/jwplayer.flash.swf”, //player.swf文件的uri
“file”: “http://localhost/1.mp4”,//视频文件的服务器uri
“aspectratio”: “16:9”,//播放器自适应比例
“height”: “360”,//播放器高度
“type”:”mp4″,//播放文件类型(可选)
“title”: “测试标题”,//标题(可选)
“description”: “测试视频描述”,//描述(可选)
“image”: “http://ww4.sinaimg.cn/large/b6839357jw1e3val80tknj20dw099jsb.jpg”,//视频封面(可选)
“repeat”:”true”,//重复播放(留空则不重复播放)
“autostart”:”true”,//自动播放(留空则不启用自动播放)
});
</script>

重点关注javascript标签中的内容,请配合注释一起食用。

效果
访问我们的nginx http://localhost/video/demo.html

%title插图%num

这样一个简单的视频点播服务器就完成了,其它的需求自行扩展即可。接下来有空会研究一下RTMP流媒体服务器的搭建(懒,逃~~)

Nginx反代GoogleAPI,解决谷歌地图无法访问的问题

1.0. 前言:

由于公司业务需求,需要使用高德地图api + 谷歌图层覆盖,但是由于在2021.1.11号之前所有国内可以访问谷歌图层的链接全部失效,所以决定使用 阿里云香港服务器 + nginx 做一下反代,保证图层可以获取到。

1.1 选择的是阿里云香港服务器

测试的时候买的是1C1G1M的服务器.. 所以*后在获取图层贴图的时候 比较慢..

1.2 安装lnmp

wgethttp://soft.vpser.net/lnmp/lnmp1.6.tar.gz -cO lnmp1.6.tar.gz && tar zxf lnmp1.6.tar.gz && cd lnmp1.6&& ./install.sh nginx复制代码

1.3 安装nginx相关包

yuminstall bison gitgitclone https://github.com/agentzh/sregexcdsregexmake&& make installcd/usr/local/nginx/mkdirmodules && cd modulesgitclone https://github.com/agentzh/replace-filter-nginx-modulecd/root/lnmp1.6vimlnmp.conf复制代码

修改其中一行

Nginx_Modules_Options=”–add-module=/usr/local/nginx/modules/replace-filter-nginx-module –with-ld-opt=’-Wl,-rpath,/usr/local/lib'”复制代码

重新编译

./upgrade.sh nginx# nginx 1.16.1,会提示编译版本,输入1.16.1即可复制代码

添加主机,带ssl

lnmp vhostadd复制代码

会提示输入域名:比如maps.{maps.your-domain.com你的域名}.com,其他一堆配置我都直接回车默认跳过了

修改虚拟主机nginx配置文件

cd/usr/local/nginx/conf/vhost/vi {maps.your-domain.com你的域名}.conf复制代码

1.4 配置https

因为官网使用了 htpps,所以这里必须使用https的配置, 以我们的

server{listen443 ssl http2;      #listen [::]:443 ssl http2;server_name{maps.your-domain.com 你的域名};ssl_certificate/usr/local/nginx/conf/ssl/{maps.your-domain.com你的域名}/xxxx.pem;  #将domain name.pem替换成您证书的文件名称。 这里可以参考自己服务器的https配置, 只需要把证书文件上传到对应位置就可以了。ssl_certificate_key/usr/local/nginx/conf/ssl/{maps.your-domain.com你的域名}/xxxx.key; #将domain name.key替换成您证书的密钥文件名称。ssl_session_timeout5m;ssl_ciphersECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密套件。ssl_protocolsTLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置。ssl_prefer_server_cipherson;      #error_page  404  /404.html;access_log/home/wwwlogs/{maps.your-domain.com你的域名}.log;location/maps/ {            #MIME TYPEdefault_typetext/javascript;proxy_set_headerAccept-Encoding ”;proxy_passhttps://maps.googleapis.com/maps/;replace_filter_max_buffered_size500k;replace_filter_last_modifiedkeep;replace_filter_typestext/javascript application/javascript;includevhost/replace_cn.txt;}location/maps-api-v3/ {proxy_passhttps://maps.googleapis.com/maps-api-v3/;}includevhost/location_cn.txt;}复制代码

1.5 http配置

如果是http则需要这样配置

server{listen80;        #listen [::]:80;server_name你的域名.com;indexindex.html index.htm index.php default.html default.htm default.php;root/home/wwwroot/你的域名.com;access_log/home/wwwlogs/你的域名.log;location/maps/ {            #MIME TYPEdefault_typetext/javascript;proxy_set_headerAccept-Encoding ”; # 这里注意是 http而不是httpsproxy_passhttp://maps.googleapis.com/maps/;replace_filter_max_buffered_size500k;replace_filter_last_modifiedkeep;replace_filter_typestext/javascript application/javascript;includevhost/replace_cn.txt;}location/maps-api-v3/ {proxy_passhttp://maps.googleapis.com/maps-api-v3/;}includevhost/location_cn.txt;}复制代码

1.6 添加指定文件

vilocation_cn.txt复制代码

如下内容 基本不用动

location/e173754b3b1bbd62a0be9c4afea73fdfa/{ proxy_pass https://mts0.google.com/; }location/ce60bcadc66cea58583ddd700f7d80bea/{ proxy_pass https://mts1.google.com/; }location/e6ff3de47e3134794d4442b83c841f20a/{ proxy_pass https://khms0.google.com/; }location/da42f38f94ce6ac80e2806122a7b1933a/{ proxy_pass https://khms1.google.com/; }location/7abac8c11716f6949e7b23f76e60fcd0a/{ proxy_pass https://khms0.googleapis.com/; }location/444c94a4157de3c06c435132eb2f1ac5a/{ proxy_pass https://khms1.googleapis.com/; }location/c7d52b85d86a06df50621e669557ea05a/{ proxy_pass https://mts0.googleapis.com/; }location/57cf283b871304a296c3bd8acde4cc22a/{ proxy_pass https://mts1.googleapis.com/; }location/3e0ae61058d4e7be83d222fb1f107310a/{ proxy_pass https://maps.gstatic.com/; }location/ae5102db1431e3fd01dc8336085d150fa/{ proxy_pass https://csi.gstatic.com/; }location/946eb25413c43b235b5806e999044125a/{ proxy_pass https://maps.google.com/; }location/dcd331573c0980eab6fe7346468e9974a/{ proxy_pass https://gg.google.com/; }location/0a68229ef53c18f5fc37f4106f09a6c9a/{ proxy_pass https://khms.google.com/; }location/e55bb777699a599b09d3011043439a06a/{ proxy_pass https://earthbuilder.googleapis.com/; }location/471b015572d3907c09789af41061e964a/{ proxy_pass https://mts.googleapis.com/; }location/3a4544842f4e8e90a152257489cb594da/{ proxy_pass https://static.panoramio.com.storage.googleapis.com/; }location/85471b91952baa51514c7fad7c57b8eba/{ proxy_pass https://lh3.ggpht.com/; }location/44b3a55364b2f63e51f2c6e911f8506fa/{ proxy_pass https://lh4.ggpht.com/; }location/3cc4fbda8f98767ab275af32669f56d4a/{ proxy_pass https://lh5.ggpht.com/; }location/2815063fda2dd0ccf9c566d1f089d4bca/{ proxy_pass https://lh6.ggpht.com/; }location/468724d08bce9811e989b526c6222863a/{ proxy_pass https://www.google.com/; }复制代码

然后是反代域名映射,这里需要修改成你的域名

vireplace_cn.txt复制代码

replace_filtermts0.google.com maps.your-domain.com/e173754b3b1bbd62a0be9c4afea73fdfa ig;replace_filtermts1.google.com maps.your-domain.com/ce60bcadc66cea58583ddd700f7d80bea ig;replace_filterkhms0.google.com maps.your-domain.com/e6ff3de47e3134794d4442b83c841f20a ig;replace_filterkhms1.google.com maps.your-domain.com/da42f38f94ce6ac80e2806122a7b1933a ig;replace_filterkhms0.googleapis.com maps.your-domain.com/7abac8c11716f6949e7b23f76e60fcd0a ig;replace_filterkhms1.googleapis.com maps.your-domain.com/444c94a4157de3c06c435132eb2f1ac5a ig;replace_filtermts0.googleapis.com maps.your-domain.com/c7d52b85d86a06df50621e669557ea05a ig;replace_filtermts1.googleapis.com maps.your-domain.com/57cf283b871304a296c3bd8acde4cc22a ig;replace_filtermaps.gstatic.com maps.your-domain.com/3e0ae61058d4e7be83d222fb1f107310a ig;replace_filtercsi.gstatic.com maps.your-domain.com/ae5102db1431e3fd01dc8336085d150fa ig;replace_filtermaps.googleapis.com maps.your-domain.com ig;replace_filtermaps.google.com maps.your-domain.com/946eb25413c43b235b5806e999044125a ig;replace_filtergg.google.com maps.your-domain.com/dcd331573c0980eab6fe7346468e9974a ig;replace_filterwww.google.com maps.your-domain.com/468724d08bce9811e989b526c6222863a ig;replace_filterkhms.google.com maps.your-domain.com/0a68229ef53c18f5fc37f4106f09a6c9a ig;replace_filterearthbuilder.googleapis.com maps.your-domain.com/e55bb777699a599b09d3011043439a06a ig;replace_filtermts.googleapis.com maps.your-domain.com/471b015572d3907c09789af41061e964a ig;replace_filterstatic.panoramio.com.storage.googleapis.com maps.your-domain.com/3a4544842f4e8e90a152257489cb594da ig;replace_filterlh3.ggpht.com maps.your-domain.com/85471b91952baa51514c7fad7c57b8eba ig;replace_filterlh4.ggpht.com maps.your-domain.com/44b3a55364b2f63e51f2c6e911f8506fa ig;replace_filterlh5.ggpht.com maps.your-domain.com/3cc4fbda8f98767ab275af32669f56d4a ig;replace_filterlh6.ggpht.com maps.your-domain.com/2815063fda2dd0ccf9c566d1f089d4bca ig;复制代码

然后就可以重启Nginx了,不过好像这里直接restart会出错,至少我这里一直报错,解决不了

systemctlrestart nginx复制代码

解决办法是 重启了一下服务器……

1.7 测试

通过上面搭建完反向代理后测试结果

%title插图%num

图层可以正常访问了..

PS: 因为上面配置的时候,采用的是

%title插图%num

这两个路径反代 mts0.google.com 路径下资源 所以在使用的时候,需要通过

{maps.your-domain.com你的域名} /e173754b3b1bbd62a0be9c4afea73fdfa/vt/lyrs=s@142&hl=zh-CN&gl=cn&x=6759&y=3209&z=13&s=Galil复制代码

这种方式获取资源.. 其他同理。

nginx搭文件服务器

安装nginx
yum -y install nginx

搭建文件服务器
1.改为root用户
原来的nginx.conf里,*行是

user ngix; # 不同版本的不一样
改为

user root;

2.加一个server
①配置文件位置
[root@VM_0_9_centos ~]# find / -name nginx.conf
/var/opt/gitlab/nginx/conf/nginx.conf
/opt/gitlab/embedded/lib/ruby/gems/2.4.0/gems/unicorn-5.1.0/examples/nginx.conf
/opt/gitlab/embedded/conf/nginx.conf
/etc/nginx/nginx.conf # 这个是要找的文件

②html目录
/usr/share/nginx/html # 默认的
1
③在原来的nginx.conf里加一个server配置
配置写在http { } 里面,与其他server 同级别。

autoindex on;# 显示目录
autoindex_exact_size on;# 显示文件大小
autoindex_localtime on;# 显示文件时间

server {
listen 8080 default_server;
listen [::]:8080 default_server;
server_name _;
#root /usr/share/nginx/html;
root /data/;

location / {
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

参考:https://www.jianshu.com/p/95602720e7c8

3.启动
service nginx start
service nginx stop
service nginx restart # 重启
nginx -s reload # 修改配置后重启,或者使用reload重载配置文件

4.访问界面
%title插图%num

5.完整的nginx配置参考

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}

http {
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;

access_log /var/log/nginx/access.log main;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

include /etc/nginx/mime.types;
default_type application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
include /etc/nginx/conf.d/*.conf;

location / {
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

autoindex on;# 显示目录
autoindex_exact_size on;# 显示文件大小
autoindex_localtime on;# 显示文件时间

server {
listen 6868 default_server;
listen [::]:6868 default_server;
server_name _;
#root /usr/share/nginx/html;
root /;

location / {
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

}

centos 下 nginx 使用了 HTTPs,网页访问不了

周一之前 https 可以正常访问,周一之后就不行。问了阿里云那边,阿里云那里可以正常访问。服务器端口都开放了,没有防火墙。域名由于法人更换,有段时间是不行的。现在 80 端口可以访问,就是 443 端口出问题了。切换端口也可以。这是什么问题啊?

tomwen 1
tomwen 2019-12-31 09:55:11 +08:00
域名能发出来看看吗?
首先端口是不是开了?不能访问是浏览器阻止了还是直接就打不开?是不是证书过期或者自制证书?
guanganqishi 2
guanganqishi 2019-12-31 10:21:35 +08:00 ❤️ 1
@tomwen kubao.360reborn.com:8443 弄了个 8443 端口可以访问 https://kubao.360reborn.com 443 端口不能访问
证书是昨天弄了新的,还是不行。浏览器显示的是连接已重置
sujin190 3
sujin190 2019-12-31 10:52:30 +08:00
香港阿里云的话,一直都有这个问题啊,tsl 连接会被阻断,过几分钟又正常了
guanganqishi 4
guanganqishi 2019-12-31 11:01:44 +08:00
@sujin190 不是香港的,服务器在杭州
ChicC 5
ChicC 2019-12-31 11:04:45 +08:00
配置问题咯
guanganqishi 6
guanganqishi 2019-12-31 11:08:26 +08:00
@ChicC
server {
listen 443 ssl http2;
listen [::]:443 ssl;
server_name *.360reborn.com;
charset utf-8;
access_log logs/kubao.https.access.log main;
error_log logs/kubao.https.error.log error;

index index.php index.html index.htm;

ssl on;
ssl_certificate /usr/local/nginx/ssh/360reborn.pem;
ssl_certificate_key /usr/local/nginx/ssh/360reborn.key;

ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;

location / {
root /var/www/kubao/web;
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}

error_page 404 /404.html;

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

location ~ \.php$ {
root /var/www/kubao/web;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

}

是这样的配置
sujin190 7
sujin190 2019-12-31 11:17:49 +08:00
看了下,tls 就建立不成功,客户端发送 client hello 之后连接被服务器重置了,但是不发送域名是可以成功建立连接的,所以要么是域名备案问题,要么是防火墙有特殊配置,实在不行也可以升级下 nginx 和 openssl 试试
sujin190 8
sujin190 2019-12-31 11:19:08 +08:00
证书和加密套件配置都没有问题,如果域名备案没有问题,防火墙也是正常的,那么 nginx 有 bug 也是可能的
guanganqishi 9
guanganqishi 2019-12-31 11:43:46 +08:00
@sujin190 嗯嗯 好的 谢谢 我再看看吧
sujin190 10
sujin190 2019-12-31 11:53:08 +08:00
看非 443 可以正常用而且是服务器 rst,所以大概率是域名合规问题,实在找不出啥问题可以给阿里云提工单问问

noqwerty 2019-12-31 12:02:30 +08:00 via Android
我这边可以直接打开你的链接啊,浏览器清一下缓存看看?
venhow 12
venhow 2019-12-31 14:33:46 +08:00
没有加载到证书啊
ChicC 13
ChicC 2019-12-31 14:42:46 +08:00
ssl_certificate /usr/local/nginx/ssh/360reborn.pem;
@guanganqishi

是配置 pem 文件吗?不是 crt 吗
justfly 14
justfly 2019-12-31 14:49:37 +08:00
https://47.98.153.226/ 没问题,也能达到正确的证书,所以倾向于证书没啥问题,但是使用域名就有问题,怀疑防火墙或者 SNI 相关逻辑,看看 nginx 的错误日志。
privil 15
privil 2019-12-31 15:10:37 +08:00
本机配置一下 host 域名指向 127.0.0.1 然后 curl 试一下行不行。
privil 16
privil 2019-12-31 15:11:25 +08:00
服务器本机。
jeblur 17
jeblur 2019-12-31 15:16:41 +08:00
正常打开
whnzy 18
whnzy 2020-01-03 16:38:24 +08:00
法人变更,是需要更新备案信息的,因为 2020 年,TLSv1.0 和 TLSv1.1 就不支持了,TLSv1.2 是支持 SNI 的,很容易被监测出来的。
我 ICP 查询,你们是备案的,但是有可能是因为没有更新备案信息。
每个浏览器对 TLSv1.0 和 TLSv1.1 的禁用时间不同,就会导致不同的浏览器会有不同的表现。
希望能帮到你。