Nginx(二)------nginx.conf 配置文件
上一篇博客我們將 nginx 安裝在 /usr/local/nginx 目錄下,其默認的配置文件都放在這個目錄的 conf 目錄下,而主配置文件 nginx.conf 也在其中,后續對 nginx 的使用基本上都是對此配置文件進行相應的修改,所以本篇博客我們先大致介紹一下該配置文件的結構。
回到頂部
1、nginx.conf 的主體結構
打開此文件,內容如下:
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 }# 開頭的表示注釋內容,我們去掉所有以 # 開頭的段落,精簡之后的內容如下:
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 }? 根據上述文件,我們可以很明顯的將 nginx.conf 配置文件分為三部分:
回到頂部
2、全局塊
從配置文件開始到 events 塊之間的內容,主要會設置一些影響nginx 服務器整體運行的配置指令,主要包括配置運行 Nginx 服務器的用戶(組)、允許生成的 worker process 數,進程 PID 存放路徑、日志存放路徑和類型以及配置文件的引入等。
比如上面第一行配置的:
worker_processes 1;這是 Nginx 服務器并發處理服務的關鍵配置,worker_processes 值越大,可以支持的并發處理量也越多,但是會受到硬件、軟件等設備的制約,這個后面會詳細介紹。
回到頂部
3、events 塊
比如上面的配置:
events {worker_connections 1024; }events 塊涉及的指令主要影響 Nginx 服務器與用戶的網絡連接,常用的設置包括是否開啟對多 work process 下的網絡連接進行序列化,是否允許同時接收多個網絡連接,選取哪種事件驅動模型來處理連接請求,每個 word process 可以同時支持的最大連接數等。
上述例子就表示每個 work process 支持的最大連接數為 1024.
這部分的配置對 Nginx 的性能影響較大,在實際中應該靈活配置。
回到頂部
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 服務器配置中最頻繁的部分,代理、緩存和日志定義等絕大多數功能和第三方模塊的配置都在這里。
需要注意的是:http 塊也可以包括?http全局塊、server 塊。
①、http 全局塊
http全局塊配置的指令包括文件引入、MIME-TYPE 定義、日志自定義、連接超時時間、單鏈接請求數上限等。
②、server 塊
這塊和虛擬主機有密切關系,虛擬主機從用戶角度看,和一臺獨立的硬件主機是完全一樣的,該技術的產生是為了節省互聯網服務器硬件成本。后面會詳細介紹虛擬主機的概念。
每個 http 塊可以包括多個 server 塊,而每個 server 塊就相當于一個虛擬主機。
而每個 server 塊也分為全局 server 塊,以及可以同時包含多個 locaton 塊。
1、全局 server 塊
最常見的配置是本虛擬機主機的監聽配置和本虛擬主機的名稱或IP配置。
2、location 塊
一個 server 塊可以配置多個 location 塊。
這塊的主要作用是基于 Nginx? 服務器接收到的請求字符串(例如 server_name/uri-string),對虛擬主機名稱(也可以是IP別名)之外的字符串(例如 前面的 /uri-string)進行匹配,對特定的請求進行處理。地址定向、數據緩存和應答控制等功能,還有許多第三方模塊的配置也在這里進行。
?
作者:YSOcean
出處:http://www.cnblogs.com/ysocean/
總結
以上是生活随笔為你收集整理的Nginx(二)------nginx.conf 配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《你必须知道的495个C语言问题》知识笔
- 下一篇: Nginx(四)------nginx