天天看點

nginx配置優化+負載均衡+動靜分離(附帶參數解析)

#指定nginx程序運作使用者以及使用者組

user www www;

#nginx要開啟的程序數為8

worker_processes  8;

#全局錯誤日志檔案

#debug輸出日志最為詳細,而crit輸出日志最少/var/log目錄是linux下的日志存放目錄

error_log /var/log/nginx/nginx_error.log crit;

#指定程序id的存儲位置

pid   /var/run/nginx.pid;

#綁定worker程序和CPU,linux核心2.4以上可用

worker_rlimit_nofile 51200;

#nginx的工作模式及連接配接輸上線

events {

    #nginx工作模式,epoll是linux平台下的高效模式

    use epoll;

    #定義nginx每個程序的最大連接配接數為51200,一般網上都配置65535,根據張宴大神的建議51200即可

    worker_connections  51200;

}

http {

    #實作對配置檔案所包含的檔案的設定

    include       mime.types;

    #設定預設類型為二進制流

    default_type  application/octet-stream;

    server_names_hash_bucket_size  128;

    #指定來自用戶端請求頭的headerbuffer大小,設定為32KB

    client_header_buffer_size  32k;

    #指定用戶端請求中較大的消息頭的緩存最大數量和大小,這裡是4個32KB

    large_client_header_buffers 4 32k;

    #上傳檔案大小

    client_max_body_size 356m;

    #nginx的HttpLog子產品指定,指定nginx日志的輸出格式,輸出格式為access

    log_format access '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

    #access日志存在未知

    access_log  /var/log/nginx/access.log  access;

    #開啟高效模式檔案傳輸模式,将tcp_nopush和tcp_nodely兩個指另設定為on,用于防止網絡阻塞。

    sendfile        on;

    tcp_nopush      on;

    tcp_nodely      on;

    #設定用戶端連接配接保持活動的逾時時間

    keepalive_timeout  65;

    server_tokens   off;

    #用戶端請求主體讀取緩存

    client_body_buffer_size  512k;

    proxy_connect_timeout    5;

    proxy_send_timeout      60;

    proxy_read_timeout       5;

    proxy_buffer_size       16k;

    proxy_buffers         4 64k;

    proxy_busy_buffers_size  128k;

    proxy_temp_file_write_size 128k;    

    #fastcgi_connect_timeout 300;

    #fastcgi_send_timeout    300;

    #fastcgi_read_timeout    300;

    #fastcgi_buffer_timeout  300;

    #fastcgi_buffers 4 64k;

    #fastcgi_busy_buffers_size 128k;

    #fastcgi_temp_file_write_size 128k;

    #開啟gzip

    gzip  on;

    #允許壓縮的最小位元組數

    gzip_min_length 1k;

    #4個機關為16k的記憶體作為壓縮結果流緩存

    gzip_buffers 4 16k;

    #設定識别HTTP協定版本,預設是1.1

    gzip_http_version 1.1;

    #gzip壓縮比,可在1~9中設定,1壓縮比最小,速度最快,9壓縮比最大,速度最慢,消耗CPU

    gzip_comp_level  2;

    #壓縮的類型

    gzip_types text/plain application/x-javascript text/css application/xml;

    #讓前端的緩存伺服器混村經過的gzip壓縮的頁面

    gzip_vary  on;

    #負載均衡

    upstream localhost.com {

        #每個請求按照ip的hash結果配置設定,同一個ip的訪客固定通路一個後端伺服器,可解決動态網頁session共享問題。

    ip_hash;

        server 127.0.0.1:8080;

        server 127.0.0.1:8082;

    }

   禁止通過ip通路

    server {

      server_name _;

       return 404;

        listen       88;

    charset    utf-8; #設定編碼為utf-8

        index index.html index.htm index.jsp index.do index.action;

    #指定虛拟主機根目錄為/var/www/

        root /var/www/;

        if ( -d $request_filename)

        {

           rewrite ^/(.*)([^/])$ http://$host/$1$2/permanent;

        }

      #方法1:将jsp等動态檔案交給的localhost.com處理,此方法用的不多

           location ~ \.(jsp|jspx|do|action)(\/.*)?$ {

              index index.jsp;

              proxy_set_header  Host $host;  

              proxy_set_header  X-Real-IP  $remote_addr;

           } 

       #方法2:将所有請求交給localhost.com處理,靜态檔案通過額外定義location交給nginx處理。

      location / {

         #當502或504時,将請求轉發到負載均衡中正常server中

         proxy_next_upstream http_502 http_504 error timeout invalid_header;

             proxy_redirect    off;

             proxy_set_header Host  $host;

         #若nginx為最前端時,後端獲得X-Real-IP傳遞的ip即為實際ip,若nginx不是最前端時,實際ip為X-Forwarded-For值。

             proxy_set_header X-Forwarded-For  $remote_addr;

          }

       #靜态檔案交給nginx處理

          location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$

          {

                root /var/www;

                expires   30d;

           }

           location ~ .*\.(js|css)?$     

           {

                root /var/www;       

                expires      1h;     

           location /media {

            #指定後端伺服器位址和端口

        #proxy_next_upstream 故障轉移待空

                proxy_redirect     off; 

        #後端伺服器擷取使用者的主機名或真實IP位址

                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;

        #後端伺服器的資料回傳時間90s,90s内未傳回,nginx将斷開連接配接

                proxy_send_timeout         90;   

        #nginx從代理的後端伺服器擷取資訊的時間90s

                proxy_read_timeout         90;   

        #緩沖區大小預設等于proxy_buffers設定的大小

                proxy_buffer_size          4k;

        #設定緩沖區的數量和大小

                proxy_buffers              4 32k;

        #設定系統很忙時可以使用的proxy_buffers的大小,官方推薦位proxy_buffersX2

                proxy_busy_buffers_size    64k;   

        #指定proxy緩存臨時檔案的大小

                proxy_temp_file_write_size 64k;

           location /files/ {

            #靜止外部通路

                internal;

        #

                alias     /home/nfs/media/; 

    # HTTPS server配置

        listen       443;

        root /var/www/webapps;

        ssl on;

        ssl_certificate      /usr/local/nginx/conf/test.crt;

        ssl_certificate_key  /usr/local/nginx/conf/test_nopass.key;

        ssl_session_timeout  5m;

        ssl_protocols  SSLv2 SSLv3 TLSv1;

        ssl_ciphers  HIGH:!aNULL:!MD5;

        ssl_prefer_server_ciphers   on;

        location / {

            root   html;

            index  index.html index.htm;

     本文轉自yzy121403725 51CTO部落格,原文連結:http://blog.51cto.com/lookingdream/1830774,如需轉載請自行聯系原作者