Nginx主配置文件nginx.conf
Nginx主配置文件nginx.conf是一个纯文本类型的文件(其他配置文件大多也是如此),它位于Nginx安装目录下的conf目录,整个配置文件是以区块的形式组织的。一般,每个区块以一个大括号“{}”来表示,区块可以分为几个层次,整个配置文件中Main区位于最上层,在Main区下面可以有Events区,HTTP区等层级,在HTTP区中又包含有一个或多个Server区,每个Server区中又可有一个或多个location区,整个Nginx配置文件nginx.conf的主体框架为:
[root@chensiqi conf]# egrep -v "#|^$" nginx.conf #去掉包含#号和空行的内容
worker_processes 1; #worker进程的数量
error_log logs/error.log; #错误日志(默认没开)
pid logs/nginx.pid; #进程号(默认没开)
events { #事件区块开始
worker_connections 1024; #每个worker进程支持的最大连接数
} #事件区块结束
http { #http区块开始
include mime.types; #Nginx支持的媒体类型库文件包含
default_type application/octet-stream; #默认的媒体类型
sendfile on; #开启高效传输模式
keepalive_timeout 65; #连接超时。
server { #网站配置区域(第一个server第一个虚拟主机站点)
listen 80; #提供服务的端口,默认80
server_name www.chensiqi.org; #提供服务的域名主机名
location / { #第一个Location区块开始
root html; #站点的根目录(相对于nginx安装路径)
index index.html index.htm; #默认的首页文件,多个用空格分开
}
error_page 500 502 503 504 /50x.html; #出现对应的http状态码时,使用50x.html回应客户
location = /50x.html { #Location区块开始,访问50x.html
root html; #指定对应的站点目录为html
}
}
server { #网站配置区域(第二个server第二个虚拟主机站点)
listen 80; #提供服务的端口,默认80
server_name bbs.chensiqi.org; #提供服务的域名主机名
location / { #服务区块
root html; #相对路径(nginx安装路径)
index index.html index.htm;
}
location = /50x.html { #发生错误访问的页面
root html;
}
}
}
整个nginx配置文件的核心框架如下:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
}
Nginx其他配置文件
- 如果是配合动态服务(例如PHP服务),Nginx软件还会用到扩展的fastcgi相关配置文件,这个配置是通过在Nginx.conf主配置文件中嵌入include命令来实现的,不过默认情况是注释状态,不会生效。
- fastcgi.conf配置文件的初始内容如下:
[root@localhost conf]# cat fastcgi.conf
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
• fastcgi_params 默认配置文件的内容如下:
[root@localhost conf]# cat fastcgi_params
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
上述未做注释的目录或文件是比较少用的,有关动态扩展配置后文讲到PHP服务时再来讲解。
规范化Nginx配置文件
下面是优化后的Nginx配置的实战方案
cat /etc/nginx/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include conf.d/*.conf; #虚拟网站配置信息统一放在了当前的conf.d目录下
}
[root@localhost nginx]# cat conf.d/www.conf
server {
listen 80;
server_name www.yunjisuan.com;
location / {
root /var/www/html/wwwcom;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
}