nginx配置說明

在此記錄下Nginx服務器nginx.conf的配置文件說明, 部分註釋收集於網絡.


user    www-data;                        #運行用戶

worker_processes  1;                  #啓動進程,通常設置成和cpu的數量相等


error_log  /var/log/nginx/error.log;   #全局錯誤日誌及PID文件
pid        /var/run/nginx.pid;


events {
   use   epoll;                                #工作模式及連接數上限

  #epoll是多路複用IO(I/O Multiplexing)中的一種方式,但是僅用於linux2.6以上內核,可以大大提高nginx的性能

   worker_connections  1024;    

  #單個後臺worker process進程的最大併發鏈接數
    # multi_accept on; 
}

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

http {
     
    include       /etc/nginx/mime.types;       #設定mime類型,類型由mime.type文件定義
    default_type  application/octet-stream;


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

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';


    access_log  logs/access.log  main;       #設定日誌格式

 

    
    sendfile        on;                    #sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,必須設爲 on,如果用來進行下載等應用磁盤IO重負載應用,可設置爲 off,以平衡磁盤與網絡I/O處理速度,降低系統的uptime.
    #tcp_nopush     on;

   
    #keepalive_timeout  0;
    keepalive_timeout  65;                 #連接超時時間
    tcp_nodelay        on;
    
    gzip  on;                                      #開啓gzip壓縮
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

   
    client_header_buffer_size    1k;    #設定請求緩衝
    large_client_header_buffers  44k;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;


    #設定負載均衡的服務器列表
     upstream mysvr {
    
    
    server 192.168.8.1:3128 weight=5;      #本機上的Squid開啓3128端口
    server 192.168.8.2:80  weight=1;
    server 192.168.8.3:80  weight=6;        #weigth參數表示權值,權值越高被分配到的機率越大
    }


   server {
    
        listen       80;                       #偵聽80端口
       server_name  www.xx.com;   #定義使用www.xx.com訪問

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

                                                   #設定本虛擬主機的訪問日誌

    #默認請求
    location / {            #location允許對不同的URL使用不同的配置,即可以使用字符串也可以使用與正則表達式。
          root    /root;                       #定義服務器的默認網站根目錄位置
          index  index.php  index.html  index.htm;   #定義首頁索引文件的名稱

         fastcgi_pass  www.xx.com;
         fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name; 
          include /etc/nginx/fastcgi_params;
        }

  
        error_page   500 502 503 504 /50x.html;    # 如果出現指定的http錯誤狀態碼,則返回給客戶端指定的url地址。
        location = /50x.html {
        root   /root;
    }


-----------------------------------

生產環境中一例(29.70-video部分):(aaron  2011-08-11)

 

 error_page  404 = @fetch ;      #如果響應錯誤代碼的頁面是php等FastCGI程序,則最好在error_page中加上等號,如本例示

        location @fetch {   #location項允許對不同的路徑進行不同的配置。@爲一命名標記,不會處理正常請求,只會處理內部重定向。

            internal;         #設定某個location路徑只能在內部使用,不能用於外部。

            proxy_store             on;  #存儲從後端服務器傳過來的文件。

            proxy_next_upstream error timeout http_500 http_503 http_404;

                                  #在哪種情況下將請求轉發到負載均衡代理服務器池中的下一臺服務器。

            proxy_store_access      user:rw  group:rw  all:r;

                                  #指定用於創建文件和目錄的權限。

            proxy_temp_path /tmp;     #指定用於緩衝較大代理請求的本地路徑。

            proxy_set_header  Host  root.video.att.xinpindao.com;

                                 #在轉發給反向代理服務器(後端)的信息中重新定義或者設置header行。(即更改轉發給反向代理服務器的信息)

            proxy_set_header  referer  http://video.att.xinpindao.com;

            proxy_pass   http://video.backend; #設置被代理服務器的端口或套接字,以及URL。

            #default root same as server root

            #root  /data/attachment/video;

        }

------------------------------------------

 

    #靜態文件,nginx自己處理
    location ~ ^/(images|javascript|js|css|flash|media|static)/ {
        root /var/www/virtual/htdocs;
        expires 30d;  #過期30天,靜態文件不怎麼更新,過期可以設大一點,如果頻繁更新,則可以設置得小一點。
    }
    #PHP 腳本請求全部轉發到 FastCGI處理. 使用FastCGI默認配置.
    location ~ \.php$ {
        root /root;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;
        include fastcgi_params;
    }
    #設定查看Nginx狀態的地址
    location /NginxStatus {
        stub_status            on;
        access_log              on;
        auth_basic              "NginxStatus";
        auth_basic_user_file  conf/htpasswd;
    }
    #禁止訪問 .htxxx 文件
    location ~ /\.ht {
        deny all;
    }
     
     }
}

以上是一些基本的配置,使用Nginx最大的好處就是負載均衡

如果要使用負載均衡的話,可以修改配置http節點如下:

#設定http服務器,利用它的反向代理功能提供負載均衡支持
http {
     
     include       /etc/nginx/mime.types;  #設定mime類型,類型由mime.type文件定義
    default_type  application/octet-stream;
    
    access_log    /var/log/nginx/access.log;  #設定日誌格式

    #省略上文有的一些配置節點

    #。。。。。。。。。。

    #設定負載均衡的服務器列表
     upstream mysvr {             #upstream指令用於設置一組可以在proxy_pass指令中使用的代理服務器,默認的代理方式爲輪詢。
    
    server 192.168.8.1x:3128 weight=5;#本機上的Squid開啓3128端口
    server 192.168.8.2x:80  weight=1;   #server指令用於指定後端服務器的名稱和參數。
    server 192.168.8.3x:80  weight=6;   #weigth參數表示權值,權值越高被分配到的機率越大
    }

   upstream mysvr2 {
    server 192.168.8.x:80  weight=1;

    server 192.168.8.x:80  weight=6;  #weigth參數表示權值,權值越高被分配到的機率越大
    }

   #第一個虛擬服務器               
   server {            #在server{...}虛擬主機內可通過proxy_pass指令設置關於反向代理的upstream服務器集羣。
    
        listen       80;  #偵聽192.168.8.x的80端口
        server_name  192.168.8.x;

      
    location ~ .*\.aspx$ {     #對aspx後綴的進行負載均衡請求

         root   /root;             #定義服務器的默認網站根目錄位置
          index index.php index.html index.htm;   #定義首頁索引文件的名稱

          proxy_pass  http://mysvr ;                 #請求轉向mysvr 定義的服務器列表

          #以下是一些反向代理的配置可刪除.

          proxy_redirect off;

          #後端的Web服務器可以通過X-Forwarded-For獲取用戶真實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;     #nginx跟後端服務器連接超時時間(代理連接超時)
          proxy_send_timeout 90;         #後端服務器數據回傳時間(代理髮送超時)
          proxy_read_timeout 90;         #連接成功後,後端服務器響應時間(代理接收超時)
          proxy_buffer_size 4k;            #設置代理服務器(nginx)保存用戶頭信息的緩衝區大小
          proxy_buffers 4 32k;            #proxy_buffers緩衝區,網頁平均在32k以下的話,這樣設置
          proxy_busy_buffers_size 64k;    #高負荷下緩衝大小(proxy_buffers*2)
          proxy_temp_file_write_size 64k;  #設定緩存文件夾大小,大於這個值,將從upstream服務器傳

       }

     }
}

發佈了15 篇原創文章 · 獲贊 35 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章