前言
本博客中的Nginx配置文件版本为1.9.5。
nginx -v
nginx version: nginx/1.9.5
配置文件说明
首先要知道,nginx.conf文件分为三部分,分别是全局配置,事件配置和HTTP配置三大部分,以;表示结束,#表示注释。
#user nobody; #运行用户
worker_processes 1; #工作进程,建议按照CPU核数指定,一般为核数的二倍
#error_log logs/error.log; #全局错误日志存放路径
#error_log logs/error.log notice; #日志级别,从小到大依次为:debug,info,notice,warn,error,crit,emerg
#error_log logs/error.log info; #此为同一配置,意思同上,建议设置为notice或warn
#pid logs/nginx.pid; #nginx的pid文件存放路径
events { #事件配置
worker_connections 1024; 单个work process进程的最大并发连接数
}
http { #http配置
include mime.types; #嵌入其他配置文件
default_type application/octet-stream; #nginx默认文件类型
#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; #与sendfile同步开关,确保数据包装满数据,避免网络堵塞
#keepalive_timeout 0; #连接保持时间
keepalive_timeout 65;
#gzip on; #是否开启gzip压缩
server { #web服务配置
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; #4XX错误网页路径
# redirect server error pages to the static page /50x.html
#将服务器错误页面重定向到静态页面/50x.html
error_page 500 502 503 504 /50x.html; #5XX错误网页路径
location = /50x.html { #匹配路径
root html; #网页根目录
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#代理PHP脚本到侦听127.0.0.1:80的Apache
#location ~ \.php$ { #匹配规则及路径
# proxy_pass http://127.0.0.1; #反向代理地址
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#将PHP脚本传递到正在监听127.0.0.1:9000的FastCGI服务器
#location ~ \.php$ { #匹配规则及路径
# root html; #网页根目录
# fastcgi_pass 127.0.0.1:9000; #fastcgi地址
# fastcgi_index index.php; #fastcgi默认网页
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; #设置fastcgi请求中的参数
# include fastcgi_params; #嵌入其他配置文件
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#如果Apache的根目录与nginx的根目录一致,拒绝对.htaccess文件的访问
#location ~ /\.ht { #匹配规则及路径
# deny all; #拒绝全部连接
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#另一种虚拟主机,混合使用基于IP、名称和端口的配置
#server { #web服务配置
# listen 8000; #服务端口
# listen somename:8080; #域名及端口
# server_name somename alias another.alias; #服务器别名
# location / { #匹配规则及路径
# root html; #网页根目录
# index index.html index.htm; #默认网页
# }
#}
# HTTPS server
#HTTPS是以安全为目标的 HTTP 通道,在HTTP的基础上通过传输加密和身份认证保证了传输过程的安全性
#server {
# listen 443 ssl; #https默认端口
# server_name localhost; #域名
# ssl_certificate cert.pem; #证书名
# ssl_certificate_key cert.key; #密码
# ssl_session_cache shared:SSL:1m; #session缓存
# ssl_session_timeout 5m; #session有效期
# ssl_ciphers HIGH:!aNULL:!MD5; #加密算法
# ssl_prefer_server_ciphers on; #服务器端选择算法
# location / { #匹配规则及路径
# root html; #网页根目录
# index index.html index.htm; #默认网页
# }
#}
}
基础配置文件修改
对于刚安装好的Nginx来说,其配置文件处于初始状态,我们需要在此基础上进行修改优化。
注:以下仅为安装时的修改结果,并非nginx优化结果
user www;
worker_processes 4;
worker_rlimit_nofile 102400; #nginx最大文件打开数,与ulimit -n命令的值保持一致
error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll; #使用epoll模型
worker_connections 4096; #每个进程的最大连接数
}
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 utf-8; #网页字符集
access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}