Nginx—nginx.conf 配置结构详解
一、nginx.conf 配置结构
函数 | 说明 |
main | 全局配置 |
event | 配置工作模式以及连接数 |
http | http模块相关配置 |
server | 虚拟主机配置,可以有多个 |
location | 路由规则,表达式 |
upstream | 集群、内网服务器(负载均衡也在这里边配) |
二、Nginx配置语法
基本的语法:
指令集组成:每个指令单独写一行,每个指令分号 ";" 分开,每个指令块用大括号 "{ ... }" 分开,大括号的后方没有分号。注释用#号分开。
$符号:$符号为nginx内部提供的一些参数变量。
三、nginx.conf 核心配置文件详解
函数 | 说明 |
main | 全局配置 |
event | 配置工作模式以及连接数 |
http | http模块相关配置 |
server | 虚拟主机配置,可以有多个 |
location | 路由规则,表达式 |
upstream | 集群、内网服务器(负载均衡也在这里边配) |
主配置文件详解
#user nobody; #表示当系统在执行worker进程的时候由哪个用户去执行,(默认为nobody)
worker_processes 10; #逻辑CPU的个数设置的值为:(n-1)
# nginx的日志级别:debug info notice warn error crit 等级逐渐升高。
#error_log logs/error.log; #错误的日志,在编译的时候已经设置相关的路径。
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
#默认使用epoll
use epoll;
#每个worker允许的客端最大连接数
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 8080;
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;
# }
#}
}
(一)main 全局配置模块
1、进程用户设置
user root;
worker_processes 10;
worker_rlimit_nofile 65535;
user root;
- 这一配置项指定了 Nginx 工作进程所使用的用户身份。
root
是系统中的超级用户,拥有最高权限。不过,从安全角度考虑,不建议让 Nginx 以root
用户身份运行,因为这会使 Nginx 拥有过高的权限,一旦出现安全漏洞,攻击者可能会获取系统的最高控制权。通常,建议创建一个专门的低权限用户来运行 Nginx。worker_processes 4;
- 此配置项用于设置 Nginx 工作进程的数量。Nginx 采用多进程模型,一个主进程(master process)负责管理多个工作进程(worker processes),工作进程负责处理实际的客户端请求。
4
代表创建 4 个工作进程。一般而言,可以根据服务器的 CPU 核心数来设置该值,通常设置为 CPU 核心数或者核心数的两倍,这样能充分利用服务器的 CPU 资源。worker_rlimit_nofile 65535;
- 该配置项设定了每个 Nginx 工作进程能够打开的最大文件描述符数量。在 Linux 系统里,一切皆文件,包括网络连接、磁盘文件等,每个打开的文件或者连接都会占用一个文件描述符。
65535
意味着每个工作进程最多可以同时打开 65535 个文件描述符。当服务器需要处理大量并发连接时,就需要增大这个值,防止出现 “too many open files” 的错误。
2、 nginx日志路径设置
#error_log logs/error.log; #错误的日志,在编译的时候已经设置相关的路径放:/var/log/nginx/
#error_log logs/error.log notice;
#error_log logs/error.log info;
3、存放pid的地方
#pid logs/nginx.pid; #进程号存在的路径,在编译的时候已经设置相关的路径放:/var/run/nginx/
(二)、events配置工作模式以及连接数
events {
#默认使用epoll
use epoll;
#每个worker允许客端连接的最大连接数,根据硬件的配置来选值的大小。
worker_connections 1024;
}
(三)、http相关网络传输的模块(包含了很多的配置内容)
http {
include mime.types; #导入外部的文件,文件中为指令块,当前目录的mime.types文件。
default_type application/octet-stream; #默认的type类型。
*********************************************日志模块分析**********************************************************
#log_format main '$remote_addr - $remote_user [$time_local] "$request" ' #access_log 日志的格式,可以自定义格式。
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
***参数注解区****
# $remote_addr 客户端的IP地址
# $remote_user 用户名称,可以是 "-"
# [$time_local] 访问时间
# $request 请求的内容包括:URL 请求的方法GET、POST
# $status 响应的状态码
# $body_bytes_sent 客户端发送的文件主体所包含内容的字节数
# $http_referer 记录着用户从哪个访问链接跳转过来的,我们在做日志分析的时候会用到。
# $http_user_agent 用户的代理
# $http_x_forwarded_for 可以记录客户端的IP
****************
******************************************************************************************************************
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8080;
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;
# }
#}
}
mime.types文件
3.1、日志格式
#log_format main '$remote_addr - $remote_user [$time_local] "$request" ' #access_log 日志的格式,可以自定义格式。
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
***参数注解区****
# $remote_addr 客户端的IP地址
# $remote_user 用户名称,可以是 "-"
# [$time_local] 访问时间
# $request 请求的内容包括:URL 请求的方法GET、POST
# $status 响应的状态码
# $body_bytes_sent 客户端发送的文件主体所包含内容的字节数
# $http_referer 记录着用户从哪个访问链接跳转过来的,我们在做日志分析的时候会用到。
# $http_user_agent 用户的代理
# $http_x_forwarded_for 可以记录客户端的IP
3.2、文件的高效传输
sendfile on; #打开,表示文件传输的性能会得到提升,nginx的性能也得到相应的提升。
#tcp_nopush on; #和sendfile一起使用,表示当我们的数据包累积到一定的大小之后再发送,可以提高传输的效率。先取数据在进行统一分发。
3.3、客户端连接服务器的超时时间(传输完成后保持的时间)
keepalive_timeout 65; #以秒为单位,http有keepalive机制,当数据传输完成之后会保持一定时间的连接处于打开状态,如果客户端有新的请求会用此连接去处理。不用创建新的连接,节省资源的开销。
3.4、gzip压缩
#gzip on; #内容的传输经过压缩之后体积变小,提升的传输速率,减少了带宽的产生,但是在压缩的过程中会消耗我们系统上CPU的性能。
3.5、server模块,虚拟主机相关配置
server {
listen 8080; #服务端口号
server_name localhost; #服务IP、域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / { #配置页面显示的路由: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;
# }
#}
}
3.5.1 在nginx.conf文件中添加新的server模块。
server {
listen 8888; #指定的服务端口为8888
server_name 127.0.0.1; #指定的服务器的名称是127.0.0.1
location / {
root html;
index test.html index.htm; #访问到的内容为test.html文件
}
3.5.2 添加test.html文件:/usr/local/nginx/html/test.html
3.6、通过include函数的调用server模块的配置,提高文件的可读性。
3.6.1 在nginx.conf文件中定义include调用server模块(支持正则匹配)
可用统一将配置文件放在/usr/local/nginx/conf/conf.d指定的路径下这样方便管理,如:
- HTTP相关的配置放在:/usr/local/nginx/conf/conf.d/http 目录下
- TCP相关的配置放在:/usr/local/nginx/conf/conf.d/tcp 目录下
user root;
worker_processes 4;
worker_rlimit_nofile 65535;
events {
...
}
include conf.d/tcp/*.conf; #TCP相关配置(不能放在下边的HTTP模块中不然会报错);这里的include是指包含/usr/local/nginx/conf/conf.d/tcp路径下所有的.conf。
http {
...
include conf.d/http/*.conf; #HTTP相关配置(需要放在HTTP模块中不然会报错);这里的include是指包含/usr/local/nginx/conf/conf.d/http 路径下所有的.conf
}