nginx 學習筆記(二) http、https、utp/tcp 代理配置 nginx.conf

安全問題,建議用nobody,不要用root.
user root;

worker數和服務器的cpu數相等是最爲適宜
worker_processes 1;

error_log path(存放路徑) level(日誌等級)path表示日誌路徑,level表示日誌等級,
具體以下:[ debug | info | notice | warn | error | crit ]
從左至右,日誌詳細程度逐級遞減,即debug最詳細,crit最少,默認爲crit。
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {

#這個值是表示每一個worker進程所能創建鏈接的最大值,因此,一個nginx能創建的最大鏈接數,

#應該是worker\_connections * worker\_processes。  
#固然,這裏說的是最大鏈接數,對於HTTP請求本地資源來講,可以支持的最大併發數量是worker\_connections * worker\_processes,  
#若是是支持http1.1的瀏覽器每次訪問要佔兩個鏈接,  
#因此普通的靜態訪問最大併發數是: worker\_connections * worker\_processes /2,  
#而若是是HTTP做爲反向代理來講,最大併發數量應該是worker\_connections * worker\_processes/4。  
#由於做爲反向代理服務器,每一個併發會創建與客戶端的鏈接和與後端服務的鏈接,會佔用兩個鏈接。  
worker_connections  1024;

#這個值是表示nginx要支持哪一種多路io複用。
#通常的Linux選擇epoll, 若是是(*BSD)系列的Linux使用kquene。
#windows版本的nginx不支持多路IO複用,這個值不用配。
use epoll;

# 當一個worker搶佔到一個連接時,是否儘量的讓其得到更多的鏈接,默認是off 。  
multi_accept on;

# 默認是on ,開啓nginx的搶佔鎖機制。  
accept_mutex  on;

}

tcp/udp 代理
stream {

upstream backend {

# 一、輪詢(默認)  
# 每一個請求按時間順序逐一分配到不一樣的後端服務器,若是後端服務器down掉,能自動剔除。  
# 二、指定權重  
# 指定輪詢概率,weight和訪問比率成正比,用於後端服務器性能不均的狀況。  
#三、IP綁定 ip_hash  
# 每一個請求按訪問ip的hash結果分配,這樣每一個訪客固定訪問一個後端服務器,能夠解決session的問題。  
#四、備機方式 backup  
# 正常狀況不訪問設定爲backup的備機,只有當全部非備機全都宕機的狀況下,服務纔會進備機。  
#五、fair(第三方)  
#按後端服務器的響應時間來分配請求,響應時間短的優先分配。     
#六、url_hash(第三方)  
#按訪問url的hash結果來分配請求,使每一個url定向到同一個後端服務器,後端服務器爲緩存時比較有效。  
  #還能夠設置請求失敗的次數。max_fails  
    #hash $remote_addr consistent;  
    # 代理tcp,都代理到3306端口,可是weight(權重)不同,代理到252服務的機率爲50%,代理到251服務的機率爲30%.  
    server 192.168.1.252:3306 weight=5 max\_fails=3 fail\_timeout=30s;  
    server 192.168.1.253:3306 weight=2 max\_fails=3 fail\_timeout=30s;  
}  

#配置tcp  

server {
listen 3306;
proxy_connect_timeout 3s;
proxytimeout 10s; proxypass backend;

}

upstream dnsservers{ server 192.168.47.44:53; } #配置udp server { listen 53 udp; #容許訪問的IP allow 192.168.21.216; #除216以外的IP都不容許訪問 deny all; #UDP traffic will be proxied to the "dnsservers" upstream group
proxy_pass dns_servers;
}

}

