转自http://www.cnblogs.com/1214804270hacker/p/9299462.html

 

一、认识访问静态资源与访问动态资源的区别

  静态资源:指存储在硬盘内的数据,固定的数据,不需要计算的数据。

  如:图片、字体、js文件、css文件等等。在用户访问静态资源时,服务器会直接将这些资源返回到用户的计算机内。

  

  动态资源:指需要服务器根据用户的操作所返回的数据,以及存储在数据库的数据,经过一系列逻辑计算后返回的数据。

  如:请求明天的天气信息数据、请求查看账户余额。

 

二、请求动态数据与请求静态资源的分离的必要性

  Tomcat应用服务器是用来处理Servlet容器和JSP的,虽然它也可以处理HTML等等一系列静态资源,但是效率不如Nginx;而且对Servlet容器和JSP的运算已经有很大压力了,如果不分离会导致大量的性能浪费。说到底,在应用服务方面,要遵循一条原则——一个服务只做一件事。要做动态请求就专做动态请求,要做静态请求就专做静态请求,这样才能提高性能。

   

  我们要做的,就是当用户访问静态资源时,让Nginx将静态资源返回给用户;当用户访问动态资源时,将访问转到Tomcat应用服务器上,Tomcat将数据返回给Nginx,Nginx再返回给用户。

 

三、Nginx配置方法

  在这里,对于Nginx的配置文件内的各项参数说明不多讲解,如需了解Nginx配置文件移步这里

  不知道配置文件位置的,一条指令:

sudo find / -name nginx.conf

  要善于利用Linux指令,这样就会无法自拔的爱上Linux;

  先来一个全部配置:

# user www www;
user root root;

worker_processes 2; #设置值和CPU核心数一致

error_log /home/zuoyu/ServerComputer/nginx/logs/nginx_error.log crit; #日志位置和日志级别


pid /home/zuoyu/ServerComputer/nginx/nginx.pid;

worker_rlimit_nofile 65535;

events {
    #使用epoll模型提高性能
    use epoll;
    #单个进程最大连接数
    worker_connections 65535;
}


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"';

    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 8m;
    types_hash_max_size 2048;
    types_hash_bucket_size 128;
     
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 60;
    tcp_nodelay on;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    # 解压缩传输
    gzip on; 
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;

    #负载均衡组
    #静态服务器组
    upstream static.zuoyu.com {
        server localhost:81;
    }

    #动态服务器组
    upstream dynamic.zuoyu.com {
        server localhost:8080;
        # server localhost:8081;
        # server localhost:8082;
        # server localhost:8083;
    }

    #配置代理参数
    proxy_redirect off;
    proxy_set_header HOST $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    # client_max_body_size 10m;
    client_body_buffer_size 128k;
    proxy_connect_timeout 90;
    proxy_send_timeout 90;
    proxy_read_timeout 90;
    proxy_buffer_size 16k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
    
    #缓存配置
    proxy_cache_key '$host:$server_port$request_uri';
    # proxy_temp_file_write_size 64k;
    proxy_temp_path /home/zuoyu/ServerComputer/nginx/proxy_temp_path;
    proxy_cache_path /home/zuoyu/ServerComputer/nginx/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=5d max_size=1g;
    proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;

    #静态资源主机
    server {
        listen 81;
        server_name localhost_0;
        charset utf8;

        location / {
            root /home/zuoyu/Public/NginxStaticSource/static;
        }
    }
    # 下面是server虚拟主机的配置
    server {
        listen 80;#监听端口
        server_name localhost_1;#域名
        charset utf8;

        location / {
            # root   /usr/share/nginx/html;
            proxy_pass http://dynamic.zuoyu.com;
            index  index.html index.jsp;
        }


        location ~ .*\.(jsp|do|action)$
        {
            index index.jsp;
            proxy_pass http://dynamic.zuoyu.com;
            
        }

    

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico|svg)$
        {
            #缓存30天
            expires 30d;
            proxy_pass http://static.zuoyu.com;
            proxy_cache cache_one;
            proxy_cache_valid 200 304 302 5d;
            proxy_cache_valid any 5d;
            proxy_cache_key '$host:$server_port$request_uri';
            add_header X-Cache '$upstream_cache_status from $host';
        }

        location ~ .*\.(ttf|woff|woff2)$
        {
            #缓存30天
            expires 30d;
            proxy_pass http://static.zuoyu.com;
            proxy_cache cache_one;
            proxy_cache_valid 200 304 302 5d;
            proxy_cache_valid any 5d;
            proxy_cache_key '$host:$server_port$request_uri';
            add_header X-Cache '$upstream_cache_status from $host';
        }

        location ~ .*\.(js|css)$
        {
            #缓存7天
            expires 7d;
            proxy_pass http://static.zuoyu.com;
            proxy_cache cache_one;
            proxy_cache_valid 200 304 302 5d;
            proxy_cache_valid any 5d;
            proxy_cache_key '$host:$server_port$request_uri';
            add_header X-Cache '$upstream_cache_status from $host';
        }

        #其他页面反向代理到tomcat容器
        location ~ .*$ {
            index index.jsp index.html;
            proxy_pass http://dynamic.zuoyu.com;
        }
        access_log off;    
        error_page   500 502 503 504  /50x.html;

        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }

    
}
View Code

相关文章:

  • 2021-08-27
  • 2021-04-15
  • 2022-12-23
  • 2021-11-09
猜你喜欢
  • 2022-01-17
  • 2022-01-06
  • 2022-12-23
  • 2021-08-15
  • 2020-07-21
  • 2022-01-04
  • 2021-05-05
相关资源
相似解决方案