Nginxconfigurationfiletutorial

深入UnderstandNginxconfigurationfilestructure and basicconfiguration指令, MasterNginx configurationtechniques

configurationfilestructureoverview

Nginx configurationfileadoptsmodule化structure, 由 many 个不同层次 configuration块组成. 主configurationfile通常名 for nginx.conf, 位于以 under 位置:

configurationfile basicstructure

1. 全局块

影响Nginx整体run configuration指令

2. events块

影响Nginx and user交互 configuration指令

3. http块

HTTPserver相关 configuration指令

3.1 server块

虚拟主机相关 configuration指令

3.1.1 location块

URLpath匹配相关 configuration指令

configurationfileexample

# 全局块
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;
        }
    }
}

全局块configuration

全局块package含影响Nginx整体run configuration指令, 通常位于configurationfile 最 on 方.

user

指定Nginx工作process runuser

user nginx;

worker_processes

指定Nginx 工作process数量, 通常设置 for CPUcore数 or auto

worker_processes auto;

error_log

指定errorlog 位置 and 级别

error_log /var/log/nginx/error.log warn;

pid

指定Nginx主processPIDfile 位置

pid /var/run/nginx.pid;

events块configuration

events块package含影响Nginx and user交互 configuration指令, 主要涉及network连接相关 设置.

worker_connections

指定每个工作process 最 big connections

events {
    worker_connections 1024;
}

use

指定Nginxusing eventmodel

events {
    use epoll;
    worker_connections 1024;
}

http块configuration

http块package含HTTPserver相关 configuration指令, is Nginxconfigurationin最 complex and important 部分.

basicHTTPconfiguration

include

package含otherconfigurationfile

http {
    include /etc/nginx/mime.types;
}

default_type

指定默认 MIMEclass型

http {
    default_type application/octet-stream;
}

log_format

定义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"';
}

access_log

指定访问log 位置 and 格式

http {
    access_log /var/log/nginx/access.log main;
}

sendfile

启用 or 禁用sendfilemechanism

http {
    sendfile on;
}

keepalive_timeout

指定keepalive连接 超时时间

http {
    keepalive_timeout 65;
}

server块configuration

server块package含虚拟主机相关 configuration指令, 用于定义不同 网站 or application.

listen

指定虚拟主机监听 端口

server {
    listen 80;
}

server_name

指定虚拟主机 域名

server {
    server_name example.com www.example.com;
}

root

指定网站file 根Table of Contents

server {
    root /var/www/example.com;
}

index

指定默认 indexfile

server {
    index index.html index.htm;
}

location块configuration

location块package含URLpath匹配相关 configuration指令, 用于processing不同path request.

location

指定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;
}

location匹配规则

实践case: configuration静态网站

以 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:

configurationfilemanagement

configurationfile 组织

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.

checkconfigurationfile语法

in applicationconfiguration之 before , 应该checkconfigurationfile 语法 is 否正确:

nginx -t

重 new 加载configuration

modifyconfigurationfile after , 需要重 new 加载configuration:

systemctl reload nginx

互动练习

1. Nginxconfigurationfile basicstructureincluding哪些部分? ( many 选)

A. 全局块
B. events块
C. http块
D. server块
E. location块

2. 以 under 哪个指令用于指定Nginx 工作process数量?

A. worker_processes
B. worker_connections
C. processes
D. connections

3. 实践练习: creation一个basic Nginxconfigurationfile

请creation一个Nginxconfigurationfile, 用于托管一个静态网站, 要求:

  • 监听80端口
  • 域名设置 for test.com
  • 网站根Table of Contents设置 for /var/www/test.com
  • 默认indexfile for index.html
  • configuration静态filecache (graph片, CSS, JSfilecache30天)

summarized and 展望

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.