Nginx解析-配置

一、主配置段

1、正常運(yùn)行必備的配置

#運(yùn)行用戶和組,組身份可以省略
user nginx nginx;

#指定nginx守護(hù)進(jìn)程的pid文件
pid path/to/nginx.pid;

#指定所有worker進(jìn)程所能打開的最大文件句柄數(shù)
worker_rlimit_nofile 100000;

2、性能優(yōu)化相關(guān)的配置

#worker進(jìn)程的個(gè)數(shù),通常應(yīng)該略少于CPU物理核心數(shù),也可以使用auto自動(dòng)獲取
worker_processes auto;

#CPU的親緣性綁定(同樣是無法避免CPU的上下文的切換的)
#優(yōu)點(diǎn):提升緩存的命中率
#context switch:會(huì)產(chǎn)生CPU不必要的消耗
work_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

#計(jì)時(shí)器解析度(請(qǐng)求到達(dá)nginx,nginx相應(yīng)用戶請(qǐng)求后,要獲取系統(tǒng)時(shí)間并記錄日志,高并發(fā)的時(shí)候可能每秒鐘獲取很多很多次)
#降低此值,可以減少gettimeofday()系統(tǒng)調(diào)用的次數(shù)
timer_resolution 100ms;

#指明worker進(jìn)程的nice值:數(shù)字越小,優(yōu)先級(jí)越高
#nice值范圍:-20,19
#對(duì)應(yīng)的優(yōu)先級(jí):100,139
worker_priority number;

二、用于調(diào)試、定位問題

#是否以守護(hù)進(jìn)程方式運(yùn)行nginx;調(diào)試時(shí)應(yīng)該設(shè)置為off
daemon {on|off}

#是否以master/worker模型來運(yùn)行;調(diào)試時(shí)可以設(shè)置為off
master_process {on|off}

#error_log 位置 級(jí)別,若要使用debug,需要在編譯nginx時(shí)使用--with-debug選項(xiàng)
error_log file | stderr | syslog:server=address[,parameter=value] | memory:size [debug|info|notice|warn|error|crit|alert|emerg];

總結(jié):常需要調(diào)整的參數(shù):worker_processes, worker_connections,work_cpu_affinity,worker_priority
新改動(dòng)配置生效方式:
nginx -s reload其他參數(shù)stop,quit,reopen也可以使用nginx -h查看到

三、nginx作為web服務(wù)器使用的配置

http {}:由ngx_http_core_module模塊所引入
配置框架:

http {
    upstream {
        ...
    }
    server {
        location URL {
            root "/path/to/somedir"
            ...
        }#類似于httpd中的<Location>,用于定義URL與本地文件系統(tǒng)的映射關(guān)系
        location URL {
            if ... {
                ...
            }
        }
    }#每個(gè)server類似于httpd中的一個(gè)<VirtualHost>
    server {
        ...
    }
}

四、完整說明

########   Nginx的main(全局配置)文件
#指定nginx運(yùn)行的用戶及用戶組,默認(rèn)為nobody
#user  nobody;   

#開啟的線程數(shù),一般跟邏輯CPU核數(shù)一致
worker_processes  1;   

#定位全局錯(cuò)誤日志文件,級(jí)別以notice顯示,還有debug,info,warn,error,crit模式,debug輸出最多,crir輸出最少,根據(jù)實(shí)際環(huán)境而定
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#指定進(jìn)程id的存儲(chǔ)文件位置
#pid        logs/nginx.pid;

#指定一個(gè)nginx進(jìn)程打開的最多文件描述符數(shù)目,受系統(tǒng)進(jìn)程的最大打開文件數(shù)量限制
#worker_rlimit_nofile 65535

events {
    #指明使用的時(shí)間模型:建議讓Nginx自行選擇
    use [epoll|rtsig|select|poll];
    
    #定義每個(gè)進(jìn)程的最大連接數(shù),受系統(tǒng)進(jìn)程的最大打開文件數(shù)量限制。
    worker_connections  1024;

    #master調(diào)度用戶請(qǐng)求至個(gè)worker進(jìn)程時(shí)使用的負(fù)載均衡鎖:on表示能讓多個(gè)worker輪流地、序列化的響應(yīng)新請(qǐng)求
    accept_mutex {off|on}
    
    #延遲等待時(shí)間,默認(rèn)為500ms
    accept_mutex_delay time;
    
    #accept_mutex用到的鎖文件路徑
    lock_file file;
    
    #單個(gè)worker進(jìn)程打開的最大并發(fā)連接數(shù),worker_processes*worker_connections
    worker_connections 2048;
    
    #告訴nginx收到一個(gè)新鏈接通知后接受盡可能多的鏈接
    multi_accept on;   

    # 并發(fā)總數(shù)是 worker_processes 和 worker_connections 的乘積
    # 即 max_clients = worker_processes * worker_connections
    # 在設(shè)置了反向代理的情況下,max_clients = worker_processes * worker_connections / 4  為什么
    # 為什么上面反向代理要除以4,應(yīng)該說是一個(gè)經(jīng)驗(yàn)值
    # 根據(jù)以上條件,正常情況下的Nginx Server可以應(yīng)付的最大連接數(shù)為:4 * 8000 = 32000
    # worker_connections 值的設(shè)置跟物理內(nèi)存大小有關(guān)
    # 因?yàn)椴l(fā)受IO約束,max_clients的值須小于系統(tǒng)可以打開的最大文件數(shù)
    # 而系統(tǒng)可以打開的最大文件數(shù)和內(nèi)存大小成正比,一般1GB內(nèi)存的機(jī)器上可以打開的文件數(shù)大約是10萬左右
    # 我們來看看360M內(nèi)存的VPS可以打開的文件句柄數(shù)是多少:
    # $ cat /proc/sys/fs/file-max
    # 輸出 34336
    # 32000 < 34336,即并發(fā)連接總數(shù)小于系統(tǒng)可以打開的文件句柄總數(shù),這樣就在操作系統(tǒng)可以承受的范圍之內(nèi)
    # 所以,worker_connections 的值需根據(jù) worker_processes 進(jìn)程數(shù)目和系統(tǒng)可以打開的最大文件總數(shù)進(jìn)行適當(dāng)?shù)剡M(jìn)行設(shè)置
    # 使得并發(fā)總數(shù)小于操作系統(tǒng)可以打開的最大文件數(shù)目
    # 其實(shí)質(zhì)也就是根據(jù)主機(jī)的物理CPU和內(nèi)存進(jìn)行配置
    # 當(dāng)然,理論上的并發(fā)總數(shù)可能會(huì)和實(shí)際有所偏差,因?yàn)橹鳈C(jī)還有其他的工作進(jìn)程需要消耗系統(tǒng)資源。
    # ulimit -SHn 65535
}

