Nginx配置文件詳解

文章來源: https://www.cnblogs.com/dongye95/p/11096785.html

一、Nginx配置文件的整體結(jié)構(gòu)

image.png

配置清單例析

image.png

二、配置文件詳解

2.1 配置文件1
########### 每個指令必須有分號結(jié)束丹泉。#################
#user administrator administrators;  #配置用戶或者組,默認為nobody nobody。
#worker_processes 2;  #允許生成的進程數(shù),默認為1
#pid /nginx/pid/nginx.pid;   #指定nginx進程運行文件存放地址
error_log log/error.log debug;  #制定日志路徑句惯,級別土辩。這個設(shè)置可以放入全局塊,http塊抢野,server塊拷淘,級別以此為:debug|info|notice|warn|error|crit|alert|emerg
events {
    accept_mutex on;   #設(shè)置網(wǎng)路連接序列化,防止驚群現(xiàn)象發(fā)生指孤,默認為on
    multi_accept on;  #設(shè)置一個進程是否同時接受多個網(wǎng)絡(luò)連接启涯,默認為off
    #use epoll;      #事件驅(qū)動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    worker_connections  1024;    #最大連接數(shù)邓厕,默認為512
}
http {
    include       mime.types;   #文件擴展名與文件類型映射表
    default_type  application/octet-stream; #默認文件類型逝嚎,默認為text/plain
    #access_log off; #取消服務(wù)日志    
    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;  #每個進程每次調(diào)用傳輸數(shù)量不能大于設(shè)定的值,默認為0伟桅,即不設(shè)上限敞掘。
    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; #單連接請求上限次數(shù)赫冬。
        listen       4545;   #監(jiān)聽端口
        server_name  127.0.0.1;   #監(jiān)聽地址       
        location  ~*^.+$ {       #請求的url過濾,正則匹配溃列,~為區(qū)分大小寫劲厌,~*為不區(qū)分大小寫。
           #root path;  #根目錄
           #index vv.txt;  #設(shè)置默認頁
           proxy_pass  http://mysvr;  #請求轉(zhuǎn)向mysvr 定義的服務(wù)器列表
           deny 127.0.0.1;  #拒絕的ip
           allow 172.18.5.54; #允許的ip           
        } 
    }
}
2.2 配置文件2
#運行用戶
user nobody;
#啟動進程,通常設(shè)置成和cpu的數(shù)量相等
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;

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

    #單個后臺worker process進程的最大并發(fā)鏈接數(shù)    
    worker_connections  1024;

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

}


http {
    #設(shè)定mime類型,類型由mime.type文件定義
    include    mime.types;
    default_type  application/octet-stream;
    #設(shè)定日志格式
    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 是否調(diào)用 sendfile 函數(shù)(zero copy 方式)來輸出文件拳喻,
    #對于普通應(yīng)用,必須設(shè)為 on,
    #如果用來進行下載等應(yīng)用磁盤IO重負載應(yīng)用猪腕,可設(shè)置為 off冗澈,
    #以平衡磁盤與網(wǎng)絡(luò)I/O處理速度,降低系統(tǒng)的uptime.
    sendfile     on;
    #tcp_nopush     on;

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

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

    #設(shè)定請求緩沖
    client_header_buffer_size    128k;
    large_client_header_buffers  4 128k;


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

        #定義服務(wù)器的默認網(wǎng)站根目錄位置
        root html;

        #設(shè)定本虛擬主機的訪問日志
        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 {
        }

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

        #PHP 腳本請求全部轉(zhuǎn)發(fā)到 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進程數(shù)岭粤,建議設(shè)置為等于CPU總核心數(shù).

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

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

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

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

設(shè)定http服務(wù)器
http
{

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

    #FastCGI相關(guān)參數(shù)是為了改善網(wǎng)站的性能:減少資源占用见咒,提高訪問速度。下面參數(shù)看字面意思都能理解宝当。
    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模塊設(shè)置
    gzip on; #開啟gzip壓縮輸出
    gzip_min_length 1k; #最小壓縮文件大小
    gzip_buffers 4 16k; #壓縮緩沖區(qū)
    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;
    #壓縮類型锈拨,默認就已經(jīng)包含text/html娄昆,所以下面就不用再寫了,寫上去也不會有問題杆怕,但是會有一個warn。
    gzip_vary on;
    #limit_zone crawler $binary_remote_addr 10m; #開啟限制IP連接數(shù)的時候需要使用

    upstream blog.ha97.com {
        #upstream的負載均衡,weight是權(quán)重留潦,可以根據(jù)機器配置定義權(quán)重。weigth參數(shù)表示權(quán)值,權(quán)值越高被分配到的幾率越大十偶。
        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;    #監(jiān)聽端口
        server_name aa.cn www.aa.cn ; #server_name end  #域名可以有多個猛频,用空格隔開
        index index.html index.htm index.php;  # 設(shè)置訪問主頁
        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;
        }
    #圖片緩存時間設(shè)置
        location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
        {
        expires 10d;
        }
    #JS和CSS緩存時間設(shè)置
        location ~ .*.(js|css)?$
        {
        expiresexpires 1h;
        }

    }

    日志格式設(shè)定

    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服務(wù)器可以通過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; #允許客戶端請求的最大單文件字節(jié)數(shù)
        client_body_buffer_size 128k; #緩沖區(qū)代理緩沖用戶端請求的最大字節(jié)數(shù)训柴,
        proxy_connect_timeout 90; #nginx跟后端服務(wù)器連接超時時間(代理連接超時)
        proxy_send_timeout 90; #后端服務(wù)器數(shù)據(jù)回傳時間(代理發(fā)送超時)
        proxy_read_timeout 90; #連接成功后洗鸵,后端服務(wù)器響應(yīng)時間(代理接收超時)
        proxy_buffer_size 4k; #設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小
        proxy_buffers 4 32k; #proxy_buffers緩沖區(qū),網(wǎng)頁平均在32k以下的設(shè)置
        proxy_busy_buffers_size 64k; #高負荷下緩沖大小(proxy_buffers*2)
        proxy_temp_file_write_size 64k;
        #設(shè)定緩存文件夾大小铲咨,大于這個值,將從upstream服務(wù)器傳

    }
    設(shè)定查看Nginx狀態(tài)的地址

    location /NginxStatus {
        stub_status on;
        access_log on;
        auth_basic "NginxStatus";
        auth_basic_user_file conf/htpasswd;
        #htpasswd文件的內(nèi)容可以用apache提供的htpasswd工具來產(chǎn)生北滥。
    }

    #本地動靜分離反向代理配置
    #所有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;
    }

    #所有靜態(tài)文件由nginx直接讀取不經(jīng)過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進程數(shù),建議設(shè)置為等于CPU總核心數(shù)鉴逞。
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;
  
