nginx.conf主要分为几大块组成
| Nginx组成 | |
| 全局块 | 全局块是默认配置文件从开始到events块之间的一部分内容,主要设置一些影响Nginx服务器整体运营的配置指令. |
| events块 | events块涉及的指令主要影响Nginx服务器与用户的网络连接 |
| http块 | http块是Nginx服务器配置中的重要部分,代理、缓存和日志定义等绝大多数的功能和第三方模块的配置’.http块中包含http全局块和多个server块 |
配置运行Nginx服务器用户(组)
user user[group];
user:指定可以运行Nginx服务器的用户
group 指定可以运行Nginx服务器的用户组
user nobody;表示所有用户(用户组)都可以运行Nginx服务
配置允许生成的worker process数
worker_processes number | auto;
number 指定Nginx最多可产生的worker process数
auto 设置Nginx进程自动检测可产生的worker process数
worker process 是Nginx服务器实现并发处理服务的关键。
配置Nginx进程PID存放路径
pid file;
file 指定存放路径和文件名,Nginx默认配置是将配置文件默认存放在Nginx安装目录的logs文件夹下,文件名为nginx.pid
配置错误日志的存放路径
error_log file
file为指定存放路径及文件名,Nginx默认配置是将配置文件默认存放在Nginx安装目录的logs文件夹下,文件名error_log
配置文件的引入
include file;
file:要引入的配置文件、支持相对路径,新引进来的文件需要要求运行Nginx进程的用户对其具有写权限,并且符合Nginx配置文件规定的相关语法和结构
配置最大连接数
worker_connections number;
number 为设置允许每一个worker process同时开启的最大连接数
定义MINME-TYPE
include mime.types
default_type application/octet-stream
自定义服务日志
access_log path[format[buffer=size]]
path 配置服务日志的文件存放的路径和名称
format 自定义服务日志的格式字符串,也可以通过“格式串的名称”使用log_format指令定义好格式
size 配置临时存放日志的内存缓存区大小
配置允许sendfile方式传输文件
sendfile on|off;
用于开启或者关闭使用sendfile()传输文件,默认为off.
sendfile_max_chunk size;
配置Nginx进程的每个worker process调用sendfile()传输数据的最大值,如果为0则无限制
配置连接超时时间
keepalive_timeout timeout[header_timeout];
timeout 服务器端对连接的保持时间,默认为75s;
header_timeout 在应答报文头中的Keep-Alive域设置超时时间
Keep-Alive timeout = header_timeout 报文中指令可以被Mozilla或者Konqueror识别
配置最大单连接请求数
keepalive_requests number;
number 为限制用户通过某一连接向Nginx服务器发送求最大次数,默认100
配置虚拟主机server块
server name可以有多个名称,也可以只有一个名称,如果为多个名称中间用空格进行分离
server_name srvername.com servername.cn
server name 还可以用通配符“*”进行表示,同配置只能用于匹配地址的首端或者尾端
server_name *.servername.com
servername 还可以使用正则表达式进行匹配,已“~”作为开始标记
server_name ~^www\d+\.servername\.com$;
针对不同匹配方式进行server name匹配的时候 匹配多个结果时按照以下顺序进行优先处理
- 准确匹配server_name
- 通配符在开始的时候匹配server_name成功
- 通配符在结尾时匹配server_name成功
- 正则表达式匹配server_name成功
在以上四种匹配方式中,如果server_name被同一优先级的匹配方式多次匹配成功时,首次匹配成功的虚拟主机进行处理
基于ip进行虚拟主机配置
nginx支持ip模式进行server服务匹配,也是在我们配置nginx负载均衡时比较常用的一种方式
server_name ipaddress;
配置location块
Nginx官方文档中所定义的Location语法结构
location [ = | ~ | ~* | ^~ ] uri {...}
uri:表示待匹配的请求字符串变量
“=”:用于标准uri前,要求请求字符串与Uri严格匹配,如果已经匹配成功,就停止继续向下搜索并立即处理此请求
“~”:用于表示Uri包含正则表达式,并且区分大小写
"~*":用于表示Uri包含正则表达式,并且不区分大小写
配置请求根目录
在server块中的location块下进行设置:其语法结构为 root path; path为Nginx服务器接收请求后查找资源的根目录
设置网站的默认首页
index指令用于设置网站默认首页,在server块中的location块下进行配置 其语法结构为 index file...;
nginx自带的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;
# }
#}
}
最近浏览



