nginx配置文件nginx.conf的结构、各个指令(元素)的含义以及用法
- 默认的nginx.conf
- nginx.conf配置文件官方解释
- 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配置文件官方解释
-
官方文档地址
http://nginx.org/en/docs/ -
进入
nginx.conf配置文件官方解释
图示:
原文如下:
nginx consists of modules which are controlled by directives specified in the configuration file.
Directives are divided into simple directives and block directives. A simple directive consists of the
name and parameters separated by spaces and ends with a semicolon (;). A block directive has the
same structure as a simple directive, but instead of the semicolon it ends with a set of additional
instructions surrounded by braces ({ and }). If a block directive can have other directives inside
braces, it is called a context (examples: events, http, server, and location).
Directives placed in the configuration file outside of any contexts are considered to be in the main
context. The events and http directives reside in the main context, server in http, and location in
server.
The rest of a line after the # sign is considered a comment.
简易翻译:
- nginx是由多个模块组成的,而这些模块又是由配置文件nginx.conf中的指定的指令所控制的。
- 指令可分为
简单指令
和块指令
。 - 简单指令的结构:
name
+空格
+参数值
+英文分号
。 形如这样,worker_processes 1;
- 块指令由一个或多个简单指令组成,用大括号{}包围起来,如上图。
块指令中可以包含其他块指令。
- 一个块指令我们也可称其为一个上下文
context
, 这样的上下文/块指令有:
events
http
server
location
- nginx.conf配置文件中在以上4个(events、http、server、location)上下文之外的配置指令的地方成为
main
上下文。 - 以上5个上下文的包含关系如下:
- 指令最前面的 井号 “#” 代表注释
nginx.conf配置文件中每一条指令或指令快的含义是什么,以及用法(使用范围:应该配置在什么地方)
见官网:
https://nginx.org/en/docs/
这里以指令proxy_pass
为例:
具体的用法如下图:
proxy_pass的用法还可参考,其他非官方文章:
Nginx反向代理location与proxy_pass配置规则总结