#指定進程可以打開的最大描述符:數(shù)目
#工作模式與連接數(shù)上限
#這個指令是指當(dāng)一個nginx進程打開的最多文件描述符數(shù)目,理論值應(yīng)該是最多打開文件數(shù)(ulimit -n)與nginx進程數(shù)相除畅姊,但是nginx分配請求并不是那么均勻,所以最好與ulimit -n 的值保持一致。
#現(xiàn)在在linux 2.6內(nèi)核下開啟文件打開數(shù)為65535舌劳,worker_rlimit_nofile就相應(yīng)應(yīng)該填寫65535捅厂。
#這是因為nginx調(diào)度時分配請求到進程并不是那么的均衡撵割,所以假如填寫10240,總并發(fā)量達到3-4萬時就有進程可能超過10240了,這時會返回502錯誤往踢。
worker_rlimit_nofile 65535;
  
  
events
{
 #參考事件模型趣效,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型
 #是Linux 2.6以上版本內(nèi)核中的高性能網(wǎng)絡(luò)I/O模型讯私,linux建議epoll楞黄,如果跑在FreeBSD上面,就用kqueue模型。
 #補充說明:
 #與apache相類,nginx針對不同的操作系統(tǒng)匪煌,有不同的事件模型
 #A)標準事件模型
 #Select齿拂、poll屬于標準事件模型吗购,如果當(dāng)前系統(tǒng)不存在更有效的方法,nginx會選擇select或poll
 #B)高效事件模型
 #Kqueue:使用于FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用雙處理器的MacOS X系統(tǒng)使用kqueue可能會造成內(nèi)核崩潰。
 #Epoll:使用于Linux內(nèi)核2.6版本及以后的系統(tǒng)笙什。
 #/dev/poll:使用于Solaris 7 11/99+芽隆,HP/UX 11.22+ (eventport),IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+孽拷。
 #Eventport:使用于Solaris 10窿侈。 為了防止出現(xiàn)內(nèi)核崩潰的問題乃秀, 有必要安裝安全補丁衙傀。
 use epoll;
  
 #單個進程最大連接數(shù)(最大連接數(shù)=連接數(shù)*進程數(shù))
 #根據(jù)硬件調(diào)整火本,和前面工作進程配合起來用金麸,盡量大揍魂,但是別把cpu跑到100%就行偎蘸。每個進程允許的最多連接數(shù)限书,理論上每臺nginx服務(wù)器的最大連接數(shù)為。
 worker_connections 65535;
  
 #keepalive超時時間。
 keepalive_timeout 60;
  
 #客戶端請求頭部的緩沖區(qū)大小秦躯。這個可以根據(jù)你的系統(tǒng)分頁大小來設(shè)置哨免,一般一個請求頭的大小不會超過1k,不過由于一般系統(tǒng)分頁都要大于1k,所以這里設(shè)置為分頁大小工扎。
 #分頁大小可以用命令getconf PAGESIZE 取得舆驶。
 #[root@web001 ~]# getconf PAGESIZE
 #4096
 #但也有client_header_buffer_size超過4k的情況,但是client_header_buffer_size該值必須設(shè)置為“系統(tǒng)分頁大小”的整倍數(shù)。
 client_header_buffer_size 4k;
  
 #這個將為打開文件指定緩存,默認是沒有啟用的,max指定緩存數(shù)量,建議和打開文件數(shù)一致,inactive是指經(jīng)過多長時間文件沒被請求后刪除緩存。
 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參數(shù)時間內(nèi)文件的最少使用次數(shù)簇捍,如果超過這個數(shù)字彰触,文件描述符一直是在緩存中打開的分蓖,如上例,如果有一個文件在inactive時間內(nèi)一次沒被使用尔许,它將被移除么鹤。
 #語法:open_file_cache_min_uses number 默認值:open_file_cache_min_uses 1 使用字段:http, server, location 這個指令指定了在open_file_cache指令無效的參數(shù)中一定的時間范圍內(nèi)可以使用的最小文件數(shù),如果使用更大的值,文件描述符在cache中總是打開狀態(tài).
 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;
}
  
  
  
#設(shè)定http服務(wù)器,利用它的反向代理功能提供負載均衡支持
http
{
 #文件擴展名與文件類型映射表
 include mime.types;
  
 #默認文件類型
 default_type application/octet-stream;
  
 #默認編碼
 #charset utf-8;
  
 #服務(wù)器名字的hash表大小
 #保存服務(wù)器名字的hash表是由指令server_names_hash_max_size 和server_names_hash_bucket_size所控制的蒸甜。參數(shù)hash bucket size總是等于hash表的大小棠耕,并且是一路處理器緩存大小的倍數(shù)。在減少了在內(nèi)存中的存取次數(shù)后柠新,使在處理器中加速查找hash表鍵值成為可能窍荧。如果hash bucket size等于一路處理器緩存的大小,那么在查找鍵的時候恨憎,最壞的情況下在內(nèi)存中查找的次數(shù)為2蕊退。第一次是確定存儲單元的地址,第二次是在存儲單元中查找鍵 值憔恳。因此瓤荔,如果Nginx給出需要增大hash max size 或 hash bucket size的提示,那么首要的是增大前一個參數(shù)的大小.
 server_names_hash_bucket_size 128;
  
 #客戶端請求頭部的緩沖區(qū)大小钥组。這個可以根據(jù)你的系統(tǒng)分頁大小來設(shè)置输硝,一般一個請求的頭部大小不會超過1k,不過由于一般系統(tǒng)分頁都要大于1k者铜,所以這里設(shè)置為分頁大小腔丧。分頁大小可以用命令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;
  
 #設(shè)定通過nginx上傳文件的大小
 client_max_body_size 8m;
  
 #開啟高效文件傳輸模式衣厘,sendfile指令指定nginx是否調(diào)用sendfile函數(shù)來輸出文件,對于普通應(yīng)用設(shè)為 on压恒,如果用來進行下載等應(yīng)用磁盤IO重負載應(yīng)用影暴,可設(shè)置為off,以平衡磁盤與網(wǎng)絡(luò)I/O處理速度探赫,降低系統(tǒng)的負載型宙。注意:如果圖片顯示不正常把這個改成off。
 #sendfile指令指定 nginx 是否調(diào)用sendfile 函數(shù)(zero copy 方式)來輸出文件伦吠,對于普通應(yīng)用妆兑,必須設(shè)為on。如果用來進行下載等應(yīng)用磁盤IO重負載應(yīng)用毛仪,可設(shè)置為off搁嗓,以平衡磁盤與網(wǎng)絡(luò)IO處理速度,降低系統(tǒng)uptime箱靴。
 sendfile on;
  
 #開啟目錄列表訪問腺逛,合適下載服務(wù)器,默認關(guān)閉衡怀。
 autoindex on;
  
 #此選項允許或禁止使用socke的TCP_CORK的選項棍矛,此選項僅在使用sendfile的時候使用
 tcp_nopush on;
   
 tcp_nodelay on;
  
 #長連接超時時間安疗,單位是秒
 keepalive_timeout 120;
  
 #FastCGI相關(guān)參數(shù)是為了改善網(wǎng)站的性能:減少資源占用,提高訪問速度茄靠。下面參數(shù)看字面意思都能理解茂契。
 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模塊設(shè)置
 gzip on; #開啟gzip壓縮輸出
 gzip_min_length 1k; #最小壓縮文件大小
 gzip_buffers 4 16k; #壓縮緩沖區(qū)
 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; #壓縮類型慨绳,默認就已經(jīng)包含textml,所以下面就不用再寫了真竖,寫上去也不會有問題脐雪,但是會有一個warn。
 gzip_vary on;
  
 #開啟限制IP連接數(shù)的時候需要使用
 #limit_zone crawler $binary_remote_addr 10m;
  
  
  
 #負載均衡配置
 upstream piao.jd.com {
   
  #upstream的負載均衡恢共,weight是權(quán)重战秋,可以根據(jù)機器配置定義權(quán)重。weigth參數(shù)表示權(quán)值讨韭,權(quán)值越高被分配到的幾率越大脂信。
  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、輪詢(默認)
  #每個請求按時間順序逐一分配到不同的后端服務(wù)器透硝,如果后端服務(wù)器down掉狰闪,能自動剔除。
  #2濒生、weight
  #指定輪詢幾率埋泵,weight和訪問比率成正比,用于后端服務(wù)器性能不均的情況罪治。
  #例如:
  #upstream bakend {
  # server 192.168.0.14 weight=10;
  # server 192.168.0.15 weight=10;
  #}
  #2丽声、ip_hash
  #每個請求按訪問ip的hash結(jié)果分配,這樣每個訪客固定訪問一個后端服務(wù)器觉义,可以解決session的問題雁社。
  #例如:
  #upstream bakend {
  # ip_hash;
  # server 192.168.0.14:88;
  # server 192.168.0.15:80;
  #}
  #3、fair(第三方)
  #按后端服務(wù)器的響應(yīng)時間來分配請求晒骇,響應(yīng)時間短的優(yōu)先分配霉撵。
  #upstream backend {
  # server server1;
  # server server2;
  # fair;
  #}
  #4、url_hash(第三方)
  #按訪問url的hash結(jié)果來分配請求厉碟,使每個url定向到同一個后端服務(wù)器喊巍,后端服務(wù)器為緩存時比較有效。
  #例:在upstream中加入hash語句箍鼓,server語句中不能寫入weight等其他的參數(shù)崭参,hash_method是使用的hash算法
  #upstream backend {
  # server squid1:3128;
  # server squid2:3128;
  # hash $request_uri;
  # hash_method crc32;
  #}
  
  #tips:
  #upstream bakend{#定義負載均衡設(shè)備的Ip及設(shè)備狀態(tài)}{
  # 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/;
  
  #每個設(shè)備的狀態(tài)設(shè)置為:
  #1.down表示單前的server暫時不參與負載
  #2.weight為weight越大,負載的權(quán)重就越大款咖。
  #3.max_fails:允許請求失敗的次數(shù)默認為1.當(dāng)超過最大次數(shù)時何暮,返回proxy_next_upstream模塊定義的錯誤
  #4.fail_timeout:max_fails次失敗后奄喂,暫停的時間。
  #5.backup: 其它所有的非backup機器down或者忙的時候海洼,請求backup機器跨新。所以這臺機器壓力會最輕。
  
  #nginx支持同時設(shè)置多組的負載均衡坏逢,用來給不用的server來使用域帐。
  #client_body_in_file_only設(shè)置為On 可以講client post過來的數(shù)據(jù)記錄到文件中用來做debug
  #client_body_temp_path設(shè)置記錄文件的目錄 可以設(shè)置最多3層目錄
  #location對URL進行匹配.可以進行重定向或者進行新的代理 負載均衡
 }
   
   
   
 #虛擬主機的配置
 server
 {
  #監(jiān)聽端口
  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;
  }
    
  #圖片緩存時間設(shè)置
  location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
  {
   expires 10d;
  }
    
  #JS和CSS緩存時間設(shè)置
  location ~ .*.(js|css)?$
  {
   expires 1h;
  }
    
  #日志格式設(shè)定
  #$remote_addr與$http_x_forwarded_for用以記錄客戶端的ip地址是整;
  #$remote_user:用來記錄客戶端用戶名稱肖揣;
  #$time_local: 用來記錄訪問時間與時區(qū);
  #$request: 用來記錄請求的url與http協(xié)議浮入;
  #$status: 用來記錄請求狀態(tài)龙优;成功是200,
  #$body_bytes_sent :記錄發(fā)送給客戶端文件主體內(nèi)容大惺滦恪彤断;
  #$http_referer:用來記錄從那個頁面鏈接訪問過來的;
  #$http_user_agent:記錄客戶瀏覽器的相關(guān)信息易迹;
  #通常web服務(wù)器放在反向代理的后面宰衙,這樣就不能獲取到客戶的IP地址了,通過$remote_add拿到的IP地址是反向代理服務(wù)器的iP地址赴蝇。反向代理服務(wù)器在轉(zhuǎn)發(fā)請求的http頭信息中菩浙,可以增加x_forwarded_for信息,用以記錄原有客戶端的IP地址和原來客戶端的請求的服務(wù)器地址句伶。
  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服務(wù)器可以通過X-Forwarded-For獲取用戶真實IP
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     
   #以下是一些反向代理的配置劲蜻,可選。
   proxy_set_header Host $host;
  
   #允許客戶端請求的最大單文件字節(jié)數(shù)
   client_max_body_size 10m;
  
   #緩沖區(qū)代理緩沖用戶端請求的最大字節(jié)數(shù)考余,
   #如果把它設(shè)置為比較大的數(shù)值先嬉,例如256k雏掠,那么易茬,無論使用firefox還是IE瀏覽器,來提交任意小于256k的圖片结借,都很正常身冬。如果注釋該指令衅胀,使用默認的client_body_buffer_size設(shè)置,也就是操作系統(tǒng)頁面大小的兩倍酥筝,8k或者16k滚躯,問題就出現(xiàn)了。
   #無論使用firefox4.0還是IE8.0,提交一個比較大掸掏,200k左右的圖片茁影,都返回500 Internal Server Error錯誤
   client_body_buffer_size 128k;
  
   #表示使nginx阻止HTTP應(yīng)答代碼為400或者更高的應(yīng)答。
   proxy_intercept_errors on;
  
   #后端服務(wù)器連接的超時時間_發(fā)起握手等候響應(yīng)超時時間
   #nginx跟后端服務(wù)器連接超時時間(代理連接超時)
   proxy_connect_timeout 90;
  
   #后端服務(wù)器數(shù)據(jù)回傳時間(代理發(fā)送超時)
   #后端服務(wù)器數(shù)據(jù)回傳時間_就是在規(guī)定時間之內(nèi)后端服務(wù)器必須傳完所有的數(shù)據(jù)
   proxy_send_timeout 90;
  
   #連接成功后丧凤,后端服務(wù)器響應(yīng)時間(代理接收超時)
   #連接成功后_等候后端服務(wù)器響應(yīng)時間_其實已經(jīng)進入后端的排隊之中等候處理(也可以說是后端服務(wù)器處理請求的時間)
   proxy_read_timeout 90;
  
   #設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小
   #設(shè)置從被代理服務(wù)器讀取的第一部分應(yīng)答的緩沖區(qū)大小募闲,通常情況下這部分應(yīng)答中包含一個小的應(yīng)答頭,默認情況下這個值的大小為指令proxy_buffers中指定的一個緩沖區(qū)的大小愿待,不過可以將其設(shè)置為更小
   proxy_buffer_size 4k;
  
   #proxy_buffers緩沖區(qū)浩螺,網(wǎng)頁平均在32k以下的設(shè)置
   #設(shè)置用于讀取應(yīng)答(來自被代理服務(wù)器)的緩沖區(qū)數(shù)目和大小,默認情況也為分頁大小呼盆,根據(jù)操作系統(tǒng)的不同可能是4k或者8k
   proxy_buffers 4 32k;
  
   #高負荷下緩沖大心昀(proxy_buffers*2)
   proxy_busy_buffers_size 64k;
  
   #設(shè)置在寫入proxy_temp_path時數(shù)據(jù)的大小,預(yù)防一個工作進程在傳遞文件時阻塞太長
   #設(shè)定緩存文件夾大小访圃,大于這個值,將從upstream服務(wù)器傳
   proxy_temp_file_write_size 64k;
  }
    
    
  #設(shè)定查看Nginx狀態(tài)的地址
  location /NginxStatus {
   stub_status on;
   access_log on;
   auth_basic "NginxStatus";
   auth_basic_user_file confpasswd;
   #htpasswd文件的內(nèi)容可以用apache提供的htpasswd工具來產(chǎn)生相嵌。
  }
    
  #本地動靜分離反向代理配置
  #所有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;
  }
    
  #所有靜態(tài)文件由nginx直接讀取不經(jīng)過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.5 配置文件5
