nginx安裝后默認的配置文件為/usr/local/nginx/conf/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; # } #} }
nginx.conf配置文件由三部分組成
1、全局塊
從配置文件開始到events之間的內容,主要會設置一些影響nginx服務整體運行的配置指令,包括運行nginx服務的用戶/組、允許生成的worker process數、進程PID存放路徑、日志存放路徑和類型、配置文件的引入等。
比方上述配置文件中的worker_processes 1,表示nginx並發處理的值,值越大表示並發處理的數越多。
2、enents塊
主要影響nginx服務與用戶的網絡連接,常用的設置包括是否開啟對多worker process下的網絡連接進行序列化、是否允許同時接收多個網絡連接、選取哪種事件驅動模型來處理連接請求、每個worker process同時支持的最大連接數等。
比如上述配置文件中的worker_connections 1024,表示每個worker process支持的最大連接數為1024。
3、http塊
nginx服務配置中最頻繁修改的部分,代理、緩存、日志定義等絕大部分功能和第三方模塊的配置都在這部分,包括http全局塊和server塊。
3.1、http全局塊
包括文件引入、MIME-TYPE定義、日志自定義、連接超時時間、單鏈接請求數上限等。
3.2、server塊
server塊與虛擬主機有密切關系,每個http塊可以包括多個server塊,而每個server塊就相當於一個虛擬主機,server塊也分為全局server塊和location塊。
3.2.1、server全局塊
最常見的是配置本虛擬機主機的監聽配置和本虛擬主機的名稱或ip配置。
3.2.2、location塊
一個server塊可以配置多個location塊。主要作用是基於nginx服務接收到的請求字符串(例如server_name/uri-string),對虛擬主機名稱(也可以是ip別名)之外的字符串(liru例如前面的/uri-string)進行匹配,對特定的請求進行處理。地址定向、數據緩存、應答控制、第三方模塊配置等都在這里完成。
即配置文件結構如下
全局塊 events { events塊; } http { http全局塊; server { sever全局塊; location / { location塊; } location ……{ location塊; } } server { } }