`
ry.china
  • 浏览: 137178 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

nginx使用缓存并与tomcat结合提速

阅读更多

服务器构架如下nginx+tomcat+mysql

####################nginx.conf#########################

user www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    use epoll;
    worker_connections  10240;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
#这里设置一下log记录方式,方便使用awstats统计。
    log_format main '\$remote_addr - \$remote_user [\$time_local]  '
                        '"\$request" \$status \$body_bytes_sent '
                                            '"\$http_referer"
                                            "\$http_user_agent"';

#log的文件名用host的域名来定,呵呵,省得server那里指定了
    access_log /var/log/nginx/\$host.access.log combined;
    sendfile        on;
#    tcp_nopush      on;

    keepalive_timeout  65;
    tcp_nodelay        on;

    gzip  off;#这里要关闭gzip,因为用代理后,再用gzip会有奇怪的问题

#这里是限制速度
    limit_zone   one  \$binary_remote_addr  10m;
    limit_req_zone  \$binary_remote_addr  zone=rone:10m   rate=10r/s;

#指定缓存文件的目录,如果有多个nginx,这个可以共享使用,呵呵,一般人我不告诉他
    proxy_cache_path /home/web/cache levels=1:2 keys_zone=JianelCache:100m  max_size=100m;

#引入其它的conf
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

#######################server######################

# 定义后台的tomcat,可以加多个,实现负载均衡
  upstream tomcat {
    server 127.0.0.1:8080;
    ip_hash;#根据ip来选择后端的tomcat,使得同一个ip只访问一个tomcat
  }


server {
    listen   80;
    server_name localhost;#域名
    location / {
        root   /var/www;
        index  index.jsp;

#使/abc这样的目录自动转到/abc/
        if (-d \$request_filename){
            rewrite ^/(.*)([^/])\$ http://\$host/\$1\$2/ permanent;
        }
    }



    #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   /var/www/nginx-default;
    }

#这里把首先会查看html文件在不在缓存内,没有就转到后台tomcat处,并把返回内容缓存下来,以被下次使用

#后台的tomcat会把htmlrewrite成jsp再处理,呵呵,这样就不需要真的静态化
    location ~ \.html {
        proxy_set_header        X-Real-IP \$remote_addr;
        proxy_set_header        X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_set_header        Host \$http_host;
        proxy_cache JianelCache;

        proxy_cache_valid 200 302 24h;#200和302状态码保存1小时
        proxy_cache_valid 301 1d;#301状态码保存一天
        proxy_cache_valid any 10h;#其它的保存一分钟
        if ( !-f \$request_filename) {
                proxy_pass  http://tomcat;
        }
    }
#这里把jsp都转到后端tomcat处理
    location ~ \.jsp {
        proxy_pass   http://tomcat;
        proxy_set_header        X-Real-IP \$remote_addr;
        proxy_set_header        X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_set_header        Host \$http_host;

#这里是限速,省得tomcat老是挂掉,呵呵

       limit_conn one 2;
        limit_req zone=rone burst=5;
    }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics