Nginx配置文件詳解


nginx配置解釋官網:http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size


 

一、Nginx配置文件的整體結構

1.1、全局塊

配置影響nginx全局的指令。主要包括:

  • 配置運行Nginx服務器用戶(組)
  • worker process數
  • Nginx進程
  • PID存放路徑錯誤日志的存放路徑
  • 配置文件的引入

1.2、events塊

配置影響nginx服務器或與用戶的網絡連接。主要包括:

  • 設置網絡連接的序列化
  • 是否允許同時接收多個網絡連接
  • 事件驅動模型的選擇
  • 最大連接數的配置

1.3、http塊

可以嵌套多個server,配置代理,緩存,日志定義等絕大多數功能和第三方模塊的配置。主要包括:

  • 定義MIMI-Type
  • 自定義服務日志
  • 允許sendfile方式傳輸文件
  • 連接超時時間
  • 單連接請求數上限

1.4、server塊

配置虛擬主機的相關參數,一個http中可以有多個server。主要包括:

  • 配置網絡監聽
  • 基於名稱的虛擬主機配置
  • 基於IP的虛擬主機配置

1.5、location塊

配置請求的路由,以及各種頁面的處理情況。主要包括:

  • location配置
  • 請求根目錄配置更改
  • location的URI
  • 網站默認首頁配置

1.6、配置清單例析

二、配置文件詳解

2.1 配置文件1

########### 每個指令必須有分號結束。#################
#user administrator administrators;  #配置用戶或者組,默認為nobody nobody。
#worker_processes 2;  #允許生成的進程數,默認為1
#pid /nginx/pid/nginx.pid;   #指定nginx進程運行文件存放地址
error_log log/error.log debug;  #制定日志路徑,級別。這個設置可以放入全局塊,http塊,server塊,級別以此為:debug|info|notice|warn|error|crit|alert|emerg
events {
    accept_mutex on;   #設置網路連接序列化,防止驚群現象發生,默認為on
    multi_accept on;  #設置一個進程是否同時接受多個網絡連接,默認為off
    #use epoll;      #事件驅動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    worker_connections  1024;    #最大連接數,默認為512
}
http {
    include       mime.types;   #文件擴展名與文件類型映射表
    default_type  application/octet-stream; #默認文件類型,默認為text/plain
    #access_log off; #取消服務日志    
    log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定義格式
    access_log log/access.log myFormat;  #combined為日志格式的默認值
    sendfile on;   #允許sendfile方式傳輸文件,默認為off,可以在http塊,server塊,location塊。
    sendfile_max_chunk 100k;  #每個進程每次調用傳輸數量不能大於設定的值,默認為0,即不設上限。
    keepalive_timeout 65;  #連接超時時間,默認為75s,可以在http,server,location塊。

    upstream mysvr {   
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup;  #熱備
    }
    error_page 404 https://www.baidu.com; #錯誤頁
    server {
        keepalive_requests 120; #單連接請求上限次數。
        listen       4545;   #監聽端口
        server_name  127.0.0.1;   #監聽地址       
        location  ~*^.+$ {       #請求的url過濾,正則匹配,~為區分大小寫,~*為不區分大小寫。
           #root path;  #根目錄
           #index vv.txt;  #設置默認頁
           proxy_pass  http://mysvr;  #請求轉向mysvr 定義的服務器列表
           deny 127.0.0.1;  #拒絕的ip
           allow 172.18.5.54; #允許的ip           
        } 
    }
}

2.2 配置文件2

#運行用戶
user nobody;
#啟動進程,通常設置成和cpu的數量相等
worker_processes  1;

#全局錯誤日志及PID文件
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

#工作模式及連接數上限
events {
    #epoll是多路復用IO(I/O Multiplexing)中的一種方式,
    #僅用於linux2.6以上內核,可以大大提高nginx的性能
    use   epoll; 

    #單個后台worker process進程的最大並發鏈接數    
    worker_connections  1024;

    # 並發總數是 worker_processes 和 worker_connections 的乘積
    # 即 max_clients = worker_processes * worker_connections
    # 在設置了反向代理的情況下,max_clients = worker_processes * worker_connections / 4  為什么
    # 為什么上面反向代理要除以4,應該說是一個經驗值
    # 根據以上條件,正常情況下的Nginx Server可以應付的最大連接數為:4 * 8000 = 32000
    # worker_connections 值的設置跟物理內存大小有關
    # 因為並發受IO約束,max_clients的值須小於系統可以打開的最大文件數
    # 而系統可以打開的最大文件數和內存大小成正比,一般1GB內存的機器上可以打開的文件數大約是10萬左右
    # 我們來看看360M內存的VPS可以打開的文件句柄數是多少:
    # $ cat /proc/sys/fs/file-max
    # 輸出 34336
    # 32000 < 34336,即並發連接總數小於系統可以打開的文件句柄總數,這樣就在操作系統可以承受的范圍之內
    # 所以,worker_connections 的值需根據 worker_processes 進程數目和系統可以打開的最大文件總數進行適當地進行設置
    # 使得並發總數小於操作系統可以打開的最大文件數目
    # 其實質也就是根據主機的物理CPU和內存進行配置
    # 當然,理論上的並發總數可能會和實際有所偏差,因為主機還有其他的工作進程需要消耗系統資源。
    # ulimit -SHn 65535

}


http {
    #設定mime類型,類型由mime.type文件定義
    include    mime.types;
    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 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,
    #對於普通應用,必須設為 on,
    #如果用來進行下載等應用磁盤IO重負載應用,可設置為 off,
    #以平衡磁盤與網絡I/O處理速度,降低系統的uptime.
    sendfile     on;
    #tcp_nopush     on;

    #連接超時時間
    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay     on;

    #開啟gzip壓縮
    gzip  on;
    gzip_disable "MSIE [1-6].";

    #設定請求緩沖
    client_header_buffer_size    128k;
    large_client_header_buffers  4 128k;


    #設定虛擬主機配置
    server {
        #偵聽80端口
        listen    80;
        #定義使用 www.nginx.cn訪問
        server_name  www.nginx.cn;

        #定義服務器的默認網站根目錄位置
        root html;

        #設定本虛擬主機的訪問日志
        access_log  logs/nginx.access.log  main;

        #默認請求
        location / {
            
            #定義首頁索引文件的名稱
            index index.php index.html index.htm;   

        }

        # 定義錯誤提示頁面
        error_page   500 502 503 504 /50x.html;
        location = /50x.html {
        }

        #靜態文件,nginx自己處理
        location ~ ^/(images|javascript|js|css|flash|media|static)/ {
            
            #過期30天,靜態文件不怎么更新,過期可以設大一點,
            #如果頻繁更新,則可以設置得小一點。
            expires 30d;
        }

        #PHP 腳本請求全部轉發到 FastCGI處理. 使用FastCGI默認配置.
        location ~ .php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

        #禁止訪問 .htxxx 文件
            location ~ /.ht {
            deny all;
        }

    }
}

2.3 配置文件3

worker_processes 8; nginx進程數,建議設置為等於CPU總核心數.

error_log /var/log/nginx/error.log info; 全局錯誤日志定義類型,[ debug | info | notice | warn | error | crit ]

pid /var/run/nginx.pid; 進程文件

一個nginx進程打開的最多文件描述符數目,理論值應該是最多打開文件數(系統的值ulimit -n)與nginx進程數相除,但是nginx分配請求並不均勻,所以建議與ulimit -n的值保持一致。
worker_rlimit_nofile 65535;

工作模式與連接數上限
events
{
  #參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本內核中的高性能網絡I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
  use epoll;
  #單個進程最大連接數(最大連接數=連接數*進程數)
  worker_connections 65535;
}

設定http服務器
http
{

    include mime.types; #文件擴展名與文件類型映射表
    default_type application/octet-stream; #默認文件類型
    #charset utf-8; #默認編碼
    server_names_hash_bucket_size 128; #服務器名字的hash表大小
    client_header_buffer_size 32k; #上傳文件大小限制
    large_client_header_buffers 4 64k; #設定請求緩
    client_max_body_size 8m; #設定請求緩
    sendfile on; #開啟高效文件傳輸模式,sendfile指令指定nginx是否調用sendfile函數來輸出文件,對於普通應用設為 on,如果用來進行下載等應用磁盤IO重負載應用,可設置為off,以平衡磁盤與網絡I/O處理速度,降低系統的負載。注意:如果圖片顯示不正常把這個改成off。
    autoindex on; #開啟目錄列表訪問,合適下載服務器,默認關閉。
    tcp_nopush on; #防止網絡阻塞
    tcp_nodelay on; #防止網絡阻塞
    keepalive_timeout 120; #長連接超時時間,單位是秒

    #FastCGI相關參數是為了改善網站的性能:減少資源占用,提高訪問速度。下面參數看字面意思都能理解。
    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壓縮輸出
    gzip_min_length 1k; #最小壓縮文件大小
    gzip_buffers 4 16k; #壓縮緩沖區
    gzip_http_version 1.0; #壓縮版本(默認1.1,前端如果是squid2.5請使用1.0)
    gzip_comp_level 2; #壓縮等級
    gzip_types text/plain application/x-javascript text/css application/xml;
    #壓縮類型,默認就已經包含text/html,所以下面就不用再寫了,寫上去也不會有問題,但是會有一個warn。
    gzip_vary on;
    #limit_zone crawler $binary_remote_addr 10m; #開啟限制IP連接數的時候需要使用

    upstream blog.ha97.com {
        #upstream的負載均衡,weight是權重,可以根據機器配置定義權重。weigth參數表示權值,權值越高被分配到的幾率越大。
        server 192.168.80.121:80 weight=3;
        server 192.168.80.122:80 weight=2;
        server 192.168.80.123:80 weight=3;
    }

    虛擬主機的配置
    server
    {
        listen 80;    #監聽端口
        server_name aa.cn www.aa.cn ; #server_name end  #域名可以有多個,用空格隔開
        index index.html index.htm index.php;  # 設置訪問主頁
        set $subdomain '';  # 綁定目錄為二級域名 bbb.aa.com  根目錄 /bbb  文件夾
        if ( $host ~* "(?:(\w+\.){0,})(\b(?!www\b)\w+)\.\b(?!(com|org|gov|net|cn)\b)\w+\.[a-zA-Z]+" ) 
        { 
            set $subdomain "/$2"; 
        }

        root /home/wwwroot/aa.cn/web$subdomain;# 訪問域名跟目錄  
        include rewrite/dedecms.conf; #rewrite end   #載入其他配置文件

        location ~ .*.(php|php5)?$
        {
            fastcgi_pass 127.0.0.1:9000;
          fastcgi_index index.php;
            include fastcgi.conf;
        }
    #圖片緩存時間設置
        location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
        {
        expires 10d;
        }
    #JS和CSS緩存時間設置
        location ~ .*.(js|css)?$
        {
        expiresexpires 1h;
        }

    }

    日志格式設定

    log_format access '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" $http_x_forwarded_for';
    #定義本虛擬主機的訪問日志
    access_log /var/log/nginx/ha97access.log access;

    #對 "/" 啟用反向代理
    location / {
        proxy_pass http://127.0.0.1:88;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;

    #后端的Web服務器可以通過X-Forwarded-For獲取用戶真實IP
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    #以下是一些反向代理的配置,可選。
        proxy_set_header Host $host;
        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服務器傳

    }
    設定查看Nginx狀態的地址

    location /NginxStatus {
        stub_status on;
        access_log on;
        auth_basic "NginxStatus";
        auth_basic_user_file conf/htpasswd;
        #htpasswd文件的內容可以用apache提供的htpasswd工具來產生。
    }

    #本地動靜分離反向代理配置
    #所有jsp的頁面均交由tomcat或resin處理
    location ~ .(jsp|jspx|do)?$ {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:8080;
    }

    #所有靜態文件由nginx直接讀取不經過tomcat或resin
    location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
    {   expires 15d;   }

    location ~ .*.(js|css)?$
    {   expires 1h;   }

}

2.4 配置文件4

######Nginx配置文件nginx.conf中文詳解#####
  
#定義Nginx運行的用戶和用戶組
user www www;
  
#nginx進程數,建議設置為等於CPU總核心數。
worker_processes 8;
  
#全局錯誤日志定義類型,[ debug | info | notice | warn | error | crit ]
error_log /usr/local/nginx/logs/error.log info;
  
