1. 程式人生 > >nginx+tomcat單站點本地代理優化筆錄(1)

nginx+tomcat單站點本地代理優化筆錄(1)

單臺tomcat,使用nginx合理搭配,無論從安全、效率方面都會有一定的提升。

從nginx配置出發

1、開啟gzip壓縮。

gzip on;
gzip_min_length 1000;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript;

2、配置html靜態快取

http下配置

 proxy_connect_timeout 600;
	proxy_read_timeout 600;
	proxy_send_timeout 600;
	proxy_buffer_size 16k;
	proxy_buffers 4 128k;
	proxy_busy_buffers_size  256k;    
	proxy_temp_file_write_size  1024k;	
	proxy_cache_path /tmp/proxy_cache levels=1:2 keys_zone=cache_one:200m inactive=30d max_size=2g;
	proxy_temp_path /tmp/proxy_cache/temp;

server下配置

location ~ .*\.html(.*)$ {
              proxy_pass http://127.0.0.1:8080;
              proxy_redirect off;
              proxy_set_header Host $host;
              proxy_cache cache_one;
              #對應狀態快取週期
              proxy_cache_valid 200 302 6h;
              proxy_cache_valid 301 12h;
              proxy_cache_valid any 1m;
              expires 4h;
              proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie";
              add_header Nginx-Cache "$upstream_cache_status";
        }

3、其他靜態資源由nginx直接處理

location ~ ^/(.*)\.(js|css|jpg|jpeg|gif|png|ico)(.*)$ {
              #proxy_pass http://127.0.0.1:8080;
              #proxy_redirect off;
              #proxy_set_header Host $host;
              #proxy_cache cache_one;
              #proxy_cache_valid 200 302 30d;
              #proxy_cache_valid 301 1d;
              #proxy_cache_valid any 1d;
              #expires 30d;
              #proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie";
              #add_header Nginx-Cache "$upstream_cache_status";
	      root /usr/local/tomcat/webapps/ROOT;
	      #access_log off;
              expires 24h;
        }

4、tomcat後臺處理其他動態請求

location / {
            proxy_pass http://127.0.0.1:8080;
            proxy_set_header        Host $host;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        }