Nginx(二)------nginx.conf 配置文件
上一篇博客我們將 nginx 安裝在 /usr/local/nginx 目錄下,其默認(rèn)的配置文件都放在這個(gè)目錄的 conf 目錄下,而主配置文件 nginx.conf 也在其中,后續(xù)對(duì) nginx 的使用基本上都是對(duì)此配置文件進(jìn)行相應(yīng)的修改,所以本篇博客我們先大致介紹一下該配置文件的結(jié)構(gòu)。
回到頂部
1、nginx.conf 的主體結(jié)構(gòu)
打開(kāi)此文件,內(nèi)容如下:
1 #user nobody;2 worker_processes 1;3 4 #error_log logs/error.log;5 #error_log logs/error.log notice;6 #error_log logs/error.log info;7 8 #pid logs/nginx.pid;9 10 11 events {12 worker_connections 1024;13 }14 15 16 http {17 include mime.types;18 default_type application/octet-stream;19 20 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '21 # '$status $body_bytes_sent "$http_referer" '22 # '"$http_user_agent" "$http_x_forwarded_for"';23 24 #access_log logs/access.log main;25 26 sendfile on;27 #tcp_nopush on;28 29 #keepalive_timeout 0;30 keepalive_timeout 65;31 32 #gzip on;33 34 server {35 listen 80;36 server_name localhost;37 38 #charset koi8-r;39 40 #access_log logs/host.access.log main;41 42 location / {43 root html;44 index index.html index.htm;45 }46 47 #error_page 404 /404.html;48 49 # redirect server error pages to the static page /50x.html50 #51 error_page 500 502 503 504 /50x.html;52 location = /50x.html {53 root html;54 }55 56 # proxy the PHP scripts to Apache listening on 127.0.0.1:8057 #58 #location ~ \.php$ {59 # proxy_pass http://127.0.0.1;60 #}61 62 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:900063 #64 #location ~ \.php$ {65 # root html;66 # fastcgi_pass 127.0.0.1:9000;67 # fastcgi_index index.php;68 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;69 # include fastcgi_params;70 #}71 72 # deny access to .htaccess files, if Apache's document root73 # concurs with nginx's one74 #75 #location ~ /\.ht {76 # deny all;77 #}78 }79 80 81 # another virtual host using mix of IP-, name-, and port-based configuration82 #83 #server {84 # listen 8000;85 # listen somename:8080;86 # server_name somename alias another.alias;87 88 # location / {89 # root html;90 # index index.html index.htm;91 # }92 #}93 94 95 # HTTPS server96 #97 #server {98 # listen 443 ssl;99 # server_name localhost; 100 101 # ssl_certificate cert.pem; 102 # ssl_certificate_key cert.key; 103 104 # ssl_session_cache shared:SSL:1m; 105 # ssl_session_timeout 5m; 106 107 # ssl_ciphers HIGH:!aNULL:!MD5; 108 # ssl_prefer_server_ciphers on; 109 110 # location / { 111 # root html; 112 # index index.html index.htm; 113 # } 114 #} 115 116 }# 開(kāi)頭的表示注釋內(nèi)容,我們?nèi)サ羲幸?# 開(kāi)頭的段落,精簡(jiǎn)之后的內(nèi)容如下:
1 worker_processes 1;2 3 events {4 worker_connections 1024;5 }6 7 8 http {9 include mime.types; 10 default_type application/octet-stream; 11 12 13 sendfile on; 14 15 keepalive_timeout 65; 16 17 server { 18 listen 80; 19 server_name localhost; 20 21 location / { 22 root html; 23 index index.html index.htm; 24 } 25 26 error_page 500 502 503 504 /50x.html; 27 location = /50x.html { 28 root html; 29 } 30 31 } 32 33 }? 根據(jù)上述文件,我們可以很明顯的將 nginx.conf 配置文件分為三部分:
回到頂部
2、全局塊
從配置文件開(kāi)始到 events 塊之間的內(nèi)容,主要會(huì)設(shè)置一些影響nginx 服務(wù)器整體運(yùn)行的配置指令,主要包括配置運(yùn)行 Nginx 服務(wù)器的用戶(組)、允許生成的 worker process 數(shù),進(jìn)程 PID 存放路徑、日志存放路徑和類型以及配置文件的引入等。
比如上面第一行配置的:
worker_processes 1;這是 Nginx 服務(wù)器并發(fā)處理服務(wù)的關(guān)鍵配置,worker_processes 值越大,可以支持的并發(fā)處理量也越多,但是會(huì)受到硬件、軟件等設(shè)備的制約,這個(gè)后面會(huì)詳細(xì)介紹。
回到頂部
3、events 塊
比如上面的配置:
events {worker_connections 1024; }events 塊涉及的指令主要影響 Nginx 服務(wù)器與用戶的網(wǎng)絡(luò)連接,常用的設(shè)置包括是否開(kāi)啟對(duì)多 work process 下的網(wǎng)絡(luò)連接進(jìn)行序列化,是否允許同時(shí)接收多個(gè)網(wǎng)絡(luò)連接,選取哪種事件驅(qū)動(dòng)模型來(lái)處理連接請(qǐng)求,每個(gè) word process 可以同時(shí)支持的最大連接數(shù)等。
上述例子就表示每個(gè) work process 支持的最大連接數(shù)為 1024.
這部分的配置對(duì) Nginx 的性能影響較大,在實(shí)際中應(yīng)該靈活配置。
回到頂部
4、http 塊
1 http {2 include mime.types;3 default_type application/octet-stream;4 5 6 sendfile on;7 8 keepalive_timeout 65;9 10 server { 11 listen 80; 12 server_name localhost; 13 14 location / { 15 root html; 16 index index.html index.htm; 17 } 18 19 error_page 500 502 503 504 /50x.html; 20 location = /50x.html { 21 root html; 22 } 23 24 } 25 26 }這算是 Nginx 服務(wù)器配置中最頻繁的部分,代理、緩存和日志定義等絕大多數(shù)功能和第三方模塊的配置都在這里。
需要注意的是:http 塊也可以包括?http全局塊、server 塊。
①、http 全局塊
http全局塊配置的指令包括文件引入、MIME-TYPE 定義、日志自定義、連接超時(shí)時(shí)間、單鏈接請(qǐng)求數(shù)上限等。
②、server 塊
這塊和虛擬主機(jī)有密切關(guān)系,虛擬主機(jī)從用戶角度看,和一臺(tái)獨(dú)立的硬件主機(jī)是完全一樣的,該技術(shù)的產(chǎn)生是為了節(jié)省互聯(lián)網(wǎng)服務(wù)器硬件成本。后面會(huì)詳細(xì)介紹虛擬主機(jī)的概念。
每個(gè) http 塊可以包括多個(gè) server 塊,而每個(gè) server 塊就相當(dāng)于一個(gè)虛擬主機(jī)。
而每個(gè) server 塊也分為全局 server 塊,以及可以同時(shí)包含多個(gè) locaton 塊。
1、全局 server 塊
最常見(jiàn)的配置是本虛擬機(jī)主機(jī)的監(jiān)聽(tīng)配置和本虛擬主機(jī)的名稱或IP配置。
2、location 塊
一個(gè) server 塊可以配置多個(gè) location 塊。
這塊的主要作用是基于 Nginx? 服務(wù)器接收到的請(qǐng)求字符串(例如 server_name/uri-string),對(duì)虛擬主機(jī)名稱(也可以是IP別名)之外的字符串(例如 前面的 /uri-string)進(jìn)行匹配,對(duì)特定的請(qǐng)求進(jìn)行處理。地址定向、數(shù)據(jù)緩存和應(yīng)答控制等功能,還有許多第三方模塊的配置也在這里進(jìn)行。
?
作者:YSOcean
出處:http://www.cnblogs.com/ysocean/
總結(jié)
以上是生活随笔為你收集整理的Nginx(二)------nginx.conf 配置文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 《你必须知道的495个C语言问题》知识笔
- 下一篇: Nginx(四)------nginx