Nginx 的運作幾乎都圍繞著一份核心設定檔 nginx.conf 展開。它把全域參數、事件模型、HTTP 處理邏輯串在一起,理解每一段的含義,才能針對自己的硬體與業務場景做出合適的調校。以下就從全域、events、http 三大區塊,搭配 FastCGI、反向代理與負載均衡等常見範例,逐一說明。
檔案開頭先設定執行身分、工作程序數與各類檔案路徑,events 區塊則決定單一工作程序能承載的連線規模。
#用戶 用戶組 user www www; #工作進程,根據硬件調整,有人說幾核cpu,就配幾個,我覺得可以多一點 worker_processes 5; #錯誤日志 error_log logs/error.log; #pid文件位置 pid logs/nginx.pid; worker_rlimit_nofile 8192; events { #工作進程的最大連接數量,根據硬件調整,和前面工作進程配合起來用,盡量大,但是別把cpu跑到100%就行 worker_connections 4096; }
實務上,worker_processes 可對應 CPU 核心數,worker_connections 則依機器規格放大,兩者相乘即為理論最大承載連線數,需與系統的檔案描述符上限(worker_rlimit_nofile)相互匹配。
http 區塊是 Nginx 處理 Web 請求的中樞,這裡載入 MIME 類型、FastCGI 與反向代理的通用參數,並定義日誌格式。
http { include conf/mime.types; #反向代理配置,可以打開proxy.conf看看 include /etc/nginx/proxy.conf; #fastcgi配置,可以打開fastcgi.conf看看 include /etc/nginx/fastcgi.conf; default_type application/octet-stream; #日志的格式 log_format main '$remote_addr - $remote_user [$time_local] $status ' '"$request" $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #訪問日志 access_log logs/access.log main; sendfile on; tcp_nopush on; #根據實際情況調整,如果server很多,就調大一點 server_names_hash_bucket_size 128;
下方是兩個 server 範例:前者演示 PHP 透過 FastCGI 交給後端處理,後者則是標準的反向代理寫法。
server { # php/fastcgi listen 80; #域名,可以有多個 server_name domain1.com www.domain1.com; #訪問日志,和上面的級別不一樣,應該是下級的覆蓋上級的 access_log logs/domain1.access.log main; root html; location / { index index.html index.htm index.php; } location ~ \.php$ { fastcgi_pass 127.0.0.1:1025; } } server { # simple reverse-proxy listen 80; server_name domain2.com www.domain2.com; access_log logs/domain2.access.log main; location ~ ^/(images|javascript|js|css|flash|media|static)/ { root /var/www/virtual/big.server.com/htdocs; expires 30d; } location / { proxy_pass https://127.0.0.1:8080; } }
反向代理與 FastCGI 的差別在於:反向代理的後端是一台完整的 Web 服務器,而 FastCGI 的後端是監聽中的 FastCGI 進程,兩者協定的本質並不相同。
當有多台後端時,可用 upstream 定義資源池,並以 weight 控制分配權重。
upstream big_server_com { server 127.0.0.3:8000 weight=5; server 127.0.0.3:8001 weight=5; server 192.168.0.1:8000; server 192.168.0.1:8001; } server { listen 80; server_name big.server.com; access_log logs/big.server.access.log main; location / { proxy_pass https://big_server_com; } } }
前面 include 的幾個檔案通常不需更動,使用時直接引入即可。以下為 fastcgi.conf、proxy.conf 與 mime.types 的內容。
fastcgi.conf # fastcgi.conf fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; fastcgi_index index.php; fastcgi_param REDIRECT_STATUS 200;
proxy.conf # proxy.conf 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 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffers 32 4k;
mime.types # mime.types types { text/html html htm shtml; text/css css; text/xml xml rss; image/gif gif; image/jpeg jpeg jpg; application/x-javascript js; text/plain txt; text/x-component htc; text/mathml mml; image/png png; image/x-icon ico; image/x-jng jng; image/vnd.wap.wbmp wbmp; application/java-archive jar war ear; application/mac-binhex40 hqx; application/pdf pdf; application/x-cocoa cco; application/x-java-archive-diff jardiff; application/x-java-jnlp-file jnlp; application/x-makeself run; application/x-perl pl pm; application/x-pilot prc pdb; application/x-rar-compressed rar; application/x-redhat-package-manager rpm; application/x-sea sea; application/x-shockwave-flash swf; application/x-stuffit sit; application/x-tcl tcl tk; application/x-x509-ca-cert der pem crt; application/x-xpinstall xpi; application/zip zip; application/octet-stream deb; application/octet-stream bin exe dll; application/octet-stream dmg; application/octet-stream eot; application/octet-stream iso img; application/octet-stream msi msp msm; audio/mpeg mp3; audio/x-realaudio ra; video/mpeg mpeg mpg; video/quicktime mov; video/x-flv flv; video/x-msvideo avi; video/x-ms-wmv wmv; video/x-ms-asf asx asf; video/x-mng mng; }
掌握以上結構後,就能依實際需求靈活調整 Nginx,兼顧效能、可擴充性與可維護性。