09-nginx常用配置詳解
Nginx配置分為各個配置塊,主配置塊負責全局配置,子配置塊可以繼承全局配置,也可以相應的配置不同設置。
main block:主配置(全局配置) event{ ... }事件驅動相關配置塊 http{ ... }http/https 協議相關的配置塊 mail{ ... }郵件服務器相關的配置塊 stream{ ... }流服務器相關的配置塊
主配置按照功能通常分為四類:
1.運行必備的配置
2.優化性能的配置
3.調試及定位問題的配置
4.事件驅動相關配置
運行必備的配置
-- user :定義worker進程的用戶與用戶組,默認是nginx
Syntax: user user [group];
Default: user nobody nobody;
Context: main
-- pid :定義存儲Master進程ID的文件路徑
Syntax: pid file;
Default: pid nginx.pid;
Context: main
-- include:配置文件可嵌入其他配置文件
-- load_module:加載動態模塊
性能優化相關配置
-- worker_processes:定義worker進程的數量,通常為內核數量減一,也可定義為auto
worker_processes auto;
-- worker_cpu_affinity:設定cpu與worker進程綁定,默認不綁定,假設由4顆CPU,值可設為 auto ,或者
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
[[email protected] nginx]# ps axo comm,pid,psr | grep nginx nginx 10729 3 nginx 10797 0 nginx 10798 1 nginx10799 2 nginx 10800 3
[[email protected] nginx]# ps aux | grep nginx root 10729 0.0 0.2 122892 5180 ? Ss 10:59 0:00 nginx: master process /usr/sbin/nginx nginx 10797 0.0 0.2 123256 4008 ? S 11:29 0:00 nginx: worker process nginx 10798 0.0 0.2 123256 4012 ? S 11:29 0:00 nginx: worker process nginx 10799 0.0 0.2 123256 4016 ? S 11:29 0:00 nginx: worker process nginx 10800 0.0 0.2 123256 3788 ? S 11:29 0:00 nginx: worker process root 10808 0.0 0.0 112660 968 pts/0 R+ 11:31 0:00 grep --color=auto nginx
-- worker_priority number:指定worker進程的nice值,默認為0,值越小優先級越高,取值範圍 -20-20
worker_priority -5; [[email protected] nginx]# ps axo comm,pid,psr,ni | grep nginx nginx 10729 3 0 nginx 10830 0 -5 nginx 10831 1 -5 nginx 10832 2 -5 nginx 10833 3 -5
-- worker_rlimit_nofile number:worker進程所能打開的文件數量上限,一般往上了調,50000;
# For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes 4; worker_cpu_affinity 0001 0010 0100 1000; error_log /var/log/nginx/error.log; pid /run/nginx.pid; worker_priority -5; worker_rlimit_nofile 50000; # Load dynamic modules. See /usr/share/nginx/README.dynamic.
定位調試相關配置
-- daemon:決定nginx是否成為守護進程
-- master_process:決定是否啟用worker進程
-- error_log:日誌相關
事件驅動相關配置
events {
...
}
-- worker_connections number:單個worker進程所能打開的最大並發連接數量;如果作反向代理,那麽同時包括發送和接受,數量還要/2;
-- use method:指明處理請求的方法;
-- accept_mutex:可選值 on | off ,如果啟用on,worker進程接受請求時采取輪流處理新請求,off則通知所有worker進程。連接請求較少時建議啟用on,可避免worker進程資源浪費;
events { worker_connections 10000; use epoll; accept_mutex on; }
09-nginx常用配置詳解