#######Nginx的Http服務(wù)器配置,Gzip配置
http {
    #主模塊指令,實(shí)現(xiàn)對(duì)配置文件所包含的文件的設(shè)定,可以減少主配置文件的復(fù)雜度,DNS主配置文件中的zonerfc1912,acl基本上都是用include語句。
    include       mime.types;
    
    #核心模塊指令,智力默認(rèn)設(shè)置為二進(jìn)制流,也就是當(dāng)文件類型未定義時(shí)使用這種方式
    default_type  application/octet-stream;

    #下面代碼為日志格式的設(shè)定,main為日志格式的名稱,可自行設(shè)置,后面引用
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #引用日志main
    #access_log  logs/access.log  main;

    #設(shè)置允許客戶端請(qǐng)求的最大的單個(gè)文件字節(jié)數(shù)
    #client_max_body_size 20M;
    #指定來自客戶端請(qǐng)求頭的headebuffer大小
    #client_header_buffer_size  32k;
    #指定連接請(qǐng)求試圖寫入緩存文件的目錄路徑
    #client_body_temp_path /dev/shm/client_body_temp;
    #指定客戶端請(qǐng)求中較大的消息頭的緩存最大數(shù)量和大小,目前設(shè)置為4個(gè)32KB
    #large client_header_buffers 4 32k;
    
    #開啟高效文件傳輸模式
    sendfile        on;
    #開啟防止網(wǎng)絡(luò)阻塞
    #tcp_nopush     on;
    #開啟防止網(wǎng)絡(luò)阻塞
    #tcp_nodelay    on;
    
    #設(shè)置客戶端連接保存活動(dòng)的超時(shí)時(shí)間
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #設(shè)置客戶端請(qǐng)求讀取超時(shí)時(shí)間
    #client_header_timeout 10;
    #設(shè)置客戶端請(qǐng)求主體讀取超時(shí)時(shí)間
    #client_body_timeout 10;
    #用于設(shè)置相應(yīng)客戶端的超時(shí)時(shí)間
    #send_timeout 
    
    ####HttpGZip模塊配置
    #httpGzip modules
    #開啟gzip壓縮
    #gzip  on;
    #設(shè)置允許壓縮的頁面最小字節(jié)數(shù)
    #gzip_min_length 1k;
    #申請(qǐng)4個(gè)單位為16K的內(nèi)存作為壓縮結(jié)果流緩存
    #gzip_buffers 4 16k;
    #設(shè)置識(shí)別http協(xié)議的版本,默認(rèn)為1.1
    #gzip_http_version 1.1;
    #指定gzip壓縮比,1-9數(shù)字越小,壓縮比越小,速度越快
    #gzip_comp_level 2;
    #指定壓縮的類型
    #gzip_types text/plain application/x-javascript text/css application/xml;
    #讓前端的緩存服務(wù)器進(jìn)過gzip壓縮的頁面
    #gzip_vary on;  
    
    #########Nginx的server虛擬主機(jī)配置
    server {
        #監(jiān)聽端口為 80
        listen       80;
        
        #設(shè)置主機(jī)域名
        server_name  localhost;
        
        #設(shè)置訪問的語言編碼
        #charset koi8-r;

        #設(shè)置虛擬主機(jī)訪問日志的存放路徑及日志的格式為main
        #access_log  logs/host.access.log  main;

        #設(shè)置虛擬主機(jī)的基本信息
        location / {
            #設(shè)置虛擬主機(jī)的網(wǎng)站根目錄
            root   html;
            
            #設(shè)置虛擬主機(jī)默認(rèn)訪問的網(wǎng)頁
            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;
    #    }
    #}

}

五、參考

1、nginx內(nèi)置變量詳解
2、nginx根據(jù)$remote_addr分發(fā) nginx根據(jù)客戶端IP分發(fā)
3、nginx基本配置與參數(shù)說明
4、Nginx配置參數(shù)說明
5、nginx基本配置與參數(shù)說明
6、nginx的location、root、alias指令用法和區(qū)別
7、隨筆分類 - Nginx & Openresty
8、nginx 常量

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容