Nginx常见问题处理

    xiaoxiao2023-11-25  182

    问题

    本案例要求对Nginx服务器进行适当优化,以提升服务器的处理性能: 

    如果客户端访问服务器提示“Too many open files”如何解决如何解决客户端访问头部信息过长的问题如何让客户端浏览器缓存数据如何自定义返回给客户端的404错误页面通过配置Nginx配置文件,实现防止盗链的功能,具体要求如下:禁止其他网站,使用链接引用本网站的图片、视频资源如果有链接引入,则重定向至一个错误页面

    然后客户机访问此Web服务器验证效果:

    使用ab压力测试软件测试并发量编写测试脚本生成长头部信息的访问请求客户端访问不存在的页面,测试404错误页面是否重定向 方案

    使用2台RHEL6虚拟机,其中一台作为Nginx服务器(192.168.4.5)、另外一台作为测试用的Linux客户机(192.168.4.205),如图-1所示。

    -1

    Nginx优化主要从配置文件着手,常见优化参数如下:

    worker_rlimit_nofile //最大打开文件数量worker_processes //与CPU核心数量一致worker_connections //每个worker最大并发连接数client_header_buffer_size //默认请求包头信息的缓存large_client_header_buffers //大请求包头部信息的缓存个数与容量error_page   404   //自定义404错误页面

    Nginx防止盗链需要使用的是valid_referers,来定义有效的跳转服务器域名,所有未定义的服务器,均不可以通过连接使用本服务器上的资源。

    步骤

    实现此案例需要按照如下步骤进行。

    步骤一:构建Nginx服务器

    1)源码安装Nginx软件

    [root@svr5 ~]# yum -y install gcc pcre-devel openssl-devel //安装常见依赖包

    [root@svr5 ~]# useradd -s /sbin/nologin nginx

    [root@svr5 ~]# tar  -zxvf   nginx-0.8.55.tar.gz

    [root@svr5 ~]# cd  nginx-0.8.55

    [root@svr5 nginx-0.8.55]# ./configure   \

    > --prefix=/usr/local/nginx   \ //指定安装路径

    > --user=nginx   \ //指定用户

    > --group=nginx  \ //指定组

    > --with-http_stub_status_module  \ //开启状态统计功能

    > --with-http_ssl_module //开启SSL加密功能

      .. ..

    nginx path prefix: "/usr/local/nginx"

      nginx binary file: "/usr/local/nginx/sbin/nginx"

      nginx configuration prefix: "/usr/local/nginx/conf"

      nginx configuration file: "/usr/local/nginx/conf/nginx.conf"

      nginx pid file: "/usr/local/nginx/logs/nginx.pid"

      nginx error log file: "/usr/local/nginx/logs/error.log"

      nginx http access log file: "/usr/local/nginx/logs/access.log"

      nginx http client request body temporary files: "client_body_temp"

      nginx http proxy temporary files: "proxy_temp"

      nginx http fastcgi temporary files: "fastcgi_temp"

      nginx http uwsgi temporary files: "uwsgi_temp"

      nginx http scgi temporary files: "scgi_temp"

    [root@svr5 nginx-0.8.55]# make && make install //编译并安装

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

    [root@svr5 ~]# /usr/local/nginx/sbin/nginx –c /usr/local/nginx/conf/nginx.conf

    [root@svr5 ~]# netstat  -anptu  |  grep nginx

    tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10441/nginx

    3)为Nginx Web服务器建立测试页面文件

    Nginx Web服务默认首页文档存储目录为/usr/local/nginx/html/,在此目录下建立一个名为index.html的文件:

    [root@svr5 ~]# cat  /usr/local/nginx/html/index.html

    <html>

    <head>

    <title>Welcome to nginx!</title>

    </head>

    <body bgcolor="white" text="black">

    <center><h1>Welcome to nginx!</h1></center>

    </body>

    </html>

    从系统中随机拷贝一份jpg图片资源,供测试使用:

    [root@svr5 ~]# cp /usr/share/pixmaps/faces/energy-arc.jpg \

    >/usr/local/nginx/html/a.jpg

    提前生成404错误页面,供测试使用:

    [root@svr5 ~]# echo "<h1>~~~~^^^Error^^^~~~</h1>" > /usr/local/nginx/html/40x.html

    4)修改Nginx实现防止盗链

    [root@svr5 ~]# /usr/local/nginx/conf/nginx.conf

    .. ..

    location ~* \.(gif|jpg|png|swf|flv)$ {

    valid_referers none blocked www.tarena.com;

    if ($invalid_referer) {

    rewrite ^/ http://www.tarena.com/403.html;

    }

    }

    5)再创建一台Nginx服务器(192.168.4.205,使用客户端模式另一台盗链服务器),并创建连接资源

    [root@svr205 ~]# vim /usr/local/nginx/html/index.html

    <html>

    <body>

    <a href=”http://www.tarena.com/a.jpg”> 图片</a>

    </body>

    </html>

    6)客户端测试

    从客户机访问192.168.4.205,验证是否可以通过链接访问www.tarena.com服务器上的资源,使用火狐浏览器访问192.168.4.205:

    [root@pc205 ~]# firefox http://192.168.4.205

     

    步骤二:优化前从客户机访问Nginx服务器测试

    1)使用ab高并发测试

    [root@svr205 ~]# ab –n 2000 –c 2000 http://192.168.4.5/

    Benchmarking 192.168.4.5 (be patient)

    socket: Too many open files (24) //提示打开文件数量过多

    2)使用脚本测试长头部请求是否能获得响应

    [root@svr205 ~]# vim test.sh

    #!/bin/bash

    url="http://192.168.4.5/index.html?debug=1"

    for i in {0..1000}

    do

        var="v$i"

        url="${url}&$var=$i"

    done

    curl $url –v

    [root@svr205 ~]# chmod +x test.sh

    [root@svr205 ~]# ./test.sh

    .. ..

    <center><h1>414 Request-URI Too Large</h1></center> //提示头部信息过大

    3)使用Firefox浏览器测试客户端缓存

    以Firefox浏览器为例,只要在地址栏内输入 http://192.168.4.5/a.jpg,回车后即连接目标主机192.168.4.5的Web服务,获得服务器上的a.jpg图片资源。若访问成功,再次,在Firefox地址栏内输入about:cache将显示Firefox浏览器的缓存信息,如图-2所示。

    -2

    4)客户端使用浏览器访问不存在的页面

    [root@svr5 ~]# firefox http://192.168.4.5/tt.html //访问不存在的页面

    步骤三:优化Nginx服务器

    1)修改Nginx配置文件

    [root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf

    .. ..

    worker_rlimit_nofile 60000; //最大打开文件数量

    events {

    worker_connections 10000; //每个worker最大并发连接数

    use epoll;

    }

    http {

    client_header_buffer_size    1k; //默认请求包头信息的缓存

    large_client_header_buffers  4 1m; //大请求包头部信息的缓存个数与容量

     

    server {

            listen       80;

            server_name  www.tarena.com;

            location / {

                root   html;

                index  index.html index.htm;

            }

    location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {

                    access_log  off;

    expires 30d; //定义客户端缓存时间为30天

    }

    error_page   404  /40x.html; //自定义错误页面

            location = /40x.html {

                root   html;

            }

    }

    }

    2)修改Linux操作系统最大打开文件数

    通过修改/etc/security/limits.conf文件修改打开文件最大数量:

    [root@svr5 ~]# vim /etc/security/limits.conf

    *               soft nofile     100000

    *               hard nofile     100000

    步骤四:优化后从客户机访问Nginx服务器测试

    对Nginx服务器进行各种参数优化后,在客户端主机重复执行步骤二所有指令,对比优化前与优化后的区别,验证优化是否生效。

    最新回复(0)