#進程pid文件
pid /usr/local/nginx/logs/nginx.pid;
  
#指定進程可以打開的最大描述符:數目
#工作模式與連接數上限
#這個指令是指當一個nginx進程打開的最多文件描述符數目,理論值應該是最多打開文件數(ulimit -n)與nginx進程數相除,但是nginx分配請求並不是那么均勻,所以最好與ulimit -n 的值保持一致。
#現在在linux 2.6內核下開啟文件打開數為65535,worker_rlimit_nofile就相應應該填寫65535。
#這是因為nginx調度時分配請求到進程並不是那么的均衡,所以假如填寫10240,總並發量達到3-4萬時就有進程可能超過10240了,這時會返回502錯誤。
worker_rlimit_nofile 65535;
  
  
events
{
 #參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型
 #是Linux 2.6以上版本內核中的高性能網絡I/O模型,linux建議epoll,如果跑在FreeBSD上面,就用kqueue模型。
 #補充說明:
 #與apache相類,nginx針對不同的操作系統,有不同的事件模型
 #A)標准事件模型
 #Select、poll屬於標准事件模型,如果當前系統不存在更有效的方法,nginx會選擇select或poll
 #B)高效事件模型
 #Kqueue:使用於FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用雙處理器的MacOS X系統使用kqueue可能會造成內核崩潰。
 #Epoll:使用於Linux內核2.6版本及以后的系統。
 #/dev/poll:使用於Solaris 7 11/99+,HP/UX 11.22+ (eventport),IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。
 #Eventport:使用於Solaris 10。 為了防止出現內核崩潰的問題, 有必要安裝安全補丁。
 use epoll;
  
 #單個進程最大連接數(最大連接數=連接數*進程數)
 #根據硬件調整,和前面工作進程配合起來用,盡量大,但是別把cpu跑到100%就行。每個進程允許的最多連接數,理論上每台nginx服務器的最大連接數為。
 worker_connections 65535;
  
 #keepalive超時時間。
 keepalive_timeout 60;
  
 #客戶端請求頭部的緩沖區大小。這個可以根據你的系統分頁大小來設置,一般一個請求頭的大小不會超過1k,不過由於一般系統分頁都要大於1k,所以這里設置為分頁大小。
 #分頁大小可以用命令getconf PAGESIZE 取得。
 #[root@web001 ~]# getconf PAGESIZE
 #4096
 #但也有client_header_buffer_size超過4k的情況,但是client_header_buffer_size該值必須設置為“系統分頁大小”的整倍數。
 client_header_buffer_size 4k;
  
 #這個將為打開文件指定緩存,默認是沒有啟用的,max指定緩存數量,建議和打開文件數一致,inactive是指經過多長時間文件沒被請求后刪除緩存。
 open_file_cache max=65535 inactive=60s;
  
 #這個是指多長時間檢查一次緩存的有效信息。
 #語法:open_file_cache_valid time 默認值:open_file_cache_valid 60 使用字段:http, server, location 這個指令指定了何時需要檢查open_file_cache中緩存項目的有效信息.
 open_file_cache_valid 80s;
  
 #open_file_cache指令中的inactive參數時間內文件的最少使用次數,如果超過這個數字,文件描述符一直是在緩存中打開的,如上例,如果有一個文件在inactive時間內一次沒被使用,它將被移除。
 #語法:open_file_cache_min_uses number 默認值:open_file_cache_min_uses 1 使用字段:http, server, location 這個指令指定了在open_file_cache指令無效的參數中一定的時間范圍內可以使用的最小文件數,如果使用更大的值,文件描述符在cache中總是打開狀態.
 open_file_cache_min_uses 1;
  
 #語法:open_file_cache_errors on | off 默認值:open_file_cache_errors off 使用字段:http, server, location 這個指令指定是否在搜索一個文件是記錄cache錯誤.
 open_file_cache_errors on;
}
  
  
  
