nginx 詳細配置例子

文件結構

...              #全局塊

events {         #events塊
   ...
}

http      #http塊
{
    ...   #http全局塊
    server        #server塊
    { 
        ...       #server全局塊
        location [PATTERN]   #location塊
        {
            ...
        }
        location [PATTERN] 
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...     #http全局塊
}

例子

#!nginx
# 使用的用戶和組,默認爲nobody nobody
user  www www;
# 指定工做衍生進程數,默認爲1
worker_processes  2;
# 指定 pid 存放的路徑
pid /var/run/nginx.pid;

# 制定日誌路徑,級別
# 級別能夠在下方直接使用 [ debug | info | notice | warn | error | crit ]  參數
error_log  /var/log/nginx.error_log  info;

events {
    # 容許的鏈接數
    connections   2000;
    # use [ kqueue | rtsig | epoll | /dev/poll | select | poll ] ;
    # 具體內容查看 http://wiki.codemongers.com/事件模型
    use kqueue;
}

http {
    # 文件擴展名與文件類型映射表
    include       conf/mime.types;
    # 文件擴展名與文件類型映射表
    default_type  application/octet-stream;

    # 自定義格式 main
    log_format main      '$remote_addr - $remote_user [$time_local]  '
    '"$request" $status $bytes_sent '
    '"$http_referer" "$http_user_agent" '
    '"$gzip_ratio"';
    # 自定義格式 download
    log_format download  '$remote_addr - $remote_user [$time_local]  '
    '"$request" $status $bytes_sent '
    '"$http_referer" "$http_user_agent" '
    '"$http_range" "$sent_http_content_range"';

    client_header_timeout  3m;
    client_body_timeout    3m;
    send_timeout           3m;

    client_header_buffer_size    1k;
    large_client_header_buffers  4 4k;

    gzip on;
    gzip_min_length  1100;
    gzip_buffers     4 8k;
    gzip_types       text/plain;

    output_buffers   1 32k;
    postpone_output  1460;

    #容許sendfile方式傳輸文件,默認爲off
    sendfile         on;
    # 每一個進程每次調用傳輸數量不能大於設定的值,默認爲0,即不設上限。
    # sendfile_max_chunk 100k;
    tcp_nopush       on;
    tcp_nodelay      on;
    send_lowat       12000;

    keepalive_timeout  75 20;

    # lingering_time     30;
    # lingering_timeout  10;
    # reset_timedout_connection  on;


    server {
        # 監聽端口
        listen        80;
        # 域名能夠有多個,用空格隔開
        server_name   one.example.com  www.one.example.com;

        access_log   /var/log/nginx.access_log  main; # 日誌格式, log_format main

        # 對 "/" 啓用反向代理
        location / {
            proxy_pass         http://127.0.0.1:8001;
            proxy_redirect     off;

            # 後端的Web服務器能夠經過Host獲取用戶真實Host
            proxy_set_header   Host             $host;
            # 後端的Web服務器能夠經過X-Real-IP獲取用戶真實remote_addr
            proxy_set_header   X-Real-IP        $remote_addr;
            # 後端的Web服務器能夠經過X-Forwarded-For獲取用戶真實IP
            # proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;

            # 容許客戶端請求的最大單文件字節數
            client_max_body_size       10m;
            # 緩衝區代理緩衝用戶端請求的最大字節數
            client_body_buffer_size    128k;

            client_body_temp_path      /var/nginx/client_body_temp;

            # nginx跟後端服務器鏈接超時時間(代理鏈接超時)
            proxy_connect_timeout      90;
            # 後端服務器數據回傳時間(代理髮送超時)
            proxy_send_timeout         90;
            # 鏈接成功後,後端服務器響應時間(代理接收超時)
            proxy_read_timeout         90;
            proxy_send_lowat           12000;

            # 設置代理服務器(nginx)保存用戶頭信息的緩衝區大小
            proxy_buffer_size          4k;
            # proxy_buffers緩衝區,網頁平均在32k如下的設置
            proxy_buffers              4 32k;
            # proxy_buffers緩衝區,網頁平均在32k如下的設置
            proxy_busy_buffers_size    64k;
            # 設定緩存文件夾大小,大於這個值,將從upstream服務器傳
            proxy_temp_file_write_size 64k;

            # 爲存儲承載從代理服務器接收到的數據的臨時文件定義目錄。指定目錄下支持3級子目錄結構
            proxy_temp_path            /var/nginx/proxy_temp;
            
            # 默認編碼
            charset utf-8; 
        }

        error_page  404  /404.html;

        location /404.html {
            root  /spool/www;

            charset         on;
            source_charset  koi8-r;
        }

        location /old_stuff/ {
            rewrite   ^/old_stuff/(.*)$  /new_stuff/$1  permanent;
        }

        location /download/ {

            valid_referers  none  blocked  server_names  *.example.com;

            if ($invalid_referer) {
                #rewrite   ^/   http://www.example.com/;
                return   403;
            }

            #rewrite_log  on;

            # rewrite /download/*/mp3/*.any_ext to /download/*/mp3/*.mp3
            rewrite ^/(download/.*)/mp3/(.*)\..*$
            /$1/mp3/$2.mp3                   break;

            root         /spool/www;
            #autoindex    on;
            access_log   /var/log/nginx-download.access_log  download;
        }

        location ~* ^.+\.(jpg|jpeg|gif)$ {
            root         /spool/www;
            access_log   off;
            expires      30d;
        }
    }
}
相關文章
相關標籤/搜索