从配置说tomcat和nginx调优

1 nginx优化

# nginx不同于apache服务器,当进行了大量优化设置后会魔术般的明显性能提升效果
# nginx在安装完成后,大部分参数就已经是最优化了,我们需要管理的东西并不多
#user  nobody;
#阻塞和非阻塞网络模型:
#同步阻塞模型,一请求一进(线)程,当进(线)程增加到一定程度后
#更多CPU时间浪费到切换一,性能急剧下降,所以负载率不高
#Nginx基于事件的非阻塞多路复用(epoll或kquene)模型
#一个进程在短时间内可以响应大量的请求
#建议值 <= cpu核心数量,一般高于cpu数量不会带好处,也许还有进程切换开销的负面影响
worker_processes 4;
#将work process绑定到特定cpu上,避免进程在cpu间切换的开销
worker_cpu_affinity 0001 0010 0100 1000 
#8内核4进程时的设置方法 worker_cpu_affinity 00000001 00000010 00000100 10000000
 
# 每进程最大可打开文件描述符数量(linux上文件描述符比较广义,网络端口、设备、磁盘文件都是)
# 文件描述符用完了,新的连接会被拒绝,产生502类错误
# linux最大可打开文件数可通过ulimit -n FILECNT或 /etc/security/limits.conf配置
# 理论值 系统最大数量 / 进程数。但进程间工作量并不是平均分配的,所以可以设置的大一些
worker_rlimit_nofile 655350 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    # 并发响应能力的关键配置值
# 每个进程允许的最大同时连接数,work_connectins * worker_processes = maxConnection;
# 要注意maxConnections不等同于可响应的用户数量,
# 因为一般一个浏览器会同时开两条连接,如果反向代理,nginx到后端服务器的连接也要占用连接数
# 所以,做静态服务器时,一般 maxClient = work_connectins * worker_processes / 2
# 做反向代理服务器时 maxClient = work_connectins * worker_processes / 4
# 这个值理论上越大越好,但最多可承受多少请求与配件和网络相关,也可最大可打开文件,最大可用sockets数量(约64K)有关
    worker_connections  500;
# 指明使用epoll 或 kquene (*BSD)
use epoll
# 备注:要达到超高负载下最好的网络响应能力,还有必要优化与网络相关的linux内核参数
}
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"';
# 关闭此项可减少IO开销,但也无法记录访问信息,不利用业务分析,一般运维情况不建议使用
access_log off
# 只记录更为严重的错误日志,可减少IO压力
error_log logs/error.log crit;
    #access_log  logs/access.log  main;
# 启用内核复制模式,应该保持开启达到最快IO效率
    sendfile        on;
# 简单说,启动如下两项配置,会在数据包达到一定大小后再发送数据
# 这样会减少网络通信次数,降低阻塞概率,但也会影响响应及时性
# 比较适合于文件下载这类的大数据包通信场景
    #tcp_nopush     on; 在 
#tcp_nodelay on|off on禁用Nagle算法 
    #keepalive_timeout  0;
# HTTP1.1支持持久连接alive
# 降低每个连接的alive时间可在一定程度上提高可响应连接数量,所以一般可适当降低此值
    keepalive_timeout  30s;
# 启动内容压缩,有效降低网络流量
    gzip on;
# 过短的内容压缩效果不佳,压缩过程还会浪费系统资源
 gzip_min_length 1000;
# 可选值1~9,压缩级别越高压缩率越高,但对系统性能要求越高
gzip_comp_level 4;
# 压缩的内容类别
 gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# 静态文件缓存
# 最大缓存数量,文件未使用存活期
open_file_cache max=655350 inactive=20s;
# 验证缓存有效期时间间隔
open_file_cache_valid 30s;
# 有效期内文件最少使用次数
open_file_cache_min_uses 2;
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            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;
    #    }
    #}
}


2 tomcat 优化

1启动优化 内存

# windows下设置方法

#set JAVA_OPTS=%JAVA_OPTS% -server -Xms512m -Xmx512m -XX:PermSize=512M -XX:MaxPermSize=512m 

# 通过内存设置充分利用服务器内存

# -server模式启动应用慢,但可以极大程度提高运行性能

# java8开始,PermSize被MetaspaceSize代替,MetaspaceSize共享heap,不会再有java.lang.OutOfMemoryError: PermGen space,可以不设置

# headless=true适用于linux系统,与图形操作有关,如生成验证码,含义是当前使用的是无显示器的服务器,应用中如果获取系统显示有关参数会抛异常

# 可通过jmap -heap proccess_id查看设置是否成功

JAVA_OPTS=$JAVA_OPTS -server -Xms2048m -Xmx2048m -XX:PermSize=256m -XX:MaxPermSize=512m -Djava.awt.headless=true

2 配置优化 连接数

<!--
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
-->
<!-- protocol 启用 nio模式,(tomcat8默认使用的是nio)(apr模式利用系统级异步io) -->
<!-- minProcessors最小空闲连接线程数-->
<!-- maxProcessors最大连接线程数-->
<!-- acceptCount允许的最大连接数,应大于等于maxProcessors-->
<!-- enableLookups 如果为true,requst.getRemoteHost会执行DNS查找,反向解析ip对应域名或主机名-->
<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" 
connectionTimeout="20000"
        redirectPort="8443
maxThreads=“500” 
minSpareThreads=“100” 
maxSpareThreads=“200”
acceptCount="200"
enableLookups="false"
/>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值