在此贴上本人对nginx配置文件的学习理解,才疏学浅,如有错误还望各位指正。
nginx.conf的内容分为几段:
- main配置段:全局配置段。其中main配置段中可能包含event配置段
- event{}:定义event模型工作特性;
- http{}:定义http协议相关配置
#user nobody; 以哪个用户启动nginx,默认nobody用户
worker_processes 1; # 启动多少个worker进程,建议和CPU核心数量一致
# 为什么建议worker_processes的值和CPU核心数量一致?
# 尽量让worker进程同时在不同的CPU里运行,不载就绪队列里排队
#error_log logs/error.log; 错误日志的路径(安装路径下的logs,yum安装的nginx路径在/usr/local/nginx/logs/)和名字
#error_log logs/error.log notice; 记录notice以上级别的日志
#error_log logs/error.log info; 记录info以上级别的日志
pid logs/nginx.pid; # 存放nginx的master进程的pid
events {
worker_connections 1024; # 最大连接数是1024,限定一个worker进程同时最多处理1024个请求
# 如果worker_processes参数的值是2,则可同时处理2048个请求,以此类推
}
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"';
# 定义了日志记录的格式,记录哪些内容
# $remote_addr 访问nginx的客户机的ip地址
# $request 用户访问的页面地址
# $status 返回状态码 200 404
# $body_bytes_sent 文件的大小
# http_user_agent 客户机使用的浏览器和系统
access_log logs/access.log main; # 指定访问日志的路径和格式,使用日志格式main
sendfile on; # 是否允许sendfile方式传输文件
#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; # 将网页根目录路由到html目录
index index.html index.htm; # 指定访问根目录时打开的页面为index.html或index.htm,优先级从左到右
}
#error_page 404 /404.html; 404错误显示的页面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; # 500 502 503 504错误时,显示的页面
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文件,可以使用nginx -t
检查配置文件的语法是否有错误。
[root@Charramma ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@Charramma ~]#
如此,配置文件语法无误。