#設定http服務器,利用它的反向代理功能提供負載均衡支持
http
{
 #文件擴展名與文件類型映射表
 include mime.types;
  
 #默認文件類型
 default_type application/octet-stream;
  
 #默認編碼
 #charset utf-8;
  
 #服務器名字的hash表大小
 #保存服務器名字的hash表是由指令server_names_hash_max_size 和server_names_hash_bucket_size所控制的。參數hash bucket size總是等於hash表的大小,並且是一路處理器緩存大小的倍數。在減少了在內存中的存取次數后,使在處理器中加速查找hash表鍵值成為可能。如果hash bucket size等於一路處理器緩存的大小,那么在查找鍵的時候,最壞的情況下在內存中查找的次數為2。第一次是確定存儲單元的地址,第二次是在存儲單元中查找鍵 值。因此,如果Nginx給出需要增大hash max size 或 hash bucket size的提示,那么首要的是增大前一個參數的大小.
 server_names_hash_bucket_size 128;
  
 #客戶端請求頭部的緩沖區大小。這個可以根據你的系統分頁大小來設置,一般一個請求的頭部大小不會超過1k,不過由於一般系統分頁都要大於1k,所以這里設置為分頁大小。分頁大小可以用命令getconf PAGESIZE取得。
 client_header_buffer_size 32k;
  
 #客戶請求頭緩沖大小。nginx默認會用client_header_buffer_size這個buffer來讀取header值,如果header過大,它會使用large_client_header_buffers來讀取。
 large_client_header_buffers 4 64k;
  
 #設定通過nginx上傳文件的大小
 client_max_body_size 8m;
  
 #開啟高效文件傳輸模式,sendfile指令指定nginx是否調用sendfile函數來輸出文件,對於普通應用設為 on,如果用來進行下載等應用磁盤IO重負載應用,可設置為off,以平衡磁盤與網絡I/O處理速度,降低系統的負載。注意:如果圖片顯示不正常把這個改成off。
 #sendfile指令指定 nginx 是否調用sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,必須設為on。如果用來進行下載等應用磁盤IO重負載應用,可設置為off,以平衡磁盤與網絡IO處理速度,降低系統uptime。
 sendfile on;
  
 #開啟目錄列表訪問,合適下載服務器,默認關閉。
 autoindex on;
  
 #此選項允許或禁止使用socke的TCP_CORK的選項,此選項僅在使用sendfile的時候使用
 tcp_nopush on;
   
 tcp_nodelay on;
  
 #長連接超時時間,單位是秒
 keepalive_timeout 120;
  
 #FastCGI相關參數是為了改善網站的性能:減少資源占用,提高訪問速度。下面參數看字面意思都能理解。
 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壓縮輸出
 gzip_min_length 1k; #最小壓縮文件大小
 gzip_buffers 4 16k; #壓縮緩沖區
 gzip_http_version 1.0; #壓縮版本(默認1.1,前端如果是squid2.5請使用1.0)
 gzip_comp_level 2; #壓縮等級
 gzip_types text/plain application/x-javascript text/css application/xml; #壓縮類型,默認就已經包含textml,所以下面就不用再寫了,寫上去也不會有問題,但是會有一個warn。
 gzip_vary on;
  
 #開啟限制IP連接數的時候需要使用
 #limit_zone crawler $binary_remote_addr 10m;
  
  
  
 #負載均衡配置
 upstream piao.jd.com {
   
  #upstream的負載均衡,weight是權重,可以根據機器配置定義權重。weigth參數表示權值,權值越高被分配到的幾率越大。
  server 192.168.80.121:80 weight=3;
  server 192.168.80.122:80 weight=2;
  server 192.168.80.123:80 weight=3;
  
  #nginx的upstream目前支持4種方式的分配
  #1、輪詢(默認)
  #每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器down掉,能自動剔除。
  #2、weight
  #指定輪詢幾率,weight和訪問比率成正比,用於后端服務器性能不均的情況。
  #例如:
  #upstream bakend {
  # server 192.168.0.14 weight=10;
  # server 192.168.0.15 weight=10;
  #}
  #2、ip_hash
  #每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題。
  #例如:
  #upstream bakend {
  # ip_hash;
  # server 192.168.0.14:88;
  # server 192.168.0.15:80;
  #}
  #3、fair(第三方)
  #按后端服務器的響應時間來分配請求,響應時間短的優先分配。
  #upstream backend {
  # server server1;
  # server server2;
  # fair;
  #}
  #4、url_hash(第三方)
  #按訪問url的hash結果來分配請求,使每個url定向到同一個后端服務器,后端服務器為緩存時比較有效。
  #例:在upstream中加入hash語句,server語句中不能寫入weight等其他的參數,hash_method是使用的hash算法
  #upstream backend {
  # server squid1:3128;
  # server squid2:3128;
  # hash $request_uri;
  # hash_method crc32;
  #}
  
  #tips:
  #upstream bakend{#定義負載均衡設備的Ip及設備狀態}{
  # ip_hash;
  # server 127.0.0.1:9090 down;
  # server 127.0.0.1:8080 weight=2;
  # server 127.0.0.1:6060;
  # server 127.0.0.1:7070 backup;
  #}
  #在需要使用負載均衡的server中增加 proxy_pass http://bakend/;
  
  #每個設備的狀態設置為:
  #1.down表示單前的server暫時不參與負載
  #2.weight為weight越大,負載的權重就越大。
  #3.max_fails:允許請求失敗的次數默認為1.當超過最大次數時,返回proxy_next_upstream模塊定義的錯誤
  #4.fail_timeout:max_fails次失敗后,暫停的時間。
  #5.backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這台機器壓力會最輕。
  
  #nginx支持同時設置多組的負載均衡,用來給不用的server來使用。
  #client_body_in_file_only設置為On 可以講client post過來的數據記錄到文件中用來做debug
  #client_body_temp_path設置記錄文件的目錄 可以設置最多3層目錄
  #location對URL進行匹配.可以進行重定向或者進行新的代理 負載均衡
 }
   
   
   
 #虛擬主機的配置
 server
 {
  #監聽端口
  listen 80;
  
  #域名可以有多個,用空格隔開
  server_name www.jd.com jd.com;
  index index.html index.htm index.php;
  root /data/www/jd;
  
  #對******進行負載均衡
  location ~ .*.(php|php5)?$
  {
   fastcgi_pass 127.0.0.1:9000;
   fastcgi_index index.php;
   include fastcgi.conf;
  }
    
  #圖片緩存時間設置
  location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
  {
   expires 10d;
  }
    
  #JS和CSS緩存時間設置
  location ~ .*.(js|css)?$
  {
   expires 1h;
  }
    
  #日志格式設定
  #$remote_addr與$http_x_forwarded_for用以記錄客戶端的ip地址;
  #$remote_user:用來記錄客戶端用戶名稱;
  #$time_local: 用來記錄訪問時間與時區;
  #$request: 用來記錄請求的url與http協議;
  #$status: 用來記錄請求狀態;成功是200,
  #$body_bytes_sent :記錄發送給客戶端文件主體內容大小;
  #$http_referer:用來記錄從那個頁面鏈接訪問過來的;
  #$http_user_agent:記錄客戶瀏覽器的相關信息;
  #通常web服務器放在反向代理的后面,這樣就不能獲取到客戶的IP地址了,通過$remote_add拿到的IP地址是反向代理服務器的iP地址。反向代理服務器在轉發請求的http頭信息中,可以增加x_forwarded_for信息,用以記錄原有客戶端的IP地址和原來客戶端的請求的服務器地址。
  log_format access '$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" $http_x_forwarded_for';
    
  #定義本虛擬主機的訪問日志
  access_log /usr/local/nginx/logs/host.access.log main;
  access_log /usr/local/nginx/logs/host.access.404.log log404;
    
  #對 "/" 啟用反向代理
  location / {
   proxy_pass http://127.0.0.1:88;
   proxy_redirect off;
   proxy_set_header X-Real-IP $remote_addr;
     
   #后端的Web服務器可以通過X-Forwarded-For獲取用戶真實IP
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     
   #以下是一些反向代理的配置,可選。
   proxy_set_header Host $host;
  
   #允許客戶端請求的最大單文件字節數
   client_max_body_size 10m;
  
   #緩沖區代理緩沖用戶端請求的最大字節數,
   #如果把它設置為比較大的數值,例如256k,那么,無論使用firefox還是IE瀏覽器,來提交任意小於256k的圖片,都很正常。如果注釋該指令,使用默認的client_body_buffer_size設置,也就是操作系統頁面大小的兩倍,8k或者16k,問題就出現了。
   #無論使用firefox4.0還是IE8.0,提交一個比較大,200k左右的圖片,都返回500 Internal Server Error錯誤
   client_body_buffer_size 128k;
  
   #表示使nginx阻止HTTP應答代碼為400或者更高的應答。
   proxy_intercept_errors on;
  
   #后端服務器連接的超時時間_發起握手等候響應超時時間
   #nginx跟后端服務器連接超時時間(代理連接超時)
   proxy_connect_timeout 90;
  
   #后端服務器數據回傳時間(代理發送超時)
   #后端服務器數據回傳時間_就是在規定時間之內后端服務器必須傳完所有的數據
   proxy_send_timeout 90;
  
   #連接成功后,后端服務器響應時間(代理接收超時)
   #連接成功后_等候后端服務器響應時間_其實已經進入后端的排隊之中等候處理(也可以說是后端服務器處理請求的時間)
   proxy_read_timeout 90;
  
   #設置代理服務器(nginx)保存用戶頭信息的緩沖區大小
   #設置從被代理服務器讀取的第一部分應答的緩沖區大小,通常情況下這部分應答中包含一個小的應答頭,默認情況下這個值的大小為指令proxy_buffers中指定的一個緩沖區的大小,不過可以將其設置為更小
   proxy_buffer_size 4k;
  
   #proxy_buffers緩沖區,網頁平均在32k以下的設置
   #設置用於讀取應答(來自被代理服務器)的緩沖區數目和大小,默認情況也為分頁大小,根據操作系統的不同可能是4k或者8k
   proxy_buffers 4 32k;
  
   #高負荷下緩沖大小(proxy_buffers*2)
   proxy_busy_buffers_size 64k;
  
   #設置在寫入proxy_temp_path時數據的大小,預防一個工作進程在傳遞文件時阻塞太長
   #設定緩存文件夾大小,大於這個值,將從upstream服務器傳
   proxy_temp_file_write_size 64k;
  }
    
    
  #設定查看Nginx狀態的地址
  location /NginxStatus {
   stub_status on;
   access_log on;
   auth_basic "NginxStatus";
   auth_basic_user_file confpasswd;
   #htpasswd文件的內容可以用apache提供的htpasswd工具來產生。
  }
    
  #本地動靜分離反向代理配置
  #所有jsp的頁面均交由tomcat或resin處理
  location ~ .(jsp|jspx|do)?$ {
   proxy_set_header Host $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_pass http://127.0.0.1:8080;
  }
    
  #所有靜態文件由nginx直接讀取不經過tomcat或resin
  location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|
  pdf|xls|mp3|wma)$
  {
   expires 15d; 
  }
    
  location ~ .*.(js|css)?$
  {
   expires 1h;
  }
 }
}
######Nginx配置文件nginx.conf中文詳解#####

