1. 程式人生 > >Nginx.conf配置檔案詳解

Nginx.conf配置檔案詳解

執行使用者 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 { worker_connections 1024;#單個後臺worker process程序的最大併發連結數 } 設定http伺服器,利用它的反向代理功能提供負載均衡支援 http { #設定mime型別,型別由mime.type檔案定義 include mime.types; default_type application/octet-stream; #設定日誌格式 #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; #伺服器設定的最大上傳檔案大小 client_max_body_size 200m; #開啟gzip壓縮 gzip on; gzip_comp_level 2; gzip_types text/css application/javascript application/json; gzip_disable “MSIE [1-6].”;

#設定負載均衡的伺服器列表 upstream collectionBackend { #本機上的Squid開啟7002埠 server 127.0.0.1:7002; }

server { #偵聽80埠 listen 80; #定義使用localhost訪問 server_name localhost;

#預設值90s, nginx連線到後端伺服器的連線超時時間 proxy_connect_timeout 90;

#設定了傳送請求給upstream伺服器的超時時間。超時設定不是為了整個傳送期間,而是在兩次write操作期間。如果超時後,upstream沒有收到新的資料,nginx會關閉連線 proxy_send_timeout 90;

設定與代理伺服器的讀超時時間。它決定了nginx會等待多長時間來獲得請求的響應。這個時間不是獲得整個response的時間,而是兩次reading操作的時間。 proxy_read_timeout 90;

nginx設定目錄瀏覽及中文亂碼問題解決方案: #charset koi8-r;

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

在這個location配置段中,如果URL請求“/public/logo.gif”,那麼nginx將會在伺服器上查詢“F:/workspace/cloud/bv-frontend-dev/public/logo.gif”檔案,即請求的URL中location後面的部分會被追加到alias指定的目錄後面,而location後面的“/public”路徑將會被自動丟棄。 location /public/ { alias F:/workspace/cloud/bv-frontend-dev/public/; }

location / { #rewrite的主要功能是實現RUL地址的重定向 rewrite ‘^/simple/.*.(html)$’ /simple-index/simple.html last;

rewrite ‘^/simple/.*config.js$’ /simple-index/config.js last;

rewrite ‘^/simple/./custom/(.)’ /simple-index/custom/$1 last;

rewrite ‘^/simple/./api/(.)’ /api/$1 last;

rewrite ‘^/simple/custom/(.*)’ /simple-index/custom/$1 last;

rewrite ‘^/simple/api/(.*)’ /api/$1 last;

alias同上含義: alias F:/workspace/bestvike/collection-handle/static/; index index.html; }

location /simple-index { alias F:/workspace/bestvike/collection-handle/static/; }

location /resources/ { rewrite ‘^/resources/simple/(.*.html)/simpleindex/simple/1 last; alias F:/workspace/bestvike/collection-handle/static/simple/; }

location /api/ {

proxy_set_header:允許重新定義或者添加發往後端伺服器的請求頭.該值可以包含文字、變數和它們的組合。在沒有定義proxy_set_header時會繼承之前定義的值。預設情況下,只有兩個欄位被重定義:proxy_set_header Host $proxy_host 和 proxy_set_header Connection close

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header REMOTE-HOST $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_pass URL,URL為要設定的被代理伺服器的地址,包含傳輸協議、主機名稱或IP地址加埠號、URI等要素。 proxy_pass http://collectionBackend/api/; }

error_page:當發生錯誤的時候能夠顯示一個預定義的地址,實際上產生了一個內部跳轉(internal redirect),當訪問出現500、502、503、504的時候就能返回50x.html中的內容。同時我們也可以自己定義這種情況下的返回狀態碼。

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