LNMP调优

1、编译安装nginx前修改: 在安装包目录下 

       vim src/core/nginx.h    //#号不代表注释

          #define nginx_version      1009009        //软件版本号

          #define NGINX_VERSION      "1.9.9"        //版本号

          #define NGINX_VER          "nginx/" NGINX_VERSION      //软件程序名字

 

        vim src/http/ngx_http_header_filter_module.c    //隐藏版本信息,请求头

           //搜索NGINX_VER

          static char ngx_http_server_string[] = "Server: nginx" CRLF;改成

          static char ngx_http_server_string[] = "hello"

        vim src/http/ngx_http_special_response.c       //隐藏版本信息,相应头

          //搜索NGINX_VER

          "<hr><center>" NGINX_VER "</center>" CRLF  改成

          "<hr><center>hello </center>" CRLF

            

        安装依赖:yum install -y gcc gcc-c++ autoconf pcre-devel automake zlib zlib-devel openssl-devel 

    编译:./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/local/src/pcre-8.37

    安装:make -j 2 && make install          

    启动:/usr/local/nginx/sbin/nginx 

    检查启动是否成功(80端口监听):netstat -lntup

    禁用防火墙和selinx

2、调优:

    主控进程(root):主要目的是产生和销毁worker进程的及生成日志,自己不处理请求。

    运行进程(worker进程:nobody):主要负责接受用户请求。

    vim /usr/local/nginx/conf/nginx.conf    //配置文件

        #如果改nginx的用户组则打开#user  nobody;将nobody更改,建议不改

        worker_processes  1;      //运行的线程数,建议将数量改为auto

        worker_cpu_affinity 00000001 ~~~~~01000000    //cpu绑定到1~7核cup上,建议不加

        worker_rlimit_nofile 1024      //一个worker进程默认打开的文件数,默认1024

        events {              //定义事件,

           use epoll            //事件模型,在linux中使用epoll模型最好

              worker_connections  1024;    //一个worker进程可以相应的最大并发数

            accept_mutex_delay 60;     //一个worker进程接受请求的时候拒绝其他请求的时间,慎重使用

            accept_mutex on        //是否打开负载均衡锁。当使用nginx作为负载均衡的时候,如果打开,可以让多个worker进程轮流的与客户端进行连接,真正成为一个负载均衡,否则一个进程一直在处理负载均衡,而其他的在闲置。

            }

        http{

            include       mime.types;

              default_type  application/octet-stream; 

            //这两行加载媒体格式,如css、js等,必须

----------------------------------------------------------------------------------

            #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  logs/access.log  main;

            //定义日志格式,可开启

----------------------------------------------------------------------------------

            sendfile        on;

            //开启高效文件传输模式sendfile指令指定ngnix是否调用,配合下面,需打开

            #tcp_nopush     on;

----------------------------------------------------------------------------------

            keepalive_timeout  65;      //持久链接

            tcp_nodelay on;

            client_header_timeout 15;    //客户端请求头读取的超时时间

            client_body_timeout 15;      //body请求读取的超时时间

            send_timeout 15           

            //这几个选项保持持久链接

----------------------------------------------------------------------------------

            #gzip  on;

            //是否开启压缩,需配置压缩的范围

            gzip_min_length 1k;       //压缩文件大小,大于1k的文件开始压缩,小于的会越压越大

            gzip_buffers 4 32k;      //缓冲区大小,4段,每段32k

            gzip_http_version 1.1;     //压缩版本,默认为1.1,可不写这行

            gzip_comp_level 5;        //压缩等级,范围1~9,一般写到4或5

            gzip_types image/png text/html text/xml application/javacript;    //压缩文件类型

            gzip_vary on;          //启动前端缓存服务

----------------------------------------------------------------------------------

             client_max_body_size 10m    //上传大小的限制,最大为10m,一般不用,在php内做限制

----------------------------------------------------------------------------------

          }

       server {

----------------------------------------------------------------------------------

           listen 80;      //监听端口

                写法:listen port;和listen ip:port     (注:port为端口)

 

----------------------------------------------------------------------------------

           server_name;      //服务器名称

                写法:www.baidu.com   *.baidu.com www.*  ~^.*.baidu.com$(正则) default_server

----------------------------------------------------------------------------------

           location写法

              =    //精确匹配

              ^~  //URI前半部分匹配

              ~  //正则表达式匹配,区分大小写

              ~*  //正则表达式匹配,不区分大小写

           

           location / {                  //根

                       root   html;          //网站根目录

                       index  index.html index.php index.htm;

                   }

----------------------------------------------------------------------------------

 

            error_page   500 502 503 504  /50x.html;

                    location = /50x.html {

                        root   html;

                    }

                //配置错误页面和状态码

----------------------------------------------------------------------------------

             proxy_pass    //重定向,写在server里,如:

                            location / {

                                    proxy_pass  http://www.sina.com;      //这样用比较少

                                  }

 

            //健康的

             location / {

                      proxy_pass http://192.168.31.223;

                      porxy_set_header Host $host;

                      porxy_set_header X-Real-IP $remote_addr;

                    }

            //在要访问的机器上配置(httpd服务)

            vim /etc/httpd/conf/httpd.conf

            //搜索LogFormat

                %h改%{X-Real-IP}i

            //搜索combined检查是否调用combined

            重启httpd

----------------------------------------------------------------------------------

           }

            //缓存

                proxy_cache_path /tmp/nginxcache levels=1:2 keys_zone=hello:10m;

                        /tmp/nginxcache      //缓存路径

                        levels=1:2          //目录层级,第一级目录为一个字符,第二级目录为两个字符,一个冒号为两层,两个冒号为三层,如:1:2:3

                        keys_zone=hello:10m      //从内存中取10m空间命名为hello

                proxy_cache_methods GET HEAD;     //缓存的格式

                proxy_cache_min_uses 1;      //设置为1的时候为这个资源启用一次开始缓存,设置为2时为启用两次时开始缓存

                proxy_cache_revalidate on;       //有效期验证

                proxy_cache_use_stale error timrout;        //如果缓存过期了,就去后端请求

                proxy_cache_valid 200 1d;      //缓存时间设置,200的状态码缓存一天

                proxy_connect_timeout 30s;      //缓存超时时间

                proxy_hide_header;          //隐藏http首部,可不配置

                proxy_buffer_size 8k;        //加速响应

                proxy_cache_bypass $http_authorization    //对哪些内容不要缓存,对http认证的内容不缓存

                //设置好以后需在server内的location内引用

                    proxy_cache  hello;

----------------------------------------------------------------------------------

                 upstream backend{

                          server ip:端口  max_fails(检测次数)=3 fail_timeout 30s;

                          }

                //配置upstream后在server下的location下的proxy_pass改为

                          porxy_pass http://backend;

----------------------------------------------------------------------------------

 

原文地址:https://www.cnblogs.com/yuyangphpweibo/p/7846822.html