user                              nobody nobody;   ## 指定運行用戶和組
worker_processes                  4;               ## 指定worker數(shù)量腿时,建議此處auto
worker_rlimit_nofile              51200;           ## 最大打開文件描述符

error_log                         logs/error.log  notice;

pid                               /var/run/nginx.pid;

events {
  use                             epoll;            ## 使用epoll事件驅(qū)動模型
  worker_connections              51200;            ## 一個worker能處理的最大并發(fā)
}

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

  proxy_redirect                off;                ## 關(guān)閉代理重定向
  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;            ## 設(shè)置客戶端請求body的最大允許大小
  client_body_buffer_size       256k;            ## 設(shè)置客戶端請求body的緩沖區(qū)大小
  proxy_connect_timeout         90;                ## 與后端服務(wù)器連接的超時時長
  proxy_send_timeout            90;                ## 把請求發(fā)送給后端服務(wù)器的超時時長
  proxy_read_timeout            90;                ## 等待后端服務(wù)器發(fā)送響應(yīng)報文的超時時長
  proxy_buffer_size             128k;            ## 從代理服務(wù)器接收的響應(yīng)的第一部分緩沖區(qū)
  proxy_buffers                 4 64k;            ## 從代理服務(wù)器讀取響應(yīng)的緩沖區(qū)number和size
  proxy_busy_buffers_size       128k;            ## 限制size在響應(yīng)尚未完全讀取時可能忙于向客戶端發(fā)送響應(yīng)的緩沖區(qū)總數(shù)
  proxy_temp_file_write_size    128k;            ## 該指令設(shè)置緩沖臨時文件的最大值

  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;    ## 代理服務(wù)器接受數(shù)據(jù)臨時目錄
  fastcgi_temp_path               /var/tmp/fastcgi_temp 1 2; ## FastCGI服務(wù)器接收臨時目錄 
  uwsgi_temp_path                 /var/tmp/uwsgi_temp 1 2; ## uwsgi 服務(wù)器接收臨時目錄
  scgi_temp_path                  /var/tmp/scgi_temp 1 2; ##scgi服務(wù)器接收臨時目錄

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

  output_buffers                  2 128k;    ## 設(shè)置用于從磁盤讀取響應(yīng)的緩沖區(qū)number和size
  postpone_output                 1460;        ## 客戶端數(shù)據(jù)的傳輸最小值,單位字節(jié)

  client_header_timeout           1m;        ## 定義讀取客戶端請求標頭的超時時長
  client_body_timeout             3m;        ## 定義讀取客戶端請求主體的超時時長
  send_timeout                    3m;        ## 設(shè)置將響應(yīng)傳輸?shù)娇蛻舳说某瑫r時長


  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;        ## 啟用長連接馬上響應(yīng)饭宾,提高性能
  tcp_nopush                      off;        ## 關(guān)閉套接字選項

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


  gzip                            on;        ## 開啟壓縮
  gzip_http_version               1.1;        ## 啟用壓縮時協(xié)議最小版本
  gzip_vary                       on;         
  gzip_proxied                    any;        ## 為所有代理請求啟用壓縮
  gzip_min_length                 1024;        ## 設(shè)置將被gzip壓縮的響應(yīng)的最小長度
  gzip_comp_level                 6;        ## 設(shè)置壓縮等級
  gzip_buffers                    16 8k;    ## 設(shè)置用于壓縮響應(yīng)的緩沖區(qū)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服務(wù)器用戶(組)

指令格式:
user user [group];
user:指定可以運行Nginx服務(wù)器的用戶
group:可選項,可以運行Nginx服務(wù)器的用戶組
如果user指令不配置或者配置為user nobody nobody弹惦,則默認所有用戶都可以啟動Nginx進程

