Nginx 五大常见应用场景,运维请收藏~

运维 系统运维
Nginx 是一个很强大的高性能 Web 和反向代理服务,它具有很多非常优越的特性,在连接高并发的情况下,Nginx 是 Apache 服务不错的替代品。

 Nginx 是一个很强大的高性能 Web 和反向代理服务,它具有很多非常优越的特性,在连接高并发的情况下,Nginx 是 Apache 服务不错的替代品。其特点是占有内存少,并发能力强,事实上 nginx 的并发能力在同类型的网页服务器中表现较好,因此国内知名大厂例如:淘宝,京东,百度,新浪,网易,腾讯等等都在使用 Nginx 网站。

[[352872]]

在我们的日常工作学习中,我们会该如何去优化自己的 Nginx 服务器?遇到以下问题我们该如何处理呢?

一、自定义返回客户端的404错误页面

1)优化前,客户端使用浏览器访问不存在的页面,会提示404文件未找到

# firefox http://192.168.4.5/xxxxx //访问一个不存在的页面 
  • 1.

2)修改 Nginx 配置文件,自定义报错页面

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf 
 
.. .. 
 
charset utf-8//仅在需要中文时修改该选项 
 
error_page 404 /404.html; //自定义错误页面 
 
.. .. 
 
# vim /usr/local/nginx/html/404.html //生成错误页面 
 
Oops,No NO no page … 
 
# nginx -s reload 
 
# 请先确保 nginx 是启动状态,否则运行该命令会报错,报错信息如下: 
 
#[error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

3)优化后,客户端使用浏览器访问不存在的页面,会提示自己定义的 40x.html 页面

# firefox http://192.168.4.5/xxxxx //访问一个不存在的页面 
  • 1.

常见的 http 状态码可用参考表所示

二、查看服务器状态信息

1)编译安装时使用--with-http_stub_status_module开启状态页面模块

# tar -zxvf nginx-1.12.2.tar.gz 
 
# cd nginx-1.12.2 
 
# ./configure \ 
 
> --with-http_ssl_module //开启SSL加密功能 
 
> --with-stream //开启TCP/UDP代理模块 
 
> --with-http_stub_status_module //开启status状态页面 
 
# make && make install //编译并安装 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

2)启用 Nginx 服务并查看监听端口状态

ss 命令可以查看系统中启动的端口信息,该命令常用选项如下:

  • -a 显示所有端口的信息
  • -n 以数字格式显示端口号
  • -t 显示TCP连接的端口
  • -u 显示UDP连接的端口
  • -l 显示服务正在监听的端口信息,如httpd启动后,会一直监听80端口
  • -p 显示监听端口的服务名称是什么(也就是程序名称)

注意:在 RHEL7 系统中可以使用ss命令替代 netstat 命令,功能一样,选项一样。

# /usr/local/nginx/sbin/nginx 
 
# netstat -anptu | grep nginx 
 
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10441/nginx 
 
# ss -anptu | grep nginx 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

3)修改 Nginx 配置文件,定义状态页面

# cat /usr/local/nginx/conf/nginx.conf 
 
… … 
 
location /status { 
 
stub_status on; 
 
#allow IP地址; 
 
#deny IP地址; 
 

 
… … 
 
# /usr/local/nginx/sbin/nginx -s reload 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

4)优化后,查看状态页面信息

# curl http://192.168.4.5/status 
 
Active connections: 1 
 
server accepts handled requests 
 
10 10 3 
 
Reading: 0 Writing: 1 Waiting: 0 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

Active connections:当前活动的连接数量。

Accepts:已经接受客户端的连接总数量。

Handled:已经处理客户端的连接总数量。

(一般与accepts一致,除非服务器限制了连接数量)。

Requests:客户端发送的请求数量。

Reading:当前服务器正在读取客户端请求头的数量。

Writing:当前服务器正在写响应信息的数量。

Waiting:当前多少客户端在等待服务器的响应。

三、优化 Nginx 并发量

1)优化前使用ab高并发测试

# ab -n 2000 -c 2000 http://192.168.4.5/ 
 
Benchmarking 192.168.4.5 (be patient) 
 
socket: Too many open files (24//提示打开文件数量过多 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

2)修改 Nginx 配置文件,增加并发量

# vim /usr/local/nginx/conf/nginx.conf 
 
.. .. 
 
worker_processes 2//与CPU核心数量一致 
 
events { 
 
worker_connections 65535//每个worker最大并发连接数 
 

 
.. .. 
 
# /usr/local/nginx/sbin/nginx -s reload 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

3)优化 Linux 内核参数(最大文件数量)

# ulimit -a //查看所有属性值 
 
# ulimit -Hn 100000 //设置硬限制(临时规则) 
 
# ulimit -Sn 100000 //设置软限制(临时规则) 
 
# vim /etc/security/limits.conf 
 
.. .. 
 
* soft nofile 100000 
 
* hard nofile 100000 
 
#该配置文件分4列,分别如下:10.#用户或组 硬限制或软限制 需要限制的项目 限制的值 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

4)优化后测试服务器并发量(因为客户端没调内核参数,所以在proxy测试)

# ab -n 2000 -c 2000 http://192.168.4.5/ 
  • 1.

四、优化 Nginx 数据包头缓存

1)优化前,使用脚本测试长头部请求是否能获得响应

[root@proxy ~]# cat lnmp_soft/buffer.sh 
 
#!/bin/bash 
 
URL=http://192.168.4.5/index.html? 
 
for i in {1..5000
 
do 
 
URL=${URL}v$i=$i 
 
done 
 
curl $URL //经过5000次循环后,生成一个长的URL地址栏 
 
[root@proxy ~]# ./buffer.sh 
 
.. .. 
 
<center><h1>414 Request-URI Too Large</h1></center> //提示头部信息过大 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

2)修改 Nginx 配置文件,增加数据包头部缓存大小

# vim /usr/local/nginx/conf/nginx.conf 
 
... .. 
 
http { 
 
client_header_buffer_size 1k; //默认请求包头信息的缓存 
 
large_client_header_buffers 4 4k; //大请求包头部信息的缓存个数与容量 
 
.. .. 
 

 
# /usr/local/nginx/sbin/nginx -s reload 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

3)优化后,使用脚本测试长头部请求是否能获得响应

1.[root@proxy ~]# cat buffer.sh 
 
2.#!/bin/bash 
 
3.URL=http://192.168.4.5/index.html? 
 
4.for i in {1..5000
 
5.do 
 
6. URL=${URL}v$i=$i 
 
7.done 
 
8.curl $URL 
 
9.[root@proxy ~]# ./buffer.sh 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

五、浏览器本地缓存静态数据

1)使用Firefox浏览器查看缓存

以Firefox浏览器为例,在Firefox地址栏内输入about:cache将显示Firefox浏览器的缓存信息,如图所示,点击List Cache Entries可以查看详细信息。

2)清空firefox本地缓存数据,如图所示。

3)改Nginx配置文件,定义对静态页面的缓存时间

# vim /usr/local/nginx/conf/nginx.conf 
 
server { 
 
listen 80
 
server_name localhost; 
 
location / { 
 
root html; 
 
index index.html index.htm; 
 

 
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ { 
 
expires 30d; //定义客户端缓存时间为30天 
 

 

 
# cp /usr/share/backgrounds/day.jpg /usr/local/nginx/html 
 
# /usr/local/nginx/sbin/nginx -s reload 
 
#请先确保nginx是启动状态,否则运行该命令会报错,报错信息如下:16.#[error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.

4)优化后,使用Firefox浏览器访问图片,再次查看缓存信息

# firefox http://192.168.4.5/day.jpg 
  • 1.

在 firefox 地址栏内输入 about:cache,查看本地缓存数据,查看是否有图片以及过期时间是否正确。

 

 

责任编辑:张燕妮 来源: 高效运维
相关推荐

2024-07-01 07:59:07

2021-09-26 05:38:16

云计算云计算环境云应用

2019-08-15 09:45:54

软件技术Docker

2019-01-10 08:41:50

生物识别身份验证指纹

2017-06-14 19:05:51

机器学习Quora应用场景

2019-08-15 10:41:33

云时代运维容器

2019-12-27 10:33:43

运维架构技术

2020-11-04 07:34:02

Redis数据类型

2009-07-20 16:52:18

运维管理流程北塔

2016-03-28 17:00:32

互联网运维体系运维

2017-04-13 10:32:15

2019-11-04 14:34:45

人生第一份工作云计算技术

2013-08-07 10:23:58

MySQL运维数据库运维

2010-07-21 08:51:26

Perl错误

2024-06-25 17:13:36

2009-03-24 10:09:58

SaaS误区调查

2023-09-12 09:47:38

云计算云管理

2018-04-10 04:01:17

2024-05-27 00:05:00

2023-08-27 21:22:02

Redis数据类
点赞
收藏

51CTO技术栈公众号