1. 程式人生 > >Nginx運維之三 配置說明

Nginx運維之三 配置說明

Nginx運維之三 配置說明

配置結構

檢視系統預設配置檔案

vim  /usr/local/nginx/nginx.conf
#user  nobody;
worker_processes  1;

#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; } 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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 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; } # 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 localhost; # 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; # location / { # root html; # index index.html index.htm; # } #} }

可以看出nginx給出的預設配置 大致結構如下

.....                                  #全域性引數配置塊
worker_processes  1;                      
.....

events {                               # nginx工作模式events塊配置
	....                            
}
http {                                 # http 配置塊
	....
	default_type  application/octet-stream;
	....
	
	upstream apiexample {            # 負載均衡配置塊
		server 103.1.248.230:8080;
		server 104.25.253.107:8080;
	}

	server {                          # Server配置塊
		....
		listen       80;
		....
		location / {                  # location 配置塊
			....
			root   html;
			....
		}
        
		location ^~ /example/ {      # location 配置塊二(支援多location配置)
			....
			proxy_set_header   Host    $host;
			proxy_pass   http://apiexample;
			client_max_body_size    10m;
			....
		}
	}
	
	server {
		....
		listen       443 ssl;        # Server配置塊二(支援多server,案例給出的是http 和 https的分別配置)
		....
		location / {
			....
			root   html;
			....
		}
	}
    
}

簡而言之,我們可以理解為Nginx配置檔案主要由6個部分組成:

  • main:用於進行nginx全域性資訊的配置
  • events:用於nginx工作模式的配置
  • http:用於進行http協議資訊的一些配置
  • upstream:用於進行負載均衡的配置
  • server:用於進行伺服器訪問資訊的配置
  • location:用於進行訪問路由的配置

main模組

 #指定nginx執行的使用者及使用者組,預設為nobody
#user  nobody;   

#開啟的程序數,保持與邏輯CPU核數一致 cat /proc/cpuinfo| grep "processor"| wc -l
worker_processes  16;   

#定位全域性錯誤日誌檔案路徑和級別,日誌級別依次為debug,info,notice,warn,error,crit模式,debug輸出最多,crir輸出最少,根據實際環境而定
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#指定程序id的儲存檔案位置
#pid        logs/nginx.pid;

#指定一個nginx程序開啟的最多檔案描述符數目,受系統程序的最大開啟檔案數量限制  ulimit -a 檢視
#worker_rlimit_nofile 65535

events 模組

推薦配置

events
{
        use epoll;
        worker_connections 65536;
        multi_accept on;
}
指令 語法 預設值 說明
accept_mutex accept_mutex [ on | off ] off Nginx使用連線互斥鎖進行順序的accept()系統呼叫
multi_accept multi_accept [ on | off ] off multi_accept在Nginx接到一個新連線通知後呼叫accept()來接受盡量多的連線
use use [ kqueue | rtsig | epoll | /dev/poll | select | poll | eventport ] 使用的事件模型
worker_connections worker_connections number 1024 Nginx每個程序的最大連線數

事件模型

與apache相類,nginx針對不同的作業系統,有不同的事件模型:

  • 標準事件模型
    select、poll屬於標準事件模型,如果當前系統不存在更有效的方法,nginx會選擇select或poll

  • 高效事件模型
    kqueue:使用於FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用雙處理器的MacOS X系統使用kqueue可能會造成核心崩潰。
    epoll:使用於Linux核心2.6版本及以後的系統。
    /dev/poll:使用於Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。
    eventport:使用於Solaris 10. 為了防止出現核心崩潰的問題, 有必要安裝安全補丁.

對於Linux系統,epoll工作模式是首選。

worker_connections

用於定義Nginx每個程序的最大連線數,即接收前端的最大請求數,預設是1024。最大客戶端連線數由worker_processes和worker_connections決定,即

Max_clients=worker_processes*worker_connections

在作為反向代理時,Max_clients變為:

Max_clients = worker_processes * worker_connections/4

程序的最大連線數受Linux系統程序的最大開啟檔案數限制,在執行作業系統命令“ulimit -n 65536”後worker_connections的設定才能生效。