2.5 配置文件5

user                              nobody nobody;   ## 指定運行用戶和組
worker_processes                  4;               ## 指定worker數量,建議此處auto
worker_rlimit_nofile              51200;           ## 最大打開文件描述符

error_log                         logs/error.log  notice;

pid                               /var/run/nginx.pid;

events {
  use                             epoll;            ## 使用epoll事件驅動模型
  worker_connections              51200;            ## 一個worker能處理的最大並發
}

http {
  server_tokens                   off;                ## 隱藏nginx版本
  include                         mime.types;        

  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          20m;            ## 設置客戶端請求body的最大允許大小
  client_body_buffer_size       256k;            ## 設置客戶端請求body的緩沖區大小
  proxy_connect_timeout         90;                ## 與后端服務器連接的超時時長
  proxy_send_timeout            90;                ## 把請求發送給后端服務器的超時時長
  proxy_read_timeout            90;                ## 等待后端服務器發送響應報文的超時時長
  proxy_buffer_size             128k;            ## 從代理服務器接收的響應的第一部分緩沖區
  proxy_buffers                 4 64k;            ## 從代理服務器讀取響應的緩沖區number和size
  proxy_busy_buffers_size       128k;            ## 限制size在響應尚未完全讀取時可能忙於向客戶端發送響應的緩沖區總數
  proxy_temp_file_write_size    128k;            ## 該指令設置緩沖臨時文件的最大值

  default_type                    application/octet-stream;
  charset                         utf-8;        ## 字符集
  
  client_body_temp_path           /var/tmp/client_body_temp 1 2;  ## 請求body臨時目錄
  proxy_temp_path                 /var/tmp/proxy_temp 1 2;    ## 代理服務器接受數據臨時目錄
  fastcgi_temp_path               /var/tmp/fastcgi_temp 1 2; ## FastCGI服務器接收臨時目錄 
  uwsgi_temp_path                 /var/tmp/uwsgi_temp 1 2; ## uwsgi 服務器接收臨時目錄
  scgi_temp_path                  /var/tmp/scgi_temp 1 2; ##scgi服務器接收臨時目錄

  ignore_invalid_headers          on;        ## 開啟控制忽略具有無效名稱的標頭字段
  server_names_hash_max_size      256;        ## 服務器名稱哈希表的最大值
  server_names_hash_bucket_size   64;        ## 服務器名稱哈希表存儲bucket大小
  client_header_buffer_size       8k;        ## 設置緩沖區以讀取客戶端請求標頭
  large_client_header_buffers     4 32k;    ## 設置緩沖區以讀取客戶端請求標頭最大值number和size
  connection_pool_size            256;        ## 允許精確調整每個連接的內存分配
  request_pool_size               64k;        ## 允許精確調整每個請求的內存分配

  output_buffers                  2 128k;    ## 設置用於從磁盤讀取響應的緩沖區number和size
  postpone_output                 1460;        ## 客戶端數據的傳輸最小值,單位字節

  client_header_timeout           1m;        ## 定義讀取客戶端請求標頭的超時時長
  client_body_timeout             3m;        ## 定義讀取客戶端請求主體的超時時長
  send_timeout                    3m;        ## 設置將響應傳輸到客戶端的超時時長


  log_format main                 '$server_addr $remote_addr [$time_local] $msec+$connection '
                                  '"$request" $status $connection $request_time $body_bytes_sent "$http_referer" '
                                  '"$http_user_agent" "$http_x_forwarded_for"';

  open_log_file_cache               max=1000 inactive=20s min_uses=1 valid=1m;

  access_log                      logs/access.log      main;
  log_not_found                   on;


  sendfile                        on;            
  tcp_nodelay                     on;        ## 啟用長連接馬上響應,提高性能
  tcp_nopush                      off;        ## 關閉套接字選項

  reset_timedout_connection       on;        ## 啟用重置超時連接
  keepalive_timeout               10 5;        ## 第一個參數設置長連接超時時長,第二個瀏覽器識別為keep-alive:timeout=5
  keepalive_requests              100;        ## 設置可通過一個保持活動連接提供的最大請求數


  gzip                            on;        ## 開啟壓縮
  gzip_http_version               1.1;        ## 啟用壓縮時協議最小版本
  gzip_vary                       on;         
  gzip_proxied                    any;        ## 為所有代理請求啟用壓縮
  gzip_min_length                 1024;        ## 設置將被gzip壓縮的響應的最小長度
  gzip_comp_level                 6;        ## 設置壓縮等級
  gzip_buffers                    16 8k;    ## 設置用於壓縮響應的緩沖區number和size
  gzip_proxied                    expired no-cache no-store private auth no_last_modified no_etag;
  gzip_types                      text/plain application/x-javascript text/css application/xml application/json;
  gzip_disable                    "MSIE [1-6]\.(?!.*SV1)";


  upstream tomcat8080 {
    ip_hash;

    server                        172.16.100.103:8080 weight=1 max_fails=2;
    server                        172.16.100.104:8080 weight=1 max_fails=2;
    server                        172.16.100.105:8080 weight=1 max_fails=2;
  }

  server {
    listen                        80;
    server_name                   www.chan.com;
    # config_apps_begin
    root                          /data/webapps/htdocs;
    access_log                    /var/logs/webapp.access.log     main;
    error_log                     /var/logs/webapp.error.log      notice;

    location / {
    
      location ~* ^.*/favicon.ico$ {
        root                      /data/webapps;
        expires                   180d;        ## 緩存180天
        break;
      }
    
      if ( !-f $request_filename ) {
        proxy_pass                http://tomcat8080;
        break;
      }
    }

    error_page                    500 502 503 504  /50x.html;
      location = /50x.html {
      root                        html;
    }
  }

  server {
    listen                        8088;
    server_name                   nginx_status;

      location / {
          access_log                  off;
          deny                        all;
          return                      503;
      }

      location /status {
          stub_status                 on;
          access_log                  off;
          allow                       127.0.0.1;
          allow                       172.16.100.71;
          deny                        all;
      }
  }

}