3.1.2否淤、worker process數(shù)配置

Nginx服務(wù)器實現(xiàn)并發(fā)處理服務(wù)的關(guān)鍵,指令格式:
worker_processes number | auto;
number:Nginx進程最多可以產(chǎn)生的worker process數(shù)
auto:Nginx進程將自動檢測
按照上文中的配置清單的實驗棠隐,我們給worker_processes配置的數(shù)目是:3石抡,啟動Nginx服務(wù)器后,我們可以后臺看一下主機上的Nginx進程情況:
ps -aux | grep nginx
很明顯助泽,理解 worker_processes 這個指令的含義就很容易了


image.png
3.1.3啰扛、Nginx進程PID存放路徑

Nginx進程是作為系統(tǒng)守護進程在運行,需要在某文件中保存當(dāng)前運行程序的主進程號嗡贺,Nginx支持該保存文件路徑的自定義
指令格式:
pid file;
file:指定存放路徑和文件名稱如果不指定默認置于路徑 logs/nginx.pid

3.1.4隐解、錯誤日志的存放路徑

指定格式:
error_log file | stderr;
file:日志輸出到某個文件file
stderr:日志輸出到標準錯誤輸出

3.1.5、配置文件的引入

指令格式:
include file;
該指令主要用于將其他的Nginx配置或者第三方模塊的配置引用到當(dāng)前的主配置文件中

3.2诫睬、events塊

3.2.1煞茫、設(shè)置網(wǎng)絡(luò)連接的序列化

指令格式:
accept_mutex on | off;
該指令默認為on狀態(tài),表示會對多個Nginx進程接收連接進行序列化,防止多個進程對連接的爭搶溜嗜。

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

所以在這里裹匙,如果accept_mutex on瑞凑,那么多個worker將是以串行方式來處理,其中有一個worker會被喚醒概页;反之若accept_mutex off籽御,那么所有的worker都會被喚醒,不過只有一個worker能獲取新連接惰匙,其它的worker會重新進入休眠狀態(tài)技掏。

3.2.2、是否允許同時接收多個網(wǎng)絡(luò)連接

指令格式:
multi_accept on | off;
該指令默認為off狀態(tài)项鬼,意指每個worker process 一次只能接收一個新到達的網(wǎng)絡(luò)連接哑梳。若想讓每個Nginx的workerprocess都有能力同時接收多個網(wǎng)絡(luò)連接,則需要開啟此配置

3.2.3绘盟、事件驅(qū)動模型的選擇

指令格式:
use model;
model模型可選擇項包括:select龄毡、poll、kqueue稚虎、epoll蠢终、rtsig等......

3.2.4序攘、最大連接數(shù)的配置

指令格式:
worker_connections number;
number默認值為512寻拂,表示允許每一個worker process可以同時開啟的最大連接數(shù)

3.3祭钉、HTTP塊-全局

3.3.1 定義MIME-Type

指令格式:
include mime.types;
default_type mime-type;
MIME-Type指的是網(wǎng)絡(luò)資源的媒體類型,也即前端請求的資源類型
include指令將mime.types文件包含進來
cat mime.types
來查看mime.types文件內(nèi)容申尼,我們發(fā)現(xiàn)其就是一個types結(jié)構(gòu),里面包含了各種瀏覽器能夠識別的MIME類型以及對應(yīng)類型的文件后綴名字霹粥,如下所示:


image.png
3.3.2、自定義服務(wù)日志

指令格式:
access_log path [format];
path:自定義服務(wù)日志的路徑 + 名稱
format:可選項浩淘,自定義服務(wù)日志的字符串格式。其也可以使用 log_format 定義的格式

3.3.3、允許sendfile方式傳輸文件

指令格式:
sendfile on | off;
sendfile_max_chunk size;
前者用于開啟或關(guān)閉使用sendfile()傳輸文件茴厉,默認off
后者指令若size>0,則Nginx進程的每個workerprocess每次調(diào)用sendfile()傳輸?shù)臄?shù)據(jù)了最大不能超出此值嗜闻;若size=0則表示不限制琉雳。默認值為0

3.3.4、連接超時時間配置

指令格式:
keepalive_timeout timeout [header_timeout];
timeout 表示server端對連接的保持時間束倍,默認75秒
header_timeout 為可選項甥桂,表示在應(yīng)答報文頭部的 Keep-Alive 域設(shè)置超時時間:“Keep-Alive :timeout = header_timeout”

3.4黄选、HTTP塊-Server

3.4.1、單連接請求數(shù)上限

指令格式:
keepalive_requests number;
該指令用于限制用戶通過某一個連接向Nginx服務(wù)器發(fā)起請求的次數(shù)

3.4.2懂诗、配置網(wǎng)絡(luò)監(jiān)聽

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

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

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

3.5庵朝、HTTP塊-location配置

3.5.1、location

指令格式為:
location [ = | ~ | ~* | ^~ ] uri {...}
這里的uri分為標準uri和正則uri昔逗,兩者的唯一區(qū)別是uri中是否包含正則表達式
uri前面的方括號中的內(nèi)容是可選項婆排,解釋如下:
“=”:用于標準uri前段只,要求請求字符串與uri嚴格匹配,一旦匹配成功則停止
“~”:用于正則uri前炕婶,并且區(qū)分大小寫;
“~*”:用于正則uri前,但不區(qū)分大小寫
“^~”:用于標準uri前涯贞,要求Nginx找到標識uri和請求字符串匹配度最高的location后,立即使用此location處理請求皇拣,而不再使用location塊中的正則uri和請求字符串做匹配

3.5.2、請求根目錄配置

指令格式:
root path;
path:Nginx接收到請求以后查找資源的根目錄路徑
當(dāng)然,還可以通過alias指令來更改location接收到的URI請求路徑费什,指令為:
alias path;
path為修改后的根路徑

3.5.3、設(shè)置網(wǎng)站的默認首頁

指令格式:
index file ......
file可以包含多個用空格隔開的文件名稿黍,首先找到哪個頁面言沐,就使用哪個頁面響應(yīng)請求

3.5.4、proxy_set_header

nginx上配有aaa.example.com的虛擬主機起便,現(xiàn)在需要將訪問http://aaa.example.com/api/x.x/client/的請求轉(zhuǎn)到http://bbb.example.com/api/x.x/client/bbb.example.com的虛擬主機在另外一臺nginx上鼻疮,其中x.x表示位數(shù)不定的版本號,如:1.0或1.20.345都可能水评。請求轉(zhuǎn)過去要求url保持不變。

用rewrite轉(zhuǎn)發(fā)的話,url會發(fā)生變化的咱扣,那就用proxy_pass吧,于是添加了如下的配置:

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

在現(xiàn)有環(huán)境的nginx里添加這段配置之后偏瓤,訪問卻始終轉(zhuǎn)不過去,查看nginx日志也只能看到是404信息证舟,并沒有更多定位問題的信息卵渴。檢查了許久也沒找到原因,于是重新裝了一臺新nginx碘橘,里面只加上面這段配置,結(jié)果nginx是能夠轉(zhuǎn)發(fā)成功的纺蛆,這說明單獨來看這條location的配置是沒有問題的,很有可能是現(xiàn)有環(huán)境nginx里的某些配置影響到了這個轉(zhuǎn)發(fā)。

為了定位問題原因堕伪,我將aaa.example.com虛擬主機下的其他配置注意注釋掉來調(diào)試,最后發(fā)現(xiàn)當(dāng)注釋掉proxy_set_header Host $http_host ;這條配置之后,就能成功轉(zhuǎn)發(fā)了。這才注意到是反向代理配置的問題。現(xiàn)有環(huán)境中原有的配置也不能隨便刪掉馍驯,上網(wǎng)查了下原因狂打,找到下面這種解決方案:

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;配置晾捏。當(dāng)Host設(shè)置為http_host時,則不改變請求頭的值,所以當(dāng)要轉(zhuǎn)發(fā)到bbb.example.com的時候呀伙,請求頭還是aaa.example.com的Host信息,就會有問題;當(dāng)Host設(shè)置為$proxy_host時戚篙,則會重新設(shè)置請求頭為bbb.example.com的Host信息。

另外,關(guān)于proxy_pass轉(zhuǎn)發(fā)url的參數(shù)痛倚,可以通過在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。上面例子(.*)是將所有參數(shù)傳給1,轉(zhuǎn)發(fā)時/1會拼接在http://bbb.example.com后面唆姐。

先來看下proxy_set_header的語法

語法: proxy_set_header field value;
默認值:
proxy_set_header Host $proxy_host;
proxy_set_header Connection close;
上下文: http, server, location

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

proxy_set_header Host       $proxy_host;
proxy_set_header Connection close;
nginx對于upstream默認使用的是基于IP的轉(zhuǎn)發(fā),因此對于以下配置:
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;  
    }  
}

當(dāng)匹配到/customer/straightcustomer/download時,使用crmtest處理,到upstream就匹配到crmtest.aty.sohuno.com,這里直接轉(zhuǎn)換成IP進行轉(zhuǎn)發(fā)了旁涤。假如crmtest.aty.sohuno.com是在另一臺nginx下配置的,ip為10.22.10.116,則$proxy_host則對應(yīng)為10.22.10.116。此時相當(dāng)于設(shè)置了Host為10.22.10.116是晨。如果想讓Host是crmtest.aty.sohuno.com,則進行如下設(shè)置:

proxy_set_header Host crmtest.aty.sohuno.com;

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

proxy_set_header Host       $http_host;

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

proxy_set_header Host       $host;

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

proxy_set_header Host       $host:$proxy_port;

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

proxy_set_header Accept-Encoding "";

nginx配置項孤荣,里面的配置項有代理https垃环,http,代理靜態(tài)文件,H5分發(fā)霹肝,代理TCP連接最铁,能滿足大多數(shù)搭建測試環(huán)境所要用的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圖片服務(wù)器
    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;
    #    }
    #}
 }
#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;
}


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;
    

    upstream sso {
       server 122.224.230.10:17003;
    }
    upstream dub-manage-webapps {
       server  192.168.8.113:8038;
    }
    upstream cgd {
    server  192.168.8.114:7201;
    }
    upstream dub-webapps {
       server  192.168.8.114:7205;
    }


    upstream dub-upload {
       server  192.168.8.114:7204;
    }
    
    upstream dub-webapps-yb {
        server  192.168.8.115:7203;
    }
    upstream dub-points-webapp {
        server  192.168.8.113:8088;
    }
    upstream dub-mobile {
        server  192.168.8.113:8088;
    }
    upstream dub-openapi {
        server  192.168.8.114:7200;
    }

    upstream openapi-test {
        server  192.168.3.133:8888;
    }
    
    upstream openapi-tsn-test {
    server  192.168.3.109:8888;
    }

    upstream dub-portal-webapp {
        server 192.168.8.113:8088;
    }

    upstream oitPortal {
        server 192.168.3.134:8040;
    }

    upstream oitUct {
       server 192.168.3.134:8040;
    }
    upstream oitBusiness {
        server 192.168.3.134:8040;
    }
    upstream dub-dleybsl-webapp {
        server 192.168.8.115:7200;
    }
    upstream dub-dlfreight-webapp {
        server 192.168.8.115:7201;
    }
    upstream dub-exchange-edl-webapp {
        server 192.168.8.115:7202;
    }
    
    upstream dub-examine-north-webapp {
        server 192.168.8.114:8202;
    }


    upstream dub-atta-upload-tool {
        server 192.168.8.114:7203;
    }

    upstream dub-baseparam-webapp {
        server 192.168.8.114:7206;
    }

    upstream dub-hezhu-webapp {
        server 192.168.8.116:8201;
    }


     upstream dub-szeybsl-webapp {
        server 192.168.8.116:8202;
    }

     upstream dub-szeybsl-webapp-new {
        server 192.168.8.116:8202;
    }

    upstream dub-exchange-esz-webapp {
        server 192.168.8.116:7203;
    }

    upstream dub-manifest-sz-webapp {
        server 192.168.8.116:8200;
    }


    upstream dfs {
        server 192.168.8.27:7210;
    }

    upstream dub-import-controller {
        server 192.168.8.117:8983;
    }

    upstream dub-param-controller {
        server 192.168.8.118:8002;
    }

    gzip  on;
    gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png image/jpg;
    server {
        listen       80;
        server_name  *.smartebao.com;

    #rewrite ^(.*) https://$server_name$1 permanent;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

       
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;

    #location / {
            #root  /usr/local/static/dub-webapps-h5/dist;
            #index  index.html index.htm;
        #}

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://192.168.8.113:8089;
        }

    location /dub-mobile {
        client_max_body_size    10m; 
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://dub-mobile;
        }

        location =/nginx_status {
            stub_status on;
            access_log  off;
        allow  192.168.8.113;
        deny  all;
        }

    location /dub {
            root /mnt/mfs;
            add_header Content-Disposition: 'attachment;';
        }

        location /dub-manage-webapps {
           proxy_pass http://dub-manage-webapps;
        }
    
        
        
        location /dub-webapps {
           client_max_body_size    10m; 
           proxy_pass http://dub-webapps;

         #support websocket

                proxy_set_header Host $host:$server_port;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";

        }

       
    location /dub-upload {
           client_max_body_size    1000m;
           proxy_pass http://dub-upload;
        }
    
    location /gateway {
           client_max_body_size    50m;
           proxy_pass http://openapi-test;
        }

        location /gateway2 {
           client_max_body_size    50m;
           proxy_pass http://openapi-tsn-test;
        }        



    location /dub-webapps-yb {
           client_max_body_size    100m;        
           proxy_pass http://dub-webapps-yb;
           proxy_read_timeout 3600s;

        #support websocket

                proxy_set_header Host $host:$server_port;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";

        }

    location /dub-points-webapp {
           client_max_body_size    5m;
           proxy_pass http://dub-points-webapp;

        #support websocket

                proxy_set_header Host $host:$server_port;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";

        }

    location /dub-openapi {
           client_max_body_size    100m;
       proxy_connect_timeout 300s;
       proxy_send_timeout 300s;
       proxy_read_timeout 300s;
           proxy_pass http://dub-openapi;

        #support websocket

                proxy_set_header Host $host:$server_port;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";

        }
    location /dub-portal-webapp {
                client_max_body_size    50m;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://dub-portal-webapp;
        }

          location /oitPortal {
            client_max_body_size    5m;
           proxy_pass http://oitPortal;
         }

        location /oitUct {
           client_max_body_size    5m;
           proxy_pass http://oitUct;
        }
        location /oitBusiness {
           client_max_body_size    5m;
           proxy_pass http://oitBusiness;
        }

        location /dub-dleybsl-webapp {
           client_max_body_size    5m;
           proxy_pass http://dub-dleybsl-webapp;
        }

        location /dub-dlfreight-webapp {
           client_max_body_size    5m;
           proxy_pass http://dub-dlfreight-webapp;
        }

        location /dub-exchange-edl-webapp {
           client_max_body_size    5m;
           proxy_pass http://dub-exchange-edl-webapp;
        }

        location /dub-examine-north-webapp {
           client_max_body_size    5m;
           proxy_pass http://dub-examine-north-webapp;
        }


        location /dub-atta-upload-tool {
           client_max_body_size    5m;
           proxy_pass http://dub-atta-upload-tool;
        }
        
        location /dub-baseparam-webapp {
           client_max_body_size    5m;
           proxy_pass http://dub-baseparam-webapp;
        }
        
        
        
        location /sso {
           client_max_body_size    5m;
           proxy_pass http://sso;
        }

    location /dfs {
            client_max_body_size   20m;
            proxy_pass http://dfs;
        }

        location /dub-import-controller {
            client_max_body_size    5m;
            proxy_pass http://dub-import-controller;
        }

        location /dub-param-controller {
            client_max_body_size    5m;
            proxy_pass http://dub-param-controller;
        }

        location ^~/dub-webapp-shenzhen/ {
            alias   /usr/local/tomcat/dub-front-end-sz/dist/;
            gzip on; #開啟或關(guān)閉gzip on off
            gzip_static on;  #是否開啟gzip靜態(tài)資源
            gzip_disable "msie6"; #不使用gzip IE6
            gzip_min_length 100k; #gzip壓縮最小文件大小,超出進行壓縮(自行調(diào)節(jié))
            gzip_buffers 4 16k; #buffer 不用修改
            gzip_comp_level 3; #壓縮級別:1-10膊夹,數(shù)字越大壓縮的越好,時間也越長
            gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; #           壓縮文件類型
            gzip_vary off;  #跟Squid等緩存服務(wù)有關(guān)放刨,on的話會在Header里增加 "Vary: Accept-Encoding"

        }


        location ^~/dub-webapp-ningbo/ {
            alias   /usr/local/tomcat/dub-front-end-nb/dist/;
            gzip on; #開啟或關(guān)閉gzip on off
            gzip_static on;  #是否開啟gzip靜態(tài)資源
            gzip_disable "msie6"; #不使用gzip IE6
            gzip_min_length 100k; #gzip壓縮最小文件大小嚎卫,超出進行壓縮(自行調(diào)節(jié))
            gzip_buffers 4 16k; #buffer 不用修改
            gzip_comp_level 3; #壓縮級別:1-10,數(shù)字越大壓縮的越好宏榕,時間也越長
            gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; #           壓縮文件類型
            gzip_vary off;  #跟Squid等緩存服務(wù)有關(guān),on的話會在Header里增加 "Vary: Accept-Encoding"

        }

        location ^~/dub-webapp-sh/ {
            alias   /usr/local/tomcat/dub-front-end-sh/dist/;
            gzip on; #開啟或關(guān)閉gzip on off
            gzip_static on;  #是否開啟gzip靜態(tài)資源
            gzip_disable "msie6"; #不使用gzip IE6
            gzip_min_length 100k; #gzip壓縮最小文件大小侵佃,超出進行壓縮(自行調(diào)節(jié))
            gzip_buffers 4 16k; #buffer 不用修改
            gzip_comp_level 3; #壓縮級別:1-10麻昼,數(shù)字越大壓縮的越好,時間也越長
            gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; #           壓縮文件類型
            gzip_vary off;  #跟Squid等緩存服務(wù)有關(guān)馋辈,on的話會在Header里增加 "Vary: Accept-Encoding"

        }

         location ^~/dub-webapp-dl/ {
             alias   /usr/local/tomcat/dub-front-end-dl/dist/;
             gzip on; #開啟或關(guān)閉gzip on off
             gzip_static on;  #是否開啟gzip靜態(tài)資源
             gzip_disable "msie6"; #不使用gzip IE6
             gzip_min_length 100k; #gzip壓縮最小文件大小抚芦,超出進行壓縮(自行調(diào)節(jié))
             gzip_buffers 4 16k; #buffer 不用修改
             gzip_comp_level 3; #壓縮級別:1-10,數(shù)字越大壓縮的越好迈螟,時間也越長
             gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif imag    e/png; #           壓縮文件類型
             gzip_vary off;  #跟Squid等緩存服務(wù)有關(guān)叉抡,on的話會在Header里增加 "Vary: Accept-Encoding"

         }

        location ^~/dub-webapp-index/ {
            alias   /usr/local/tomcat/dub-webapp-index/dist/;
            gzip on; #開啟或關(guān)閉gzip on off
            gzip_static on;  #是否開啟gzip靜態(tài)資源
            gzip_disable "msie6"; #不使用gzip IE6
            gzip_min_length 100k; #gzip壓縮最小文件大小,超出進行壓縮(自行調(diào)節(jié))
            gzip_buffers 4 16k; #buffer 不用修改
            gzip_comp_level 3; #壓縮級別:1-10答毫,數(shù)字越大壓縮的越好褥民,時間也越長
            gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; #           壓縮文件類型
            gzip_vary off;  #跟Squid等緩存服務(wù)有關(guān),on的話會在Header里增加 "Vary: Accept-Encoding"

        }

        location /dub-hezhu-webapp {
           client_max_body_size    5m;
           proxy_pass http://dub-hezhu-webapp;
        }


        location /dub-manifest-sz-webapp {
           client_max_body_size    5m;
           proxy_pass http://dub-manifest-sz-webapp;
        }


         location /dub-szeybsl-webapp {
           client_max_body_size    5m;
           proxy_pass http://dub-szeybsl-webapp;
        }

         location ^~ /dub-szeybsl-webapp-new {
           client_max_body_size    5m;
           proxy_pass http://dub-szeybsl-webapp-new/dub-szeybsl-webapp/;
        }

        location /dub-exchange-esz-webapp {
           client_max_body_size    5m;
           proxy_pass http://dub-exchange-esz-webapp;
        }
        
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # 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 8899;
       
    server_name  192.168.8.113;
    location / {
        root   html;
            index  index.html index.htm;
        }


    location /cgd {
           client_max_body_size    5m; 
           proxy_pass http://cgd;
        }

        error_page 404 403  500 502 503 504  /400.html;
                location =/400.html {
                root /codeflag/site/site_web/error_html;
        }
        location ~/errorHtml/ {
            rewrite /errorHtml/(.*) /$1 break;
            root /codeflag/site/site_web/error_html;
        }
     }





   server{
        listen 8888;
        root /usr/local/static/user_web/dist/;
        index index.html index.htm;
#        server_name xxx.xxxx.com;
#        access_log  /home/www/test/temp/nginx_service_logs/nginx_user_access.log  main;
#        error_log  /home/www/test/temp/nginx_service_logs/nginx_user_error.log  error;

        location /customerApi/{
                proxy_pass http://192.168.8.113:8081;
                client_max_body_size    1000m;
        }
        location /basis/ {
            proxy_pass http://192.168.8.113:8081;
            client_max_body_size    1000m;
        }
        location /api/ {
            proxy_pass http://192.168.8.113:8081;
            client_max_body_size    1000m;
        }
        location /coapi/ {
            rewrite /coapi/(.*) /$1 break;
            proxy_pass http://192.168.8.113:8081;
            client_max_body_size    1000m;
        }
        location /pack_image/ {
            rewrite /coapi/(.*) /$1 break;
            proxy_pass http://192.168.8.113:8081;
            client_max_body_size    1000m;
        }
        location /ediRelated/ {
            rewrite /coapi/(.*) /$1 break;
            proxy_pass http://192.168.8.113:8081;
            client_max_body_size    1000m;
        }
        location /yhapi/ {
            rewrite /coapi/(.*) /$1 break;
            proxy_pass http://192.168.8.113:8081;
            client_max_body_size    1000m;
        }
        location /oss/ {
                rewrite /oss/(.*) /$1 break;
                proxy_pass http://upload.autoport.com.cn;
        }
        location / {
                try_files $uri $uri/ /index.html;
        }
        error_page 404 403  500 502 503 504  /400.html;
                location =/400.html {
                root /codeflag/site/site_web/error_html;
        }
        location ~/errorHtml/ {
            rewrite /errorHtml/(.*) /$1 break;
            root /codeflag/site/site_web/error_html;
        }
     }


    server {
        listen       8089 ssl;
          server_name  localhost;
        ssl_certificate      /usr/local/nginx/cert/200314_.smartebao.com_bundle.crt;
        ssl_certificate_key  /usr/local/nginx/cert/200314_smartebao.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 / {
            root  /usr/local/static/dub-portal-html/dist;
            index  index.html index.htm;
        }
    location /dub-portal-webapp {
                client_max_body_size    50m;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://dub-portal-webapp;
        }
    }


      #溫州大屏
      server {
        listen       8082;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/dubaixwz.access.log  info;

        location / {
            root   html/dist;
            index  index.html index.htm;
        }
        
        location /dubaixwz {
            proxy_pass http://192.168.3.46:6070/dubaixwz;
            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       443 ssl;
        server_name  *.smartebao.com;

        ssl_certificate      /usr/local/nginx/cert/200314_.smartebao.com_bundle.crt;
        ssl_certificate_key  /usr/local/nginx/cert/200314_smartebao.com.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;
        }

    location /dub-openapi {
           client_max_body_size    50m;
           proxy_pass http://dub-openapi;

        #support websocket

                proxy_set_header Host $host:$server_port;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";

        }

    location /gateway {
               client_max_body_size    50m;
               proxy_pass http://openapi-test;
        #support websocket
                proxy_set_header Host $host:$server_port;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }
      location /gateway2 {
                 client_max_body_size    50m;
                 proxy_pass http://openapi-tsn-test;
                 #support websocket;
                 proxy_set_header Host $host:$server_port;
         proxy_http_version 1.1;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection "upgrade";
    } 

    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末洗搂,一起剝皮案震驚了整個濱河市消返,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌耘拇,老刑警劉巖撵颊,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異惫叛,居然都是意外死亡倡勇,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門嘉涌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來妻熊,“玉大人,你說我怎么就攤上這事洛心」淘牛” “怎么了?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵词身,是天一觀的道長厅目。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么损敷? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任葫笼,我火速辦了婚禮,結(jié)果婚禮上拗馒,老公的妹妹穿的比我還像新娘路星。我一直安慰自己,他們只是感情好诱桂,可當(dāng)我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布洋丐。 她就那樣靜靜地躺著,像睡著了一般挥等。 火紅的嫁衣襯著肌膚如雪友绝。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天肝劲,我揣著相機與錄音迁客,去河邊找鬼。 笑死辞槐,一個胖子當(dāng)著我的面吹牛掷漱,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播榄檬,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼卜范,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了丙号?” 一聲冷哼從身側(cè)響起先朦,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎犬缨,沒想到半個月后喳魏,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡怀薛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年刺彩,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片枝恋。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡创倔,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出焚碌,到底是詐尸還是另有隱情畦攘,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布十电,位于F島的核電站知押,受9級特大地震影響叹螟,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜台盯,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一罢绽、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧静盅,春花似錦良价、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至市咽,卻和暖如春袖外,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背魂务。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留泌射,地道東北人粘姜。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像熔酷,于是被迫代替她去往敵國和親孤紧。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,512評論 2 359