1. 程式人生 > >一篇不大靠譜的nginx 1.11.10配置檔案

一篇不大靠譜的nginx 1.11.10配置檔案

前言

網站是前後端分離,前端打包站點部署需要自力更生,為了避免跨域問題.
選擇了nginx這個知名的反向代理伺服器.
這裡不探究安裝這種問題。。。

版本 : nginx-1.11.10

配置檔案(nginx.conf)


#user  nobody; # 使用者許可權
worker_processes  auto;  # 工作程序的數量
#worker_cpu_affinity auto;

#全域性錯誤日誌及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)中的一種方式, #use 設定用於複用客戶端執行緒的輪詢方法。如果你使用Linux 2.6+,你應該使用epoll。如果你使用*BSD,你應該使用kqueue。 use epoll; # IOCP[window] , kqueue[bsd] , epoll[linux] #單個後臺worker process程序的最大併發連結數 worker_connections 1024; #multi_accept 告訴nginx收到一個新連線通知後接受盡可能多的連線。
multi_accept on; # 併發總數是 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 { include mime.types; #設定mime型別,型別由mime.type檔案定義 default_type application/octet-stream; #設定日誌格式 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #設定nginx是否將儲存訪問日誌。關閉這個選項可以讓讀取磁碟IO操作更快 #access_log logs/access.log main; #sendfile 指令指定 nginx 是否呼叫 sendfile 函式(zero copy 方式)來輸出檔案, #對於普通應用,必須設為 on, #如果用來進行下載等應用磁碟IO重負載應用,可設定為 off, #以平衡磁碟與網路I/O處理速度,降低系統的uptime. # 高效檔案傳輸 sendfile on; tcp_nopush on; tcp_nodelay on; # types_hash_max_size 影響散列表的衝突率。types_hash_max_size越大,就會消耗更多的記憶體,但雜湊key的衝突率會降低,檢索速度就更快。types_hash_max_size越小,消耗的記憶體就越小,但雜湊key的衝突率可能上升。 types_hash_max_size 2048; #連線超時時間 #keepalive_timeout 0; keepalive_timeout 65; gzip on; gzip_disable "MSIE [1-6]."; gzip_comp_level 6; gzip_min_length 1000; #置對資料啟用壓縮的最少位元組數。如果一個請求小於1000位元組,我們最好不要壓縮它,因為壓縮這些小的資料會降低處理此請求的所有程序的速度。 gzip_proxied expired no-cache no-store private auth; gzip_types text/plain application/x-javascript text/xml text/css application/xml; #Buffers:另一個很重要的引數為buffer,如果buffer太小,Nginx會不停的寫一些臨時檔案,這樣會導致磁碟不停的去讀寫,現在我們先了解設定buffer的一些相關引數: #client_body_buffer_size:允許客戶端請求的最大單個檔案位元組數 #client_header_buffer_size:用於設定客戶端請求的Header頭緩衝區大小,大部分情況1KB大小足夠 #client_max_body_size:設定客戶端能夠上傳的檔案大小,預設為1m #large_client_header_buffers:該指令用於設定客戶端請求的Header頭緩衝區大小 #設定請求緩衝 client_body_buffer_size 10K; client_header_buffer_size 1k; client_max_body_size 8m; large_client_header_buffers 2 1k; map $http_user_agent $outdated { # 判斷瀏覽器版本 default 0; "~MSIE [6-9].[0-9]" 1; "~MSIE 10.0" 1; } # weight:輪詢權值,預設值為1。 # down:表示當前的server暫時不參與負載。 # max_fails:允許請求失敗的次數,預設為1。當超過最大次數時,返回 proxy_next_upstream 模組定義的錯誤。 # fail_timeout:有兩層含義,一是在fail_timeout時間內最多容許max_fails次失敗;二是在經歷了max_fails次失敗以後,30s時間內不分配請求到這臺伺服器。 # backup : 備份機器。當其他所有的非 backup 機器出現故障的時候,才會請求backup機器,因此這臺機器的壓力最輕。 # max_conns: 限制同時連線到某臺後端伺服器的連線數,預設為 0。即無限制。 # proxy_next_upstream : 這個指令屬於 http_proxy 模組的,指定後端返回什麼樣的異常響應 #負載均衡 #upstream DataBase { # ip_hash; # 每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端伺服器,可以解決session的問題。 # server 10.xx.xx.xx weight=1 max_fails=2 fail_timeout=30s; # server 10.xx.xx.xx; # server 10.xx.xx.xx; #} server { listen 80; #埠號 server_name v.fpdiov.com; #域名 location /api { #代理API proxy_pass http://api.fpdiov.com:8090; # proxy_set_header Host $host; # proxy_set_header X-Real-IP $remote_addr; #在web伺服器端獲得使用者的真實ip # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_redirect off; proxy_connect_timeout 600; #nginx跟後端伺服器連線超時時間(代理連線超時) proxy_read_timeout 600; #連線成功後,後端伺服器響應時間(代理接收超時) proxy_send_timeout 600; #後端伺服器資料回傳時間(代理髮送超時) proxy_buffer_size 32k; #設定代理伺服器(nginx)儲存使用者頭資訊的緩衝區大小 proxy_buffers 4 32k; #proxy_buffers緩衝區,網頁平均在32k以下的話,這樣設定 proxy_busy_buffers_size 64k; #高負荷下緩衝大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #設定快取資料夾大小,大於這個值,將從upstream服>務器傳 keepalive_requests 500; proxy_http_version 1.1; proxy_ignore_client_abort on; } location ^~ / { root /mnt/www/fpd-car-manage-frontend; #啟動根目錄 if ($outdated = 1){ rewrite ^ http://oisbyqrnc.bkt.clouddn.com redirect; #判斷瀏覽器版本跳轉 } index index.html; #預設訪問頁面 try_files $uri $uri/ /index.html; } } #server { # listen 80; #偵聽80埠 # server_name localhost; #訪問域名 #charset koi8-r; # 字符集 #access_log logs/host.access.log main; #location / { # root html; # index index.html index.htm; #} #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; #} #靜態檔案,nginx自己處理 #location ~ ^/(images|javascript|js|css|flash|media|static)/ { #過期30天,靜態檔案不怎麼更新,過期可以設大一點, #如果頻繁更新,則可以設定得小一點。 # expires 30d; #} # 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 { #啟用 https, 使用 http/2 協議, nginx 1.9.11 啟用 http/2 會有bug, 已在 1.9.12 版本中修復. # listen 443 ssl; # server_name localhost; # ssl on; # 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; # 設定協商加密演算法時,優先使用我們服務端的加密套件,而不是客戶端瀏覽器的加密套件。 # ssl_session_timeout 60m; #快取有效期 # ssl_session_cache shared:SSL:10m; #儲存SSL會話的快取型別和大小 # location / { # root html; # index index.html index.htm; # } #} }

總結

配置檔案包含了以下一些考慮點:

  • 反向代理
  • 壓縮
  • 連線數
  • 瀏覽器版本過低重定向
  • 頭部快取及其他一些快取設定(效能優化)
  • 負載均衡的寫法(註釋了)
  • https的寫法(註釋了)

至此,公司的網站已經跑起來了。若覺得有用就保留一份吧,沒用就當衝浪吧;
抽空寫個Typescript 2+Angular 2&4的系列教程。。。這篇文章也算是一個鋪墊吧