三、按塊詳解

3.1、全局塊

3.1.1、配置運行Nginx服務器用戶(組)

指令格式:
user user [group];

user:指定可以運行Nginx服務器的用戶
group:可選項,可以運行Nginx服務器的用戶組

如果user指令不配置或者配置為user nobody nobody,則默認所有用戶都可以啟動Nginx進程

3.1.2、worker process數配置

Nginx服務器實現並發處理服務的關鍵,指令格式:

worker_processes number | auto;

number:Nginx進程最多可以產生的worker process數
auto:Nginx進程將自動檢測

按照上文中的配置清單的實驗,我們給worker_processes配置的數目是:3,啟動Nginx服務器后,我們可以后台看一下主機上的Nginx進程情況:
ps -aux | grep nginx

很明顯,理解 worker_processes 這個指令的含義就很容易了

3.1.3、Nginx進程PID存放路徑

Nginx進程是作為系統守護進程在運行,需要在某文件中保存當前運行程序的主進程號,Nginx支持該保存文件路徑的自定義

指令格式:
pid file;

file:指定存放路徑和文件名稱如果不指定默認置於路徑 logs/nginx.pid

3.1.4、錯誤日志的存放路徑

指定格式:

error_log file | stderr;

file:日志輸出到某個文件file
stderr:日志輸出到標准錯誤輸出

3.1.5、配置文件的引入

指令格式:

include file;

該指令主要用於將其他的Nginx配置或者第三方模塊的配置引用到當前的主配置文件中

3.2、events塊

3.2.1、設置網絡連接的序列化

指令格式:

accept_mutex on | off;

該指令默認為on狀態,表示會對多個Nginx進程接收連接進行序列化,防止多個進程對連接的爭搶。

  說到該指令,首先得闡述一下什么是所謂的 “驚群問題”,可以參考 WIKI百科的解釋。就Nginx的場景來解釋的話大致的意思就是:當一個新網絡連接來到時,多個worker進程會被同時喚醒,但僅僅只有一個進程可以真正獲得連接並處理之。如果每次喚醒的進程數目過多的話,其實是會影響一部分性能的。

  所以在這里,如果accept_mutex on,那么多個worker將是以串行方式來處理,其中有一個worker會被喚醒;反之若accept_mutex off,那么所有的worker都會被喚醒,不過只有一個worker能獲取新連接,其它的worker會重新進入休眠狀態

  這個值的開關與否其實是要和具體場景掛鈎的。

3.2.2、是否允許同時接收多個網絡連接

指令格式:

multi_accept on | off;

該指令默認為off狀態,意指每個worker process 一次只能接收一個新到達的網絡連接。若想讓每個Nginx的workerprocess都有能力同時接收多個網絡連接,則需要開啟此配置

3.2.3、事件驅動模型的選擇

指令格式:

use model;

model模型可選擇項包括:select、poll、kqueue、epoll、rtsig等......

3.2.4、最大連接數的配置

指令格式:

worker_connections number;

number默認值為512,表示允許每一個worker process可以同時開啟的最大連接數

3.3、HTTP塊-全局

3.3.1 定義MIME-Type

指令格式:

include mime.types;

default_type mime-type;

MIME-Type指的是網絡資源的媒體類型,也即前端請求的資源類型
include指令將mime.types文件包含進來
cat mime.types 來查看mime.types文件內容,我們發現其就是一個types結構,里面包含了各種瀏覽器能夠識別的MIME類型以及對應類型的文件后綴名字,如下所示:

3.3.2、自定義服務日志

指令格式:

access_log path [format];

path:自定義服務日志的路徑 + 名稱
format:可選項,自定義服務日志的字符串格式。其也可以使用 log_format 定義的格式

