Nginx优化

编译安装 Nginx:

[root@nginx ~]# yum -y install  gcc gcc-c++ make pcre-devel zlib-devel openssl-devel

[root@nginx ~]# tar xf nginx-1.6.0.tar.gz -C /usr/src/

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

[root@nginx ~]# cd /usr/src/nginx-1.6.0

[root@nginx nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module&&make && make install

一、更改Nginx运行进程

         在高并发场景,需要启动更多的nginx进程以保证快速影响,以处理用户的请求,避免造成阻塞。

修改配置文件的worker_processes参数,一般设置为CPU的个数或者核数的2

 

[root@localhost ~]# grep 'core id' /proc/cpuinfo | uniq | wc -l

1

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

  3 worker_processes  2;

[root@localhost ~]# /usr/local/nginx/sbin/nginx

[root@localhost ~]# ps aux | grep nginx | grep -v grep

root 1317 0.0 0.5 47140 2616 ? Ss 15:05 0:00 nginx: master process nginx
nginx 1513 0.0 0.4 47536 2064 ? S 19:56 0:00 nginx: worker process
nginx 1514 0.0 0.4 47536 2064 ? S 19:56 0:00 nginx: worker process

默认Nginx的多个进程可能更多的跑在一颗CPU上,可以分配不同的进程给不同的CPU处理,充分利用硬件多核多CPU。在一台4核物理服务器,可以进行下面的配置,将进程进行分配。

worker_cpu_affinity  0001 0010 0100 1000

二、配置 Nginx 隐藏版本号

在生产环境中,需要隐藏 Nginx 等服务的版本信息,以避免安全风险。

隐藏方法:

(1)安装之前,修改源码包

[root@nginx ~]#  tar xf nginx-1.6.0.tar.gz

[root@nginx ~]#  vim nginx-1.6.0 /src/core/nginx.h

13 #define NGINX_VERSION     "9999"

14 #define NGINX_VER              "Apache/" NGINX_VERSION

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

[root@nginx ~]#  cd nginx-1.6.0

[root@nginx  nginx-1.6.0]# yum -y install pcre-devel zlib-devel

[root@nginx  nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-pcre && make && make install

[root@nginx ~]# /usr/local/nginx/sbin/nginx

[root@nginx ~]# netstat -anpt | grep nginx

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      23521/nginx: master

root@nginx ~]# curl -I http://192.168.10.11

HTTP/1.1 200 OK
Server: Apache/9999
Date: Fri, 13 Sep 2019 07:48:29 GMT
Content-Type: text/html
Content-Length: 667
Last-Modified: Fri, 13 Sep 2019 07:22:06 GMT
Connection: keep-alive
ETag: "5d7aef3e-29b"
Accept-Ranges: bytes

(2)启动服务之后,修改配置文件

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

26         server_tokens off;

[root@nginx ~]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@nginx ~]# killall -HUP nginx

[root@nginx ~]# curl -I http://192.168.10.11

HTTP/1.1 200 OK
Server: nginx
Date: Fri, 13 Sep 2019 07:48:29 GMT
Content-Type: text/html
Content-Length: 667
Last-Modified: Fri, 13 Sep 2019 07:22:06 GMT
Connection: keep-alive
ETag: "5d7aef3e-29b"
Accept-Ranges: bytes

三、配置Nginx网页缓存时间

         当Nginx将网页数据返回给客户端后,可设置资源在客户端缓存的时间,以方便客户端在日后进行相同内容的请求时直接返回,以避免重复请求,加快了访问速度,一般针对静态网页进行设置,对动态网页不用设置缓存时间。可在Windows客户端中使用fiddler查看网页缓存时间。

设置方法:可修改配置文件,在http段、或server段、或者location段加入对特定内容的过期参数。

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

         location ~ .(gif|jpg|jpeg|png|bmp|ico)$ {

             expires 1d;

         }

                location ~ .*.(js|css)$ {

            expires 1h;

         }

 

[root@localhost ~]# killall -HUP nginx

四、配置Nginx实现连接超时

         在企业网站中,为了避免同一个客户长时间占用连接,造成服务器资源浪费,可以设置相应的连接超时参数,实现控制连接访问时间。

keepalived_timeout:设置连接保持超时时间,一般可只设置该参数,默认为65秒,可根据网站的情况设置,或者关闭,可在http段、server段、或者location段设置。

client_header_timeout:指定等待客户端发送请求头的超时时间。

client_body_timeout:设置请求体读取超时时间。

注意:若出现超时,会返回408报错

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

 34     keepalive_timeout  65;

 35     client_header_timeout 60;

 36     client_body_timeout 60;

[root@localhost ~]# killall -HUP nginx

五、配置Nginx实现防盗链功能

配置说明:

valid_referers    设置信任网站

none           浏览器中referer(Referer是header的一部分,当浏览器向web服务器发送请求的时候,一般会带上Referer,告诉服务器我是从哪个页面链接过来的,服务器基此可以获得一些信息用于处理)为空的情况,就直接在浏览器访问图片

blocked            referer不为空的情况,但是值被代理或防火墙删除了,这些值不以http://或https://开头

 

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

        location ~* .(wma|wmv|asf|mp3|mmf|zip|rar|jpg|gif|png|swf|flv)$ {

            valid_referers none blocked *.source.com source.com;

            if ($invalid_referer) {

                rewrite ^/ http://www.source.com/linux.jpg;

                #return 403;

            }

        }

[root@localhost ~]# nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@localhost ~]# killall -HUP nginx

 

第一行:wma|wmv|asf|mp3|mmf|zip|rar|jpg|gif|png|swf|flv表示对这些后缀的文件实行防盗链

第二行:none blocked *.source.com source.com;   //不区分大小写

表示referers信息中匹配none blocked *.source.com source.com (*代表任何,任何的二级域名)

if{}里面内容的意思是,如果链接不是来自第二行指定的就强制跳转到403错误页面,当然直接返回404也是可以的,也可以是图片。

 

六、自定义错误页面

48  error_page  404              /404.html;

49  location = /50x.html {

50  root html;
}

七、通过UA实现手机端和电脑端的分离

实现nginx区分pc和手机访问不同的网站,是物理上完全隔离的两套网站(一套移动端、一套pc端),这样带来的好处pc端和移动端的内容可以不一样,移动版网站不需要包含特别多的内容,只要包含必要的文字和较小的图片,这样会更节省流量。有好处当然也就会增加困难,难题就是你需要维护两套环境,并且需要自动识别出来用户的物理设备并跳转到相应的网站,当判断错误时用户可以自己手动切换回正确的网站。

有两套网站代码,一套PC版放在/usr/local/nginx/html/web,一套移动版放在/usr/local/nginx/html/mobile。只需要修改nginx的配置文件,nginx通过UA来判断是否来自移动端访问,实现不同的客户端访问不同内容。

location / {

         #默认PC端访问内容

    root /usr/local/nginx/html/web;

 

         #如果是手机移动端访问内容

    if ( $http_user_agent ~ "(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT-)|(SonyEricsson)|(NEC-)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG-)|(LG/)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC-)|(SED-)|(EMOL-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera)" )

         {

                   root /usr/local/nginx/html/mobile;

         }

 

         index index.html index.htm;

}

实验模拟:不同浏览器访问到不同的页面:

[root@localhost html]# mkdir firefox msie

[root@localhost html]# echo "hello,firefox" > firefox/index.html

[root@localhost html]# echo "hello,msie" > msie/index.html

        location / {

        if ($http_user_agent ~ Firefox) {

            root /usr/local/nginx/html/firefox;

        }

        if ($http_user_agent ~ MSIE) {

            root /usr/local/nginx/html/msie;

        }

            index  index.html index.htm;

        }

原文地址:https://www.cnblogs.com/wanghs8/p/11545082.html