天天看點

詳解nginx.conf檔案配置項(包括負載均衡)

#運作使用者

user  nobody nobody;

#啟動程序

worker_processes  2;

#全局錯誤日志及PID檔案

error_log  logs/error.log notice;

pid        logs/nginx.pid;

#工作模式及連接配接數上限

events {

        use epoll;

        worker_connections      1024;

}

#設定http伺服器,利用它的反向代理功能提供負載均衡支援

http {

        #設定mime類型

        include       conf/mime.types;

        default_type  application/octet-stream;

        #設定日志格式

        log_format main         '$remote_addr - $remote_user [$time_local] '

                                                '"$request" $status $bytes_sent '

                                                '"$http_referer" "$http_user_agent" '

                                                '"$gzip_ratio"';

        log_format download '$remote_addr - $remote_user [$time_local] '

                                                '"$http_range" "$sent_http_content_range"';

        #設定請求緩沖

        client_header_buffer_size    1k;

        large_client_header_buffers  4 4k;

        #開啟gzip子產品

        gzip on;

        gzip_min_length  1100;

        gzip_buffers     4 8k;

        gzip_types       text/plain;

        output_buffers   1 32k;

        postpone_output  1460;

        #設定access log

        access_log  logs/access.log  main;

        client_header_timeout  3m;

        client_body_timeout    3m;

        send_timeout           3m;

        sendfile                on;

        tcp_nopush              on;

        tcp_nodelay             on;

        keepalive_timeout  65;

        #設定負載均衡的伺服器清單

        upstream mysvr {

                #weigth參數表示權值,權值越高被配置設定到的幾率越大

                #本機上的Squid開啟3128端口

                server 192.168.8.1:3128 weight=5;

                server 192.168.8.2:80   weight=1;

                server 192.168.8.3:80   weight=6;

        }

        #設定虛拟主機

        server {

                listen          80;

                server_name     192.168.8.1 www.yejr.com;

                charset gb2312;

                #設定本虛拟主機的通路日志

                access_log  logs/www.yejr.com.access.log  main;

                #如果通路 /img/*, /js/*, /css/* 資源,則直接取本地檔案,不通過squid

                #如果這些檔案較多,不推薦這種方式,因為通過squid的緩存效果更好

                location ~ ^/(img|js|css)/  {

                        root    /data3/Html;

                        expires 24h;

                }

                #對 "/" 啟用負載均衡

                location / {

                        proxy_pass      http://mysvr;

                        proxy_redirect          off;

                        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;

                        proxy_send_timeout      90;

                        proxy_read_timeout      90;

                        proxy_buffer_size       4k;

                        proxy_buffers           4 32k;

                        proxy_busy_buffers_size 64k;

                        proxy_temp_file_write_size 64k;

                #設定檢視Nginx狀态的位址

                location /NginxStatus {

                        stub_status             on;

                        access_log              on;

                        auth_basic              "NginxStatus";

                        auth_basic_user_file  conf/htpasswd;

運作以下指令檢測配置檔案是否無誤:

  如果沒有報錯,那麼就可以開始運作Nginx了,執行以下指令即可:

  備注:conf/htpasswd 檔案的内容用 apache 提供的 htpasswd 工具來産生即可,内容大緻如下:

  3.) 檢視 Nginx 運作狀态

  輸入位址 http://192.168.8.1/NginxStatus/,輸入驗證帳号密碼,即可看到類似如下内容:

Active connections: 291

server accepts handled requests

16630948 16630948 31070465

Reading: 6 Writing: 179 Waiting: 106active connections -- 對後端發起的活動連接配接數

server accepts handled requests -- nginx 總共處理了 16630948 個連接配接, 成功建立 16630948 次握手 (證明中間沒有失敗的), 總共處理了 31070465 個請求 (平均每次握手處理了 1.8個資料請求)

reading -- nginx 讀取到用戶端的Header資訊數

writing -- nginx 傳回給用戶端的Header資訊數

waiting -- 開啟 keep-alive 的情況下,這個值等于 active - (reading + writing),意思就是Nginx說已經處理完正在等候下一次請求指令的駐留連接配接

作者:​​水木​​    

繼續閱讀