1. 程式人生 > >Nginx動靜分離及效能調優實踐

Nginx動靜分離及效能調優實踐

直接看配置檔案!直接看配置檔案!直接看配置檔案!

#user  nobody;
worker_processes  8;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

#----------------Nginx優化引數---------------Start-----------------###
worker_rlimit_nofile 65535;
#----------------Nginx優化引數---------------End-------------------###
events {
    worker_connections  1024;
	use epoll;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #----------------Nginx優化引數--------------Start------------------###
	open_file_cache          max=2048 inactive=20s;	#最多快取n個檔案,快取多少時間
    open_file_cache_valid    30s;	#多少時間檢查一次,如果發現30s內沒有用過2次的刪除
    open_file_cache_min_uses 2;	#在30S中沒有使用到這個配置的次數的話就刪除
    open_file_cache_errors   on;
	
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 15;
    
	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
	gzip  on;
    gzip_disable     "MSIE [1-6]\.";
    gzip_proxied any;
	gzip_http_version 1.1;
    gzip_comp_level 4;
    gzip_buffers 16 8k;
    gzip_min_length    1k;
    gzip_vary on;
    gzip_types text/plain text/css image/gif image/jpeg image/png application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
   
	#----------------Nginx優化引數---------------End-------------------###
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" '
		      '$upstream_addr $upstream_response_time $request_time '
		      '$http_true_client_ip ';
    #---優化專案----------------關閉Nginx訪問日誌
    # access_log  logs/access.log  main;
    server_tokens off;  #關閉版本顯示 
	
    sendfile        on;
 
	#防止ddos攻擊
    client_header_timeout 10;
    client_body_timeout 10;
	
    #h5-no session
	upstream balanceNoSessionServer{
	     #---優化專案----------------轉換為本機物理IP
		 server 10.3.1.18:8003 ;
		 server 10.3.1.18:8004 ;
		 server 10.3.1.18:8005 ;
		 server 10.3.1.18:8006 ;
	}
	#h5-star service 
	upstream balanceSessionServer{
	     sticky httponly;
		 #---優化專案----------------轉換為本機物理IP
		 server 10.3.1.18:8003 ;
		 server 10.3.1.18:8004 ;
		 server 10.3.1.18:8005 ;
		 server 10.3.1.18:8006 ;
	}
    server {
        listen       8088;
        server_name  localhost;

        #charset koi8-r;
		
		#--------------------------動靜分離-----------------靜態資源對映-------------------#
        location ~ /(proh5|proh5-itr-services)/.*\.(gif|jpg|jpeg|png|bmp|ico|css|js|html)$ {
		   #指定靜態資源根目錄
		   #靜態資源查詢邏輯為:/app/nginx/static/proh5/index.html
		   #                    /app/nginx/static/proh5-itr-services/index.html
		   root /app/nginx/static;
		}
		
		#--------------------------動靜分離-----------------動態資源對映-------------------#
        location ~ /proh5/.*\.(jsp|do|action|jspx)$ {
            proxy_pass  http://balanceNoSessionServer$request_uri; 
			proxy_set_header X-Forwarded-Host $host;
			proxy_set_header X-Forwarded-Server $host;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header X-Real-IP  $remote_addr;  #支撐後端獲取源IP
			proxy_redirect off;
			proxy_set_header Host $http_host;
			proxy_set_header Connection Close;
			port_in_redirect off;
			proxy_connect_timeout 60s;
			proxy_read_timeout 60s;
			proxy_send_timeout 60s;
			proxy_buffer_size 4k;
			proxy_buffers 6 32k;
			proxy_busy_buffers_size 64k;
			proxy_temp_file_write_size 64k;
			proxy_intercept_errors off;
			#---------------------------------Nginx優化引數--------------Start------------------###
            client_body_buffer_size 128k;
        }
		location ~ /proh5-itr-services/.*\.(jsp|do|action|jspx)$ {
            proxy_pass  http://balanceSessionServer$request_uri; 
			proxy_set_header X-Forwarded-Host $host;
			proxy_set_header X-Forwarded-Server $host;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header X-Real-IP  $remote_addr;   #支撐後端獲取源IP
			proxy_redirect off;
			proxy_set_header Host $http_host;
			proxy_set_header Connection Close;
			port_in_redirect off;
			proxy_connect_timeout 60s;
			proxy_read_timeout 60s;
			proxy_send_timeout 60s;
			proxy_buffer_size 4k;
			proxy_buffers 6 32k;
			proxy_busy_buffers_size 64k;
			proxy_temp_file_write_size 64k;
			proxy_intercept_errors off;
			#---------------------------------Nginx優化引數--------------Start------------------###
            client_body_buffer_size 128k;
        }
		
    	location ~ ^/WEB-INF/ {
        	deny  all;
    	}

        location /res {
            alias   /app2/resource/images;
            index  index.html index.htm;
			expires 1d;
        }

        #設定健康檢查頁面
		location = /healthcheck.html {
            root   /app/nginx/html;
			access_log off; #關閉健康檢查訪問日誌
        }
		#設定404頁面[/app/nginx/html目錄下的檔案]
		error_page  404 403             /404.html;
		location = /404.html {
            root   /app/nginx/html;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /app/nginx/html;
        }
        
    }
}