深入UnderstandNginxconfigurationfilestructure and basicconfiguration指令, MasterNginx configurationtechniques
Nginx configurationfileadoptsmodule化structure, 由 many 个不同层次 configuration块组成. 主configurationfile通常名 for nginx.conf, 位于以 under 位置:
/etc/nginx/nginx.conf/etc/nginx/nginx.confnginxinstallationTable of Contents/conf/nginx.conf影响Nginx整体run configuration指令
影响Nginx and user交互 configuration指令
HTTPserver相关 configuration指令
虚拟主机相关 configuration指令
URLpath匹配相关 configuration指令
# 全局块
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
# events块
events {
worker_connections 1024;
}
# http块
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
# server块
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html index.htm;
# location块
location / {
try_files $uri $uri/ =404;
}
}
}
全局块package含影响Nginx整体run configuration指令, 通常位于configurationfile 最 on 方.
指定Nginx工作process runuser
user nginx;
指定Nginx 工作process数量, 通常设置 for CPUcore数 or auto
worker_processes auto;
指定errorlog 位置 and 级别
error_log /var/log/nginx/error.log warn;
指定Nginx主processPIDfile 位置
pid /var/run/nginx.pid;
events块package含影响Nginx and user交互 configuration指令, 主要涉及network连接相关 设置.
指定每个工作process 最 big connections
events {
worker_connections 1024;
}
指定Nginxusing eventmodel
events {
use epoll;
worker_connections 1024;
}
http块package含HTTPserver相关 configuration指令, is Nginxconfigurationin最 complex and important 部分.
package含otherconfigurationfile
http {
include /etc/nginx/mime.types;
}
指定默认 MIMEclass型
http {
default_type application/octet-stream;
}
定义log格式
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
}
指定访问log 位置 and 格式
http {
access_log /var/log/nginx/access.log main;
}
启用 or 禁用sendfilemechanism
http {
sendfile on;
}
指定keepalive连接 超时时间
http {
keepalive_timeout 65;
}
server块package含虚拟主机相关 configuration指令, 用于定义不同 网站 or application.
指定虚拟主机监听 端口
server {
listen 80;
}
指定虚拟主机 域名
server {
server_name example.com www.example.com;
}
指定网站file 根Table of Contents
server {
root /var/www/example.com;
}
指定默认 indexfile
server {
index index.html index.htm;
}
location块package含URLpath匹配相关 configuration指令, 用于processing不同path request.
指定URLpath匹配规则
location / {
try_files $uri $uri/ =404;
}
location /images/ {
alias /var/www/images/;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
以 under is a 完整 Nginxconfigurationexample, 用于托管静态网站:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html index.htm;
# 访问logconfiguration
access_log /var/log/nginx/example.com.access.log main;
error_log /var/log/nginx/example.com.error.log warn;
# 静态filecacheconfiguration
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires 30d;
add_header Cache-Control "public, max-age=2592000";
}
# 主pathconfiguration
location / {
try_files $uri $uri/ =404;
}
# 404页面configuration
error_page 404 /404.html;
location = /404.html {
internal;
}
# 50xerror页面configuration
error_page 500 502 503 504 /50x.html;
location = /50x.html {
internal;
}
}
这个configurationpackage含了静态网站托管 basic要素, including:
for 了更 good 地managementNginxconfiguration, 可以将不同 configuration分离 to 单独 filein:
# 主configurationfile /etc/nginx/nginx.conf
http {
include /etc/nginx/conf.d/*.conf;
}
然 after in /etc/nginx/conf.d/Table of Contentsincreation各个网站 configurationfile, such asexample.com.conf.
in applicationconfiguration之 before , 应该checkconfigurationfile 语法 is 否正确:
nginx -t
modifyconfigurationfile after , 需要重 new 加载configuration:
systemctl reload nginx
1. Nginxconfigurationfile basicstructureincluding哪些部分? ( many 选)
2. 以 under 哪个指令用于指定Nginx 工作process数量?
3. 实践练习: creation一个basic Nginxconfigurationfile
请creation一个Nginxconfigurationfile, 用于托管一个静态网站, 要求:
through本tutorial, 您已经Learning了Nginxconfigurationfile structure and basicconfiguration指令. Nginx configurationsystem具 has 以 under 特点:
in after 续 tutorialin, 我们将深入LearningNginx 虚拟主机configuration, 反向proxy, load balancing, 静态fileservice, securityconfiguration, performanceoptimizationetc.advanced features, helping您全面MasterNginx usingtechniques.