三、Nginx配置檔案說明
一、配置檔案路徑
/usr/local/nginx/conf
二、配置檔案
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } 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; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #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; } # 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的配置檔案由三部分組成
第一部分:全域性塊
從配置檔案開始到events塊之間的內容,主要會設定一些影響nginx伺服器整體執行的配置指令。主要包括配置執行nginx伺服器的使用者(組)、允許生產的worker process數,程序PID存放路徑、日誌存放路徑和型別以及配置檔案的引入等。
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid;
worker_processes 1; 表示nginx伺服器併發處理服務的關鍵配置,worker_process值越大,可以支援的併發處理量也越多,但是會受到硬體,軟體等裝置的制約。
第二部分:events塊
events {
worker_connections 1024;
}
events塊設計的指令主要影響Nginx伺服器與使用者的網路連線,常用的設定包括是否開啟對多work process下的網路連線進行序列化,是否允許同時接收多個網路連線,選取哪種事件驅動模型來處理連線請求,每個word process可以同時支援的最大連線數等。
上面標示每個word process支援的最大連線數為1024。
events部分的配置對nginx的效能影響較大,在實際中應該靈活配置。
第三部分: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;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#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;
}
# 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;
# }
#}
}
nginx伺服器配置中最頻繁的部分,代理,快取和日誌定義等絕大多數功能和第三方模組的配置都在這裡。
需要注意的是:http塊也可以包括http全域性塊、server塊。
1、http全域性塊
http全域性塊配置的指令包括檔案引入、MIME-TYPE定義、日誌自定義、連線超時時間、單鏈接請求數上限等。
2、server塊
這塊和虛擬主機有密切關係,虛擬主機從使用者角度看,和一臺獨立的硬體主機是完全一樣,該技術的產生是為了節省網際網路伺服器硬體成本。
每個http塊可以包括多個server塊,而每個server塊就相當於一個虛擬主機。
每個server塊也分為全域性server塊,以及可以同時包含多個location塊。
1、全域性server塊
最常見的配置是本虛擬機器主機的監聽配置和本虛擬主機的名稱或IP配置
2、location塊
一個server塊可以配置多個location塊
這塊的主要作用是基於nginx伺服器接收到的請求字串(例如:server_name/uri-string),對虛擬主機名稱(也可以是IP別名)之外的字串(例如:前面的/uri-string)進行匹配,對特定的請求進行處理。地址定向、資料快取和應答控制等功能,還有許多第三方模組的配置也在這裡進行。