3.3.3、允許sendfile方式傳輸文件

指令格式:

sendfile on | off;

 
sendfile_max_chunk size;

前者用於開啟或關閉使用sendfile()傳輸文件,默認off
后者指令若size
>0,則Nginx進程的每個workerprocess每次調用sendfile()傳輸的數據了最大不能超出此值;若size=0則表示不限制。默認值為0

3.3.4、連接超時時間配置

指令格式:

keepalive_timeout timeout [header_timeout];

timeout 表示server端對連接的保持時間,默認75秒
header_timeout 為可選項,表示在應答報文頭部的 Keep
-Alive 域設置超時時間:“Keep-Alive :timeout = header_timeout”

3.4、HTTP塊-Server

3.4.1、單連接請求數上限

指令格式:

keepalive_requests number;

該指令用於限制用戶通過某一個連接向Nginx服務器發起請求的次數

3.4.2、配置網絡監聽

指令格式:

第一種:配置監聽的IP地址: listen IP[:PORT]; 第二種:配置監聽的端口: listen PORT;
實際舉例: listen192.168.31.177:8080; # 監聽具體IP和具體端口上的連接
listen192.168.31.177; # 監聽IP上所有端口上的連接
listen 8080; # 監聽具體端口上的所有IP的連接

3.4.3、基於名稱和IP的虛擬主機配置

指令格式:

server_name name1 name2 ...
name可以有多個並列名稱,而且此處的name支持正則表達式書寫 實際舉例: server_name
~^www\d+\.myserver\.com$ 此時表示該虛擬主機可以接收類似域名 www1.myserver.com 等的請求,而拒絕 www.myserver.com 的域名請求,所以說用正則表達式可以實現更精准的控制 至於基於IP的虛擬主機配置比較簡單,不再太贅述: 指令格式: server_name IP地址

3.6、HTTP塊-location配置

3.6.1、location

指令格式為:

location [ = | ~ | ~* | ^~ ] uri {...}

這里的uri分為標准uri和正則uri,兩者的唯一區別是uri中是否包含正則表達式

uri前面的方括號中的內容是可選項,解釋如下:

“=”:用於標准uri前,要求請求字符串與uri嚴格匹配,一旦匹配成功則停止
“~”:用於正則uri前,並且區分大小寫
“~*”:用於正則uri前,但不區分大小寫
“^~”:用於標准uri前,要求Nginx找到標識uri和請求字符串匹配度最高的location后,立即使用此location處理請求,而不再使用location塊中的正則uri和請求字符串做匹配

3.6.2、請求根目錄配置

指令格式:

root path;

path:Nginx接收到請求以后查找資源的根目錄路徑

當然,還可以通過alias指令來更改location接收到的URI請求路徑,指令為:

alias path;

# path為修改后的根路徑

3.6.3、設置網站的默認首頁

指令格式:

index file ......

file可以包含多個用空格隔開的文件名,首先找到哪個頁面,就使用哪個頁面響應請求

3.6.4、proxy_set_header

目前有一個需求如下:

nginx上配有aaa.example.com的虛擬主機,現在需要將訪問http://aaa.example.com/api/x.x/client/的請求轉到http://bbb.example.com/api/x.x/client/,bbb.example.com的虛擬主機在另外一台nginx上,其中x.x表示位數不定的版本號,如:1.0或1.20.345都可能。請求轉過去要求url保持不變

用rewrite轉發的話,url會發生變化的,那就用proxy_pass吧,於是添加了如下的配置:

location ~ ^/api/([0-9]+)(\.[0-9]+)*/client/ {
    proxy_pass http://bbb.example.com;
}

  在現有環境的nginx里添加這段配置之后,訪問卻始終轉不過去,查看nginx日志也只能看到是404信息,並沒有更多定位問題的信息。檢查了許久也沒找到原因,於是重新裝了一台新nginx,里面只加上面這段配置,結果nginx是能夠轉發成功的,這說明單獨來看這條location的配置是沒有問題的,很有可能是現有環境nginx里的某些配置影響到了這個轉發。 
  為了定位問題原因,我將aaa.example.com虛擬主機下的其他配置注意注釋掉來調試,最后發現當注釋掉proxy_set_header Host $http_host ;這條配置之后,就能成功轉發了。這才注意到是反向代理配置的問題。現有環境中原有的配置也不能隨便刪掉,上網查了下原因,找到下面這種解決方案:

location ~ ^/api/([0-9]+)(\.[0-9]+)*/client/ {
    proxy_pass http://bbb.example.com;
    proxy_set_header Host $proxy_host;
}

即,在location里面添加一條proxy_set_header Host $proxy_host;配置。當Host設置為$http_host時,則不改變請求頭的值,所以當要轉發到bbb.example.com的時候,請求頭還是aaa.example.com的Host信息,就會有問題;當Host設置為$proxy_host時,則會重新設置請求頭為bbb.example.com的Host信息。

另外,關於proxy_pass轉發url的參數,可以通過在location中用rewrite來做,所以完善后的配置如下:

location ~ ^/api/([0-9]+)(\.[0-9]+)*/client/ {
    rewrite /(.*)$ /$1 break;
    proxy_pass http://bbb.example.com;
    proxy_set_header Host $proxy_host;
}

在location用rewrite改變了URI之后,proxy_pass將使用改變后的URI。上面例子(.*)是將所有參數傳給$1,轉發時/$1會拼接在http://bbb.example.com后面。

先來看下proxy_set_header的語法

 

語法: proxy_set_header field value;
默認值:
proxy_set_header Host $proxy_host;
proxy_set_header Connection close;
上下文: httpserverlocation

允許重新定義或者添加發往后端服務器的請求頭value可以包含文本、變量或者它們的組合。 當且僅當當前配置級別中沒有定義proxy_set_header指令時,會從上面的級別繼承配置。 默認情況下,只有兩個請求頭會被重新定義:

proxy_set_header Host       $proxy_host;
proxy_set_header Connection close;

nginx對於upstream默認使用的是基於IP的轉發,因此對於以下配置:

