nginx 默認(rèn)配置文件
1. 結(jié)構(gòu)
?在 nginx 的配置文件中倦逐,大概分為幾個(gè)區(qū)域:events {}
荷并、http {}
硝全、和沒(méi)有被 {}
包裹的區(qū)域氢烘。而 http {}
中還有 server {}
堵第,以及 server {}
中的 location {}
弄诲。結(jié)構(gòu)如下:
...
worker_processes 1;
events {
worker_connections 1024;
}
http {
...
server {
...
location {
...
}
}
server {
...
}
}
沒(méi)有被
{}
包裹的部分為全局配置包颁,如worker_processes 1;
設(shè)置工作進(jìn)程(子進(jìn)程)數(shù)為 1events {}
為 nginx 連接配置的模塊铆帽,如worker_connections 1024;
設(shè)置每一個(gè)子進(jìn)程最大允許連接 1024 個(gè)連接http {}
為 nginx http 核心配置模塊server {}
為虛擬主機(jī)配置模塊啡彬,包括監(jiān)聽(tīng)端口羹与、監(jiān)聽(tīng)域名等location {}
URI 匹配
2. 配置文件分析
?nginx 安裝完成之后會(huì)在 /usr/local/nginx/conf/ 目錄下生成一個(gè)配置文件 nginx.conf,也就是默認(rèn)使用的配置文件故硅。內(nèi)容如下(注意配置文件中"#"用于注釋):
# user 指定運(yùn)行 nginx 的用戶(hù)和組(第一個(gè)參數(shù)為用戶(hù)第二個(gè)為組,這里只有用戶(hù))
#user nobody;
# 指定工作進(jìn)程數(shù)(一般設(shè)置為CPU核數(shù))
worker_processes 1;
# 指定錯(cuò)誤日志為 logs/ 目錄下的 error.log 文件
#error_log logs/error.log;
# 指定錯(cuò)誤日志纵搁,并指定寫(xiě)入格式為 notice
#error_log logs/error.log notice;
# 指定錯(cuò)誤日志吃衅,并指定寫(xiě)入格式為 info
#error_log logs/error.log info;
# 指定 pid 文件(存放主進(jìn)程 pid 號(hào))
#pid logs/nginx.pid;
# nginx 連接配置模塊
events {
# 指定每個(gè)工作進(jìn)程最大連接數(shù)為 1024
worker_connections 1024;
}
# http 配置模塊
http {
# 通過(guò) include 加載 mime.types 文件,里面的 types {} 模塊將文件擴(kuò)展名映射到響應(yīng)的 MIME 類(lèi)型
include mime.types;
# 定義響應(yīng)的默認(rèn) MIME 類(lèi)型
default_type application/octet-stream;
# 寫(xiě)入格式 main 的內(nèi)容格式如下
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
# 指定訪(fǎng)問(wèn)日志和寫(xiě)入格式為 main
#access_log logs/access.log main;
# 啟用或者禁用 sendfile()
sendfile on;
# 啟用或者禁用使用套接字選項(xiàng)(僅在 sendfile 使用時(shí)使用)
#tcp_nopush on;
# 0 值禁用保持活動(dòng)的客戶(hù)端連接
#keepalive_timeout 0;
# 65 s 超時(shí)
keepalive_timeout 65;
# 啟用或者禁用 gzip
#gzip on;
# 虛擬主機(jī)配置模塊
server {
# 監(jiān)聽(tīng) 80 端口
listen 80;
# 監(jiān)聽(tīng)域名為 localhost
server_name localhost;
# 將指定的 charset 添加到 “Content-Type” 響應(yīng)頭字段腾誉。如果此charset與source_charset指令中指定的charset不同徘层,則執(zhí)行轉(zhuǎn)換。
#charset koi8-r;
# 指定該虛擬主機(jī)的訪(fǎng)問(wèn)日志
#access_log logs/host.access.log main;
# 將特定的文件或目錄重新定位利职,如 php 文件趣效,image 目錄等
location / {
# 設(shè)置請(qǐng)求的根目錄
root html;
# 定義索引,按順序匹配
index index.html index.htm;
}
# 定義顯示 404 錯(cuò)誤的 uri
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
# location 精準(zhǔn)匹配 '/50x.html'
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
# 正則表達(dá)式匹配 php 文件
#location ~ \.php$ {
# 設(shè)置代理服務(wù)器的協(xié)議和地址猪贪,以及應(yīng)該映射位置的可選URI跷敬。作為協(xié)議,可以指定“http”或“https”热押。該地址可以指定為一個(gè)域名或IP地址西傀,以及一個(gè)可選端口
# 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;
# 設(shè)置 FastCGI 服務(wù)器的地址。地址可以指定為一個(gè)域名或 IP 地址桶癣,以及一個(gè)端口
# fastcgi_pass 127.0.0.1:9000;
# 設(shè)置將在以斜杠結(jié)尾的URI之后追加的文件名拥褂,
# fastcgi_index index.php;
# 設(shè)置一個(gè)應(yīng)該傳遞給FastCGI服務(wù)器的參數(shù)。
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# 加載 conf/fastcgi_params 文件
# 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
#
# ssl 配置鬼廓,要啟用 ssl 模塊需要在編譯 nginx 時(shí)加上 --with-http_ssl_module 參數(shù)
#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 虛擬主機(jī)