centos 安装nginx而且优化

准备工做:javascript

yum -y install gcc gcc-c++ autoconf automake makephp

yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel css

以后我用的阿里的那个tengine-2.1.1.tar.gz(同事给的,我就用上了,至于有什么特别之处,还待研究),html

groupadd nginx前端

useradd -g nginx nginxjava

以后编译nginx,node

tar -zxvf nginx-1.9.5.tar.gznginx

cd nginx-1.9.5c++

这个有点狠,安装了一大堆.缓存

./configure  --prefix=/usr/local/nginx  --user=nginx   --group=nginx  --with-http_ssl_module  --with-http_realip_module --with-http_addition_module --with-http_sub_module  --with-http_dav_module  --with-http_flv_module   --with-http_mp4_module  --with-http_gunzip_module  --with-http_gzip_static_module  --with-http_random_index_module  --with-http_secure_link_module   --with-http_stub_status_module  --with-http_auth_request_module  --with-mail  --with-mail_ssl_module --with-pcre

make && make install

这个连接说了怎么把nginx作成服务,我保存先,之后再看

/etc/rc.d/rc.local文件里,写上开机的脚本便可

/usr/local/nginx/sbin/nginx


调优:

echo 1000 >/proc/sys/net/core/somaxconn



#user  nobody;

worker_processes  auto;


error_log  logs/error.log;

error_log  logs/error.log  notice;

error_log  logs/error.log  info;


pid        logs/nginx.pid;



events {

    use epoll;#Linux 下性能最好的 event 模式

    worker_connections  65535;# 每一个工做进程容许最大的同时链接数

}


# load modules compiled as Dynamic Shared Object (DSO)

#

#dso {

#    load ngx_http_fastcgi_module.so;

#    load ngx_http_rewrite_module.so;

#}


http {

    include       mime.types;

    default_type  application/octet-stream;


    #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;

    #tcp_nopush     on;

    tcp_nodelay     on;#这个不知道与上面的有啥不同

    #keepalive_timeout  0;

    keepalive_timeout  65;#长链接超时时间,单位是秒

    

   client_header_buffer_size 4k;#经过getconf PAGESIZE 获得4096,因而设置成与分页大小同样

    open_file_cache max=102400 inactive=20s;#将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指通过多长时间文件没被请求后删除缓存

    open_file_cache_valid 30s;#这个是指多长时间检查一次缓存的有效信息

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


    gzip on; #开启gzip压缩输出

gzip_min_length 1k; #最小压缩文件大小

gzip_buffers 4 16k; #压缩缓冲区

gzip_http_version 1.1; #压缩版本(默认1.1,前端若是是squid2.5请使用1.0)

gzip_comp_level 2; #压缩等级,9压缩的最小可是耗费的cpu最高

gzip_types text/plain application/x-javascript text/css application/xml;

#压缩类型,默认就已经包含text/html,因此下面就不用再写了,写上去也不会有问题,可是会有一个warn。

gzip_vary on;

     #limit_zone crawler $binary_remote_addr 10m; #开启限制IP链接数的时候须要使用

   

       upstream backend1 {

                ip_hash;

                server 10.4.42.219:12601 weight=10;

                server 10.4.101.63:8080 weight=10;

        }


    server {

        listen       80;

        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;

        

location /NginxStatus { 

   stub_status on; #Nginx 状态监控配置

            allow all;

            #deny all;

   access_log off; 

         }

 


         

        location / {

proxy_next_upstream error timeout invalid_header  http_502 http_503 http_504; #去掉404与500

        proxy_read_timeout 1200s;# 设置超时时间,默认是60s,有的请求返回慢

        proxy_set_header HOST $host;

        proxy_set_header X-REAL-IP $remote_addr;

        proxy_set_header X-FORWARD-FOR $proxy_add_x_forwarded_for;

        proxy_pass http://backend1/;

       root   html;

        index  index.html index.htm;

        }




        #error_page  404              /404.html;


        # redirect server error pages to the static page /50x.html

        #

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }


        # proxy the PHP scripts to Apache listening on 127.0.0.1:80

        #

        #location ~ \.php$ {

        #    proxy_pass   http://127.0.0.1;

        #}


        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        #

        #location ~ \.php$ {

        #    root           html;

        #    fastcgi_pass   127.0.0.1:9000;

        #    fastcgi_index  index.php;

        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

        #    include        fastcgi_params;

        #}


        # deny access to .htaccess files, if Apache's document root

        # concurs with nginx's one

        #

        #location ~ /\.ht {

        #    deny  all;

        #}

    }



    # another virtual host using mix of IP-, name-, and port-based configuration

    #

    #server {

    #    listen       8000;

    #    listen       somename:8080;

    #    server_name  somename  alias  another.alias;


    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}



    # HTTPS server

    #

    #server {

    #    listen       443 ssl;

    #    server_name  localhost;


    #    ssl_certificate      cert.pem;

    #    ssl_certificate_key  cert.key;


    #    ssl_session_cache    shared:SSL:1m;

    #    ssl_session_timeout  5m;


    #    ssl_ciphers  HIGH:!aNULL:!MD5;

    #    ssl_prefer_server_ciphers  on;


    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}


}



接下来优化下操做系统的参数:

vi /etc/sysctl.conf


net.ipv4.ip_local_port_range = 1024 65000

net.core.rmem_default=262144

net.core.rmem_max=262144

net.core.wmem_default=262144

net.core.wmem_max=262144

net.ipv4.tcp_max_tw_buckets=6000

net.core.somaxconn=262144

net.core.netdev_max_backlog=262144

net.ipv4.tcp_max_syn_backlog=262144

fs.file-max = 6553560

net.ipv4.tcp_fin_timeout = 30

#net.ipv4.icmp_echo_ignore_all = 1  #这个是禁用被ping,防止被人ping挂了

sysctl -p  #生效

给个网址:http://ifeve.com/inside-nginx-how-we-designed-for-performance-scale/

相关文章
相关标签/搜索