upstream backend {  
    server 127.0.0.1:8080;  
}  
upstream crmtest {  
    server crmtest.aty.sohuno.com;  
}  
server {  
        listen       80;  
        server_name  chuan.aty.sohuno.com;  
        proxy_set_header Host $http_host;  
        proxy_set_header x-forwarded-for  $remote_addr;  
        proxy_buffer_size         64k;  
        proxy_buffers             32 64k;  
        charset utf-8;  
  
        access_log  logs/host.access.log  main;  
        location = /50x.html {  
            root   html;  
        }  
    location / {  
        proxy_pass backend ;  
    }  
          
    location = /customer/straightcustomer/download {  
        proxy_pass http://crmtest;  
        proxy_set_header Host $proxy_host;  
    }  
}  

當匹配到/customer/straightcustomer/download時,使用crmtest處理,到upstream就匹配到crmtest.aty.sohuno.com,這里直接轉換成IP進行轉發了。假如crmtest.aty.sohuno.com是在另一台nginx下配置的,ip為10.22.10.116,則$proxy_host則對應為10.22.10.116。此時相當於設置了Host為10.22.10.116。如果想讓Host是crmtest.aty.sohuno.com,則進行如下設置:

proxy_set_header Host crmtest.aty.sohuno.com;

如果不想改變請求頭“Host”的值,可以這樣來設置:

proxy_set_header Host       $http_host;

但是,如果客戶端請求頭中沒有攜帶這個頭部,那么傳遞到后端服務器的請求也不含這個頭部。 這種情況下,更好的方式是使用$host變量——它的值在請求包含“Host”請求頭時為“Host”字段的值,在請求未攜帶“Host”請求頭時為虛擬主機的主域名:

proxy_set_header Host       $host;

此外,服務器名可以和后端服務器的端口一起傳送:

proxy_set_header Host       $host:$proxy_port;

如果某個請求頭的值為空,那么這個請求頭將不會傳送給后端服務器:

proxy_set_header Accept-Encoding "";

 

 

 

nginx配置項,里面的配置項有代理https,http,代理靜態文件,H5分發,代理TCP連接,能滿足大多數搭建測試環境所要用的nginx的情況,大家碰到要使用nginx的時候可以參考下

#user  nobody;
worker_processes  1;

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

#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
#代理TCP連接
stream {
    upstream tcp_proxy_boc {
        server 172.16.100.59:8890 ;
    }

    server {
        listen 20080 so_keepalive=on;
        proxy_connect_timeout 20s;
        proxy_timeout 30s;
        proxy_pass tcp_proxy_boc;

    }


   upstream tcp_proxy_sls {
        server 172.16.100.59:8892 ;
    }

    server {
        listen 20081 so_keepalive=on;
        proxy_connect_timeout 20s;
        proxy_timeout 30s;
        proxy_pass tcp_proxy_sls;

    }

 upstream tcp_proxy_ccb {
        server 172.16.100.59:8891 ;
    }

    server {
        listen 20082 so_keepalive=on;
        proxy_connect_timeout 20s;
        proxy_timeout 30s;
        proxy_pass tcp_proxy_ccb;

    }
}
http {
    include       mime.types;
    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;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    client_max_body_size 20m;

    #gzip  on;
#liumaAPPH5
    server {
        listen       20074;
          server_name  localhost;
        location / {
            root   /etc/nginx/xcy/kwl/dist;
            index  index.html index.htm;
        }
        location /etcapi {
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://192.168.3.100:8089/;
         }
        location /nbapi  {
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass https://nb.nbcolt.com:20059/;

        }
            error_log    logs/error_www.abc.com.log    error; 
    }
#中行H5
    server {
        listen       20078;
        server_name  localhost;
        location / {
            root   /etc/nginx/xcy/hyn/dist;
            index  index.html index.htm;
        try_files $uri $uri/ @router;

         }
#    error_page   500 502 503 504  /50x.html;
#        location = /50x.html {
#            root   html;
#        }

                location /zjoffice  {
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://192.168.3.134:8183/zjoffice/;

        }
           location @router {
             rewrite ^.*$ /index.html last;
    }
    }
#liumaAPPH5圖片服務器
    server {
        listen       20075 ;
        server_name  localhost;
        location /kwl    {
                root   html;
                index  index.html index.htm;
                proxy_pass http://192.168.3.111/kwl/;                
        }
    }
#liuma
    server {
        listen       20079 ssl ;
        server_name  localhost;
        ssl_certificate      /etc/nginx/cert/oit/2441956_nb.nbcolt.com.pem;
        ssl_certificate_key  /etc/nginx/cert/oit/2441956_nb.nbcolt.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM;
        ssl_prefer_server_ciphers on;
        location /oitUct { 
            proxy_pass http://192.168.3.134:8040/oitUct;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        } 
        location /oitBaseParam { 
            proxy_pass http://192.168.3.134:8040/oitBaseParam;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location /oitBusiness { 
            proxy_pass http://192.168.3.134:8040/oitBusiness;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        } 
        location /oitManage { 
            proxy_pass http://192.168.3.134:8040/oitManage;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location /oitManagement { 
            proxy_pass http://192.168.3.134:8040/oitManagement;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        } 
        location /oitPortal { 
            proxy_pass http://192.168.3.134:8040/oitPortal;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location /oitFinance { 
            proxy_pass http://192.168.3.134:8042/oitFinance;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location /oitMiddleware { 
            proxy_pass http://192.168.3.134:8040/oitMiddleware;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location /sso { 
            proxy_pass http://122.224.230.10:17003/sso/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    }
        location /dub-webapps-yb { 
            proxy_pass http://122.224.230.26:20019/dub-webapps-yb/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
        location /member {
            proxy_pass http://192.168.3.30:5050/member/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
        location /dub-upload {
            proxy_pass http://122.224.230.26:20019/dub-upload/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
#代理https並保留源地址
    server {
        listen       20076 ssl ;
        server_name  localhost;
        ssl_certificate      /usr/local/1633691_test.etczj.com.pem;
        ssl_certificate_key  /usr/local/1633691_test.etczj.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM;
        ssl_prefer_server_ciphers on;
        location / {   
            proxy_next_upstream http_500 http_502 http_503 http_504 timeout error invalid_header;  
            proxy_pass                http://192.168.3.100:8089/;  
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }  

        }
#代理圖片
    server {
        listen       20077;
        server_name  localhost;
        location / {
            root   /mnt/mfs;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }



    
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM