Nginx:配置文件
跳到导航
跳到搜索
关于
nginx本身作为一个完成度非常高的负载均衡框架,和很多成熟的开源框架一样,大多数功能都可以通过修改配置文件来完成,使用者只需要简单修改一下 nginx 配置文件,便可以非常轻松的实现比如反向代理,负载均衡这些常用的功能。
nginx 配置文件默认都放在 nginx 安装路径下的 conf 目录:
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
通常将 nginx 配置文件分为三大块:
- 全局块:
- 设置一些影响 nginx 服务器整体运行的配置指令。主要包括:配置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型以及配置文件的引入等。
user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf;
- events块:
- 主要影响 Nginx 服务器与用户的网络连接。常用的设置包括:是否开启对多 work process 下的网络连接进行序列化,是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 word process 可以同时支持的最大连接数等。
events { worker_connections 1024; }
- http块:
- http 全局块配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。
http { server { } }
- http是一个大块,里面也可以包括很多小块,比如:
- “http全局块”:
- “server块”:相当于一个虚拟主机,一个http块可以拥有多个“server块”。
- server块又包括:
- “全局server块”:包括了本虚拟机主机的监听配置,和本虚拟主机的名称或 IP 配置;
- “location块”:用来对虚拟主机名称之外的字符串进行匹配,对特定的请求进行处理。包括:地址定向、数据缓存和应答控制等功能,还有许多第三方模块的配置也在这里进行。