配置文件之http块
http {
include mime.types;
default_type application/octet-stream;
access_log logs/access.log;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.html;
}
}
}定义 MIME-Type
在常用的浏览器中,可以显示的内容有 HTML、XML、GIF 及 Flash 等种类繁多的文本、媒体等资源,浏览器为区分这些资源,需要使用 MIME Type。换言之,MIME Type 是网络资源的媒体类型。Nginx 服务器作为 Web 服务器,必须能够识别前端请求的资源类型。
include mime.types;
default_type application/octet-stream;自定义服务日志
记录 Nginx 服务器提供服务过程应答前端请求的日志,我们将其称为服务日志以示区分。 Nginx 服务器支持对服务日志的格式、大小、输出等进行配置,需要使用两个指令,分别是 access_log 和 log_format 指令。
access_log logs/access.log;
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 与 $http_x_forwarded_for 用以记录客户端的 ip 地址;
$remote_user:用来记录客户端用户名称;
$time_local: 用来记录访问时间与时区;
$request: 用来记录请求的 url 与 http 协议;
$status: 用来记录请求状态;成功是 200,
$body_bytes_sent :记录发送给客户端文件主体内容大小;
$http_referer:用来记录从那个页面链接访问过来的;
$http_user_agent:记录客户浏览器的相关信息;
通常 web 服务器放在反向代理的后面,这样就不能获取到客户的 IP 地址了,通过$remote_add 拿到的 IP 地址是反向代理服务器的 iP 地址。反向代理服务器在转发请求的 http 头信息中,可以增加 x_forwarded_for 信息,用以记录原有客户端的 IP 地址和原来客户端的请求的服务器地址。
配置连接超时时间
与用户建立会话连接后,Nginx 服务器可以保持这些连接打开一段时间,指令 keepalive_timeout 就是用来设置此时间的。
keepalive_timeout 65;提示
此指令还可以出现在 server 块和 location 块中。
配置虚拟主机
server{} 包含在http{}内部,每一个server{}都是一个虚拟主机(站点)
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}配置网络监听
listen 80;基于名称虚拟主机配置
这里的“主机”,就是指此 server 块对外提供的虚拟主机。设置了主机的名称并配置好 DNS,用户就可以使用这个名称向此虚拟主机发送请求了。
语法: server_name name1 name2 name3 ...;
server_name localhost jiameikj.com;
server_name www.baidu.com; //精确匹配
server_name *.baidu.com; //通配至此,本章节的学习就到此结束了,如有疑惑,可对接技术客服进行相关咨询。