Nginx 學習總結(2) —— 基本配置

這是 Nginx 學習總結的第二篇,上一篇介紹到了 Nginx 學習總結(1) —— 概述,這一篇會對 Nginx 的基本配置作一些總結。php

Nginx 配置信息主要分爲六大部分: main(全局設置)events(事件設置)http(HTTP服務器設置)sever(虛擬主機設置)location(URL匹配設置)upstream(反向代理設置)html

main 模塊

user nobody nobody;

worker_processes auto;

error_log  /usr/local/var/log/nginx/error.log  error;

pid        /usr/local/var/run/nginx/nginx.pid;

worker_rlimit_nofile 1024;
  • user 指定 Worker 進程的運行用戶以及用戶組,默認由 nobody 運行;
  • worker_processes 設置 Nginx 要開啓的 Worker 進程數。最優值取決於許多因素,包括(但不限於)CPU 內核的數量、存儲數據的硬盤數量及負載模式。不能肯定的時候,將其設置爲可用的 CPU 內核數將是一個好的開始(設置爲auto將嘗試自動檢測它);
  • error_log 指定全局錯誤日誌的輸出文件位置和錯誤級別。日誌級別有 debuginfonoticewarnerrorcritalertemerg 可供選擇,錯誤級別依次遞增。設置某個日誌級別將記錄指定的以及錯誤級別更高的全部消息(即 debug 輸出日誌最爲最詳細,而 emerg 輸出日誌最少)。默認開啓爲 error。想要開啓 debug 錯誤級別,須要在編譯 Nginx 時指定 --with-debug 參數;
  • pid 指定記錄 PID 進程的存儲文件位置( Nginx 向 Worker 進程發送信號時,須要知道是在向哪一個進程發送信息,而不一樣的進程有不一樣的 PID),用戶僅需建立該空白文件便可;
  • worker_rlimit_nofile 設置每一個 Worker 進程能夠打開的最大文件數目(若是不設置該選項,則該選項的值爲操做系統的限制值,使用「ulimit -a」命令可查看)。

events 模塊

events {
    use epoll; 
    worker_connections  1024;
}
  • use 設置事件處理模型。Nginx 支持的事件處理模型有: selectpollkqueueepollrtsig/dev/poll,其中 kqueue(BSD 特有) 和 epoll(Linux 特有) 都是高效的事件處理模型。若是不設置,Nginx 將會自動選擇一個最適合當前操做系統的事件處理模型;
  • worker_connections 設置每一個 Worker 進程能併發處理的最大鏈接數。最大客戶端鏈接數由 worker_processesworker_connections 決定:Nginx 做爲 HTTP 服務器時,Max_clients = worker_processes * worker_connections;Nginx 做爲反向代理時,Max_clients = worker_processes * worker_connections / 4

http 模塊

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  /usr/local/var/log/nginx/access.log  main;
    #access_log off

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;

    keepalive_timeout  65;
    
    # gzip壓縮功能設置
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers    4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 6;
    
    client_max_body_size 10M;
    client_body_buffer_size 128k;
    
    # http_proxy 設置
    proxy_connect_timeout   75;
    proxy_send_timeout   75;
    proxy_read_timeout   75;
    proxy_buffer_size   4k;
    proxy_buffers   4 32k;
    proxy_busy_buffers_size   64k;
    proxy_temp_file_write_size  64k;
    proxy_temp_path   /usr/local/nginx/proxy_temp 1 2;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}
  • include 加載 mime.type 文件,用於幫助 Nginx 識別文件的 MIME 類型;
  • default_type 指定文件默認的 MIME 類型。例如,在沒有配置 asp 的環境時,Nginx 是不予解析的,此時,瀏覽器訪問 asp 文件就會出現下載了;
  • log_format 設置日誌的格式;
  • access_log 設置 access_log 日誌文件地址;
  • sendfile 是否開啓高效文件傳輸模式。將 tcp_nopushtcp_nodelay 兩個指令設置爲 on 用於防止網絡阻塞;
  • keepalive_timeout 設置客戶端鏈接保持活動的超時時間,在超過這個時間以後,服務器會關閉該鏈接;
  • gzip 是否開啓 gzip 壓縮,這將會減小咱們發送的數據量;
  • client_max_body_size 容許客戶端請求的最大單文件字節數。若是上傳較大文件,請增長它的限制值;
  • client_body_buffer_size 容許客戶端請求的最大緩衝區字節數;
  • proxy_connect_timeout 設置與後端服務器鏈接超時時間。

server 模塊

server {
    listen       80;
    server_name  localhost 192.168.12.10 jochen.com;
        
    root   /usr/www;
    index  index.php index.html index.htm; 

    charset utf-8;
    
    error_page  404   /404.html;
    error_page  403   /403.html;
    error_page  500 502 503 504  /50x.html;
    
    access_log  usr/local/var/log/host.access.log  main;
    aerror_log  usr/local/var/log/host.error.log  error;
}
  • listen 設置監聽端口,默認 80,小於 1024 的要以 root 用戶啓動。能夠爲 listen *:80listen 127.0.0.1:80 等形式;
  • server_name 設置域名,多個域名之間用空格分開;
  • root 設置虛擬主機的默認網站根目錄;
  • index 定義默認訪問的文件名;
  • charset 設置網頁的默認編碼格式;
  • error_page 設置錯誤頁面,相對於上面的 root 目錄。

location 模塊

location / {
    root   /usr/share/nginx/html;
    index  index.php index.html index.htm;
}

location ~ \.php$ {
    root           /usr/share/nginx/html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi.conf;
}

location /nginx-status {
  stub_status on;
  allow 192.168.10.100;
  allow 172.29.73.0/24;
  deny all;
}

location 模塊用於針對某些特定的 URL 進行配置,有關 location 匹配規則詳情請見《Nginx 學習總結(3) —— location 模塊》。node

upstram 模塊

upstream jochen.com {
    ip_hash;
    server 192.168.12.1:80;
    server 192.168.12.2:80 down;
    server 192.168.12.3:8080  max_fails=3  fail_timeout=20s;
    server 192.168.12.4:8080;
}

upstream 模塊用於負載均衡,有關 upstream 模塊詳情請見《Nginx 學習總結(7) —— 負載均衡》。nginx

參考文章:segmentfault

  1. nginx documentation
  2. nginx 服務器安裝及配置文件詳解
  3. nginx 的配置、虛擬主機、負載均衡和反向代理(1)
相關文章
相關標籤/搜索