Nginx配置文件詳解


1、Nginx采用的是編譯安裝,安裝目錄在/apps/nginx/,Nginx的配置文件nginx.conf位於其安裝目錄的conf目錄下,/apps/nginx/conf/nginx.conf
Nginx的配置⽂件的組成部分: 主配置⽂件:nginx.conf,⼦配置⽂件 include conf.d/*.conf。Nginx.conf由多個塊組成,依次是全局配置,Events和HTTP,HTTP包含upstream和多個Server,Server又包含多個location。過濾掉注釋,空行,安裝之后的默認配置如下所示:

2、全局配置端

全局配置端,對全局⽣效,主要設置nginx的啟動⽤⼾/組,啟動的⼯作進程數量,⼯作模式,Nginx的PID路徑,⽇志路徑等。

2.1 user nginx nginx; #啟動Nginx⼯作進程的⽤⼾和組
2.2 worker_processes [number | auto]; #啟動Nginx⼯作進程的數量,數量由cpu決定,四核就會開啟4個工作進程。可以寫數字1~4,也可以auto自動檢測,例如虛擬機四核,我選的是auto自動模式,查看進程如下:一個主進程,四個工作進程

worker_cpu_affinity  #將Nginx⼯作進程綁定到指定的CPU核⼼,默認Nginx是不進⾏進程綁定的,綁定並不是意味着當前nginx進程獨占以⼀核⼼CPU,但是可以保證此進程不會運⾏在其他核⼼上,這就極⼤減少了nginx的⼯作進程在不同的cpu核⼼上的來回跳轉,減少了CPU對進程的資源分配與回收以及內存管理等,因此可以有效的提升nginx服務器的性能。四核可以進行如下綁定,worker_cpu_affinity 0001 0010 0100 1000。當然nginx的官方文檔也支持auto自動綁定。

綁定之前:

 

 選擇 worker_cpu_affinity 0001 0010 0100 1000 綁定之后:

 

 2.3 錯誤日志

#錯誤⽇志記錄配置,語法:error_log file [debug | info | notice | warn | error | crit |alert | emerg]

#error_log logs/error.log;

#error_log logs/error.log notice;
error_log /apps/nginx/logs/error.log error;

2.4   pid⽂件保存路徑       pid /apps/nginx/logs/nginx.pid;

2.5   worker_rlimit_nofile

worker_rlimit_nofile 65536; #這個數字包括Nginx的所有連接(例如與代理服務器的連接等),⽽不僅僅是與客⼾端的連接,另⼀個考慮因素是實際的並發連接數不能超過系統級別的最⼤打開⽂件數的限制.這個值一般調大點

 

 

3、events事件模型配置

events設置快,主要影響nginx服務器與⽤⼾的⽹絡連接,⽐如是否允許同時接受多個⽹絡連接,使⽤哪種事件驅動模型處理請求,每個⼯作進程可以同時⽀持的最⼤連接數,是否開啟對多⼯作進程下的⽹絡連接進⾏序列化等

3.1  worker_connections  65536

設置單個nginx⼯作進程可以接受的最⼤並發,作為web服務器的時候最⼤並發數為worker_connections * worker_processes,作為反向代理的時候為(worker_connections *worker_processes)/2

3.2  use epoll;

使⽤epoll事件驅動(默認就是),Nginx⽀持眾多的事件驅動,⽐如select、poll、epoll,只能設置在events模塊中設置。比如使用select模塊,就可以寫成use selec,如果出現錯誤可能是編譯安裝的時候沒有安裝select模塊,需要重新編譯安裝

3.3 accept_mutex on; #優化同⼀時刻只有⼀個請求⽽避免多個睡眠進程被喚醒的設置,on為防⽌被同時喚醒默認為off,全部喚醒的過程也成為"驚群",因此nginx剛安裝完以后要進⾏適當的優化。

3.4 multi_accept on; Nginx服務器的每個⼯作進程可以同時接受多個新的⽹絡連接,但是需要在配置⽂件中配置,此指令默認為關閉,即默認為⼀個⼯作進程只能⼀次接受⼀個新的⽹絡連接,打開后⼏個同時接受多個。

 

 

 

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 logs/access.log main;

 

#⾃定義優化參數
sendfile on; #實現⽂件零拷⻉
#tcp_nopush on; #在開啟了sendfile的情況下,合並請求后統⼀發送給客⼾端。
#tcp_nodelay off; #在開啟了keepalived模式下的連接是否啟⽤TCP_NODELAY選項,當為off時,延遲0.2s發送,默認On時,不延遲發送,⽴即發送⽤⼾相應報⽂。
#keepalive_timeout 0;
keepalive_timeout 65 65; #設置會話保持時間
#gzip on; #開啟⽂件壓縮

 

server {
listen 80; #設置監聽地址和端⼝
server_name localhost; #設置server name,可以以空格隔開寫多個並⽀持正則表達式,如*.xxxx.com www.xxxxx.* www.(site\d+)\.xxxxx\.com$ default_server
#charset koi8-r; #設置編碼格式,默認是俄語格式,可以改為utf-8
charset utf-8;
#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;
} #可以自定義404錯誤界面,如下:

 

#error_page 404 /404.html; #定義錯誤⻚⾯
#location = /404.html {
#root html; } 然后自己再做一個404的html文件就好了

 

 

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ { #以http的⽅式轉發php請求到指定web服務器
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ { #以fastcgi的⽅式轉發php請求到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 { #拒絕web形式訪問指定⽂件,如很多的⽹站都是通過.htaccess⽂件來改變⾃⼰
的重定向等功能。
# deny all;
#}
location ~ /passwd.html {
deny all;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server { #⾃定義虛擬server
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm; #指定默認⽹⻚⽂件,此指令由ngx_http_index_module模
塊提供
# }
#}
# HTTPS server
#
#server { #https服務器配置
# 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;
# }
location /linux38/passwd.ht {
deny all;
}
#}

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM