nginx 反向代理、緩存

  lvs+keepalive+nginx(realserver)兩臺+tomcat(後端服務器),nginx的配置文件nginx.conf如下

user  nobody nobody;
worker_processes 12;
error_log /var/log/nginx/error.log crit;(取消記錄錯誤日誌)
#error_log  /var/log/nginx/debug.log  debug_http;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
pid /var/run/nginx.pid;
worker_rlimit_nofile 65535;(進程能夠打開的最多文件描述符數)
events {
    use epoll;
    worker_connections  65535;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    gzip on;  #開啓gzip壓縮
    gzip_buffers 4 8k; #指定緩存壓縮應答的緩衝區數量和大小
    gzip_comp_level 6; #指定壓縮等級,其值從1到9,1爲最小化壓縮(處理速度快)
    gzip_min_length 1k;#設置被壓縮的最小請求,單位爲bytes。少於這個值大小的請求將不會被壓縮
    gzip_http_version 1.1; #是否根據HTTP請求版本來啓用gzip壓縮
    #gzip_proxied expired no-cache no-store private auth any off;
    gzip_proxied any; #爲所有請求啓用壓縮
    gzip_types text/plain application/x-javascript text/css application/xml; #爲除“text/html”之外的MIME類型啓用壓縮,“text/html”總是會被壓縮
    gzip_vary off; #關閉應答頭“Vary: Accept-Encoding”
    sendfile on; #sendfile()函數在一個文件描述符與另一個之間拷貝數據,由於這個拷貝過程是在內核狀態下完成
    tcp_nodelay on;#這個指令指定是否使用socket的TCP_NODELAY選項,這個選項只對keep-alive連接有效
    keepalive_timeout 60; #參數的第一個值指定了客戶端與服務器長連接的超時時間,超過這個時間,服務器將關閉連接
    tcp_nopush on; #這個指令指定是否使用socket的TCP_NOPUSH(FreeBSD)或TCP_CORK(linux)選項,這個選項只在使用sendfile時有效
    server_names_hash_bucket_size       128; #服務器名稱哈希表每個頁框的大小,這個指令的默認值依賴於cpu緩存
    client_header_buffer_size           32k; #指令指定客戶端請求頭部的緩衝區大小
    large_client_header_buffers         8 32k;#指定客戶端一些比較大的請求頭使用的緩衝區數量和大小
    client_max_body_size                8m; #指令指定允許客戶端連接的最大請求實體大小,它出現在請求頭部的Content-Length字段
    client_body_buffer_size 128k; #這個指令可以指定連接請求實體的緩衝區大小
    client_body_temp_path  /tmp/client_temp 1 2; #指令指定連接請求實體試圖寫入的臨時文件路徑
    open_file_cache max=65535 inactive=30s;#這個指令指定緩存是否啓用,如果啓用,將記錄文件以下信息
    #max - 指定緩存的最大數目,如果緩存溢出,最近最少使用的文件(LRU)將被移除
    #inactive - 指定緩存文件被移除的時間,如果在這段時間內文件沒被下載,默認爲60秒)
    open_file_cache_valid 60s;#這個指令指定了何時需要檢查open_file_cache中緩存項目的有效信息
    open_file_cache_min_uses 1;#這個指令指定了在open_file_cache指令無效的參數中一定的時間範圍內可以使用的最小文件數,如果使用更大的值,文件描述符在cache中總是打開狀態
    server_name_in_redirect on; #如果這個指令打開,nginx將使用server_name指定的基本服務器名作爲重定向地址,如果關閉,nginx將使用請求中的主機頭
    server_tokens off;#是否在錯誤頁面和服務器頭中輸出nginx版本信息
    index index.html index.htm index.jsp index.php;
####### begin fastcgi #######
    fastcgi_connect_timeout 30; #指定同FastCGI服務器的連接超時時間,這個值不能超過75秒
    fastcgi_send_timeout 30; #指令爲上游服務器設置等待一個FastCGI進程的傳送數據時間
    fastcgi_read_timeout 30; #前端FastCGI服務器的響應超時時間,如果有一些直到它們運行完纔有輸出的長時間運行的FastCGI進程,或者在錯誤日誌中出現前端服務器響應超時錯誤,可能需要調整這個值
    fastcgi_buffer_size 64k; #這個參數指定將用多大的緩衝區來讀取從FastCGI進程到來應答頭
    fastcgi_buffers 4 64k; #這個參數指定了從FastCGI進程到來的應答,本地將用多少和多大的緩衝區讀取
    fastcgi_busy_buffers_size 128k;#一般設置爲fastcgi_buffer_size的兩倍
    fastcgi_temp_file_write_size 128k;
####### end fastcgi #######
####### begin proxy #######
    proxy_redirect off;
    proxy_connect_timeout 20; #指定一個連接到代理服務器的超時時間,單位爲秒,需要注意的是這個時間最好不要超過75秒
    proxy_send_timeout 30; #設置代理服務器轉發請求的超時時間,單位爲秒
    proxy_read_timeout 30; #決定讀取後端服務器應答的超時時間,單位爲秒,它決定nginx將等待多久時間來取得一個請求的應答
    proxy_buffer_size 32k; #設置從被代理服務器讀取的第一部分應答的緩衝區大小
    proxy_buffers 32 64k; #設置用於讀取應答(來自被代理服務器)的緩衝區數目和大小
    proxy_busy_buffers_size 64k; #爲proxy_buffer_size的兩倍
    proxy_pass_header  Set-Cookie; #這個指令允許爲應答轉發一些隱藏的頭部字段
    fastcgi_pass_header Set-Cookie;
    proxy_temp_path /www/cache/proxy_temp; #類似於http核心模塊中的client_body_temp_path指令,指定一個地址來緩衝比較大的被代理請求
    fastcgi_temp_path  /www/cache/fastcgi_temp; #指令指定存儲從別的服務器傳送來的數據臨時文件路徑,同樣可以指定三級目錄已經哈希存儲
    fastcgi_cache_path /www/cache/cache_t2 levels=1:2 keys_zone=cache_t2:200m inactive=1d max_size=3g;
    proxy_cache_path /www/cache/cache_t1 levels=1:2 keys_zone=cache_t1:200m inactive=1d max_size=3g;
    proxy_cache_path /www/cache/www.test2.com levels=1:2 keys_zone=test2:200m inactive=7d max_size=3g;
    proxy_cache_path /www/cache/www.test1.com levels=1:2 keys_zone=test1:200m inactive=7d max_size=3g;
    proxy_cache_path /www/cache/www.test.com levels=1:2 keys_zone=test:200m inactive=7d max_size=3g;
####### end proxy #######
    log_format access_sinoicity '$remote_addr|$remote_user|[$time_local]|$request|'
   '$status|$body_bytes_sent|$http_referer|'
   '$http_user_agent|$http_x_forwarded_for|$sent_http_content_range|$request_time|$host'
   '$request_body|$upstream_addr';
    log_format forcdn   '$remote_addr|$remote_user|[$time_local]|$request|$status|$body_bytes_sent|'
'$http_referer|$http_user_agent|$http_x_forwarded_for|'
'$http_Cdn_Src_Ip|$http_Via';
#
# The default server
#
server {
    listen 80;
    server_name  localhost;
    access_log off;
    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;
    }
}
    include conf.d/*.conf;
}

虛擬主機配置文件

conf.d/www.test1.com.conf

server {
    listen 80;
    server_name  www.test.com   #  www.test1.com 
    #charset utf-8;
    access_log  /var/log/nginx/www.test.com.log combined;
    location / {
    proxy_set_header Host www.test.com;
    proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_pass http://test;#test1、test2
    proxy_redirect http://www.test.com /;
    }
    #location ~ .*\.(gif|jpg|jpeg|png|bmp|ico|rar|css|js|zip|xml|txt|flv|swf|mid|doc|cur|xls|pdf|txt|mp3|wma)$ {
    location ~ .*\.(css|js|gif|jpg|jpeg|png|bmp|ico|xml|txt|swf|doc|cur|xls)$ {
    #proxy_set_header Host $http_host;
    proxy_set_header Host    #www.test1.com  www.test2.com
    proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_pass_header  Set-Cookie;
    proxy_pass http://test; #test1、test2 
    #add_header Cache-Control "max-age=604800";
    add_header X-Cache "Cached by nginx - sinoicity-01";
    proxy_cache_valid  200 304 7d;
    proxy_cache test; #test1、test2
    #proxy_cache_key $host$uri$is_args$args;
    proxy_cache_key $host$uri$is_args;
    expires 7d;
    }
}

conf.d/upstream.conf

upstream test {
    server 192.168.100.10:80;
}
upstream test1 {
    server 192.168.100.20:80;
}
upstream test2 {
    server 192.168.100.30:80;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章