http {

# lua 腳本的配置  
  lua\_package\_path "/usr/local/src/nginx/conf/waf/?.lua";  
  lua\_shared\_dict limit 10m;  
  init\_by\_lua_file /usr/local/src/nginx/conf/waf/init.lua;  
 access\_by\_lua_file /usr/local/src/nginx/conf/waf/waf.lua;

#當web服務器收到靜態的資源文件請求時,依據請求文件的後綴名在服務器的MIME配置文件中找到對應的MIME Type,再根據MIME Type設置HTTP Response的Content-Type,而後瀏覽器根據Content-Type的值處理文件。
include mime.types;

#若是 不能從mime.types找到映射的話,用如下做爲默認值
default_type application/octet-stream;

#關閉反向代理緩存
proxy_buffering off;

#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;

#開啓從磁盤直接到網絡的文件傳輸,適用於有大文件上傳下載的狀況,提升IO效率。

sendfile        on;  
#tcp_nopush     on;

#一個請求完成以後還要保持鏈接多久, 默認爲0,表示完成請求後直接關閉鏈接。

#keepalive_timeout  0;  
keepalive_timeout  65;

#開啓或者關閉gzip模塊

#gzip  on;  

#設置容許壓縮的頁面最小字節數,頁面字節數從header頭中的Content-Length中進行獲取。
#gzip_min_lenth 1k;

# gzip壓縮比,1 壓縮比最小處理速度最快,9 壓縮比最大但處理最慢(傳輸快但比較消耗cpu)  
#gzip\_comp\_level 4;

#匹配MIME類型進行壓縮,(不管是否指定)"text/html"類型老是會被壓縮的。  
#gzip_types types text/plain text/css application/json  application/x-javascript text/xml 

#動靜分離  
#服務器端靜態資源緩存,最大緩存到內存中的文件,不活躍期限  
open\_file\_cache max=655350 inactive=20s;     
#活躍期限內最少使用的次數,不然視爲不活躍。  
open\_file\_cache\_min\_uses 2;

#驗證緩存是否活躍的時間間隔  
open\_file\_cache_valid 30s;

   server {  
    listen       81;  
    server_name  nginx.test1.com;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

#location \[=|~|~*|^~\] /uri/ { … }     
# = 精確匹配  
# ~ 正則匹配,區分大小寫  
# ~\* 正則匹配,不區分大小寫  
# ^~  關閉正則匹配  
#匹配原則:  
# 一、全部匹配分兩個階段,第一個叫普通匹配,第二個叫正則匹配。  
# 二、普通匹配,首先經過「=」來匹配徹底精確的location  
    #   2.一、 若是沒有精確匹配到, 那麼按照最大前綴匹配的原則,來匹配location  
    #   2.二、 若是匹配到的location有^~,則以此location爲匹配最終結果,若是沒有那麼會把匹配的結果暫存,繼續進行正則匹配。  
    # 三、正則匹配,依次從上到下匹配前綴是~或~*的location, 一旦匹配成功一次,則馬上以此location爲準,再也不向下繼續進行正則匹配。  
    # 四、若是正則匹配都不成功,則繼續使用以前暫存的普通匹配成功的location.

# 匹配任何查詢,由於全部請求都已 / 開頭。可是正則表達式規則和長的塊規則將被優先和查詢匹配。

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

    #反向代理路徑  
        proxy_pass http://192.168.2.3/test;

    #反向代理的超時時間  
        proxy\_connect\_timeout 10;

        proxy_redirect default;       

     }

 # auth\_request表示須要調用第三方接口進行受權,後臺response.setStatus(200)返回時會繼續走proxy\_pass 代理。

    location /test {  
     auth_request /auth;  
     proxy_pass http://192.168.40.26:8080/cloud/wapi/v1.0/hello;  
    }  
       location /auth {  
        proxy_pass http://192.168.40.26:8080/cloud/wapi/v1.0/auth/;  
        #proxy\_pass\_request_body off;  
        #proxy\_set\_header Content-Length "";  
        #proxy\_set\_header X-Original-URI $request_uri;  
    }  


 }  
server {  
    listen       80;  
    server_name  localhost;  
    #charset koi8-r;

    #access_log  logs/host.access.log  main;

      proxy\_set\_header   Host   $host;  
      proxy\_set\_header   Referer $http_referer;  
      proxy\_set\_header   Cookie $http_cookie;  
      proxy\_set\_header   X-Real-IP  $remote_addr;  
      proxy\_set\_header   X-Forwarded-For              $proxy\_add\_x\_forwarded\_for;

  # 訪問test2時,會經過lua腳本的access\_by\_lua調用後臺方法v1.0/auth方法。

    location /test2 {  
     access\_by\_lua 'local res = ngx.location.capture("/v1.0/auth")  
        if res.status == ngx.HTTP_OK then  
        return  
        end  
        if res.status == ngx.HTTP_FORBIDDEN then  
        ngx.exit(res.status)  
        end  
        ngx.exit(ngx.HTTP\_INTERNAL\_SERVER_ERROR)  
        ';  
     proxy_pass http://192.168.40.119:8888/ipam-server;  

    }

  #調用後臺方法v1.0/auth方法 時會被代理到192.168.40.26:8080/v1.0/auth上去。  
    location ~ /v1.0/(.*) {  
          internal;  
         proxy_pass http://192.168.40.26:8080;  
     }

    #error_page  404              /404.html;

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

    # 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       443 ssl;  
    server_name  nginx.test2.com;

  # 經過 openssl 生成對應的證書

   ssl_certificate      /usr/local/src/nginx/ssl/nginx.crt;   
   ssl\_certificate\_key  /usr/local/src/nginx/ssl/nginx.key;

   ssl\_session\_cache    shared:SSL:1m;  
   ssl\_session\_timeout  5m;

   ssl_ciphers  HIGH:!aNULL:!MD5;  
   ssl\_prefer\_server_ciphers  on;

    location = /favicon.ico {  
        log\_not\_found off;  
        access_log off;  
    }

  # https的代理單點登陸

    location /cas/login {  
      proxy\_set\_header Host $host:$server_port;    
      proxy\_set\_header   Referer $http_referer;  
      proxy\_set\_header   Cookie $http_cookie;  
      proxy\_set\_header X-Real-IP $remote_addr;    
      proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
      proxy\_set\_header X-FORWARDED-HOST $server_addr;    
      proxy\_set\_header X-FORWARDED-PORT $server_port;

     # 單點登陸的服務器的IP  
      proxy_pass https://192.168.40.119:8090/cas/login;  
    }

  # 登陸單點登陸時所需的css、js、jpg等文件

    location ~*\\.(gif|jpg|jpeg|png|css|js|jsp|htm)$ {          

      # 此處不能配置 https://192.168.40.119:8090/ 不然會報錯                                                                                           
         proxy_pass https://192.168.40.119:8090;  
    }  

 }

}
相關文章
相關標籤/搜索