Nginx配置文件
Nginx配置文件时nginx.conf,由三部分组成,全局块,事件events块和http块,在http块中又包含http全局配置,多个server块和location块,简单的文件结构如下。
全局配置
events{
……
}
http {
http全局配置
server {
serer全局配置
……
location /{
……
}
}
}
全局块说明
简单来说,全局块的配置就是就是为整个Nginx提供服务,如下所示
#user nobody; 默认使用nobody这个用户去启动nginx
worker_processes 2; 工作进程的数量,建议和cpu的核心一致
#error_log logs/error.log; 日志
#error_log logs/error.log notice;日志级别
#error_log logs/error.log info;
#pid logs/nginx.pid; master进程的pid
日志级别
日志分为九个级别,严重级别从小到大为:
none<debug<info<notice<warning<error<critical<alert<emerg
none :不记录日志
debug:调试信息,系统进行调试时产生的日志,不属于错误日志,不需要过多关注。
info: -般的通知信息,用来反馈系统的当前状态给当前用户。
notice :提醒信息,需要检查一下程序了,不理会可能会出现错误。
warning:警告信息,当出现警告时,你的程序可能已经出现了问题,但不影响程序正常运行,尽快进行处理,以免导致服务宕掉。
error :错误信息,出现这一项时,已经挑明服务出现了问题,服务都无法确认是否能正常运行。
critical :比较严重的错误信息,服务已经宕了,可能已经无法修复。
alert :警报信息,需要立即采取行动,不仅是服务宕了,还会影响系统的正常后动。
事件块
events块主要是用来跟用户建立网络连接的,如果worker_processes为2的话,那么该网络最大的连接数就可以达到2048个。
events { #事件块,并发的数量
worker_connections 1024;
}
不过,这个值不能超过超过系统支持打开的最大文件数,也就是文件描述符。修改文件描述符大小方法如下。
- 临时修改
ulimit -n 65535
- 永久修改
vim /etc/security/limits.conf
#加入如下内容,重启系统生效
* hard nofile 65535
* soft nofile 65535
http块
http块是Nginx配置文件的重要部分,包括代理、日志格式、路由等模块都可以加载到http块中。
http { #http块,与http协议相关的配置
include mime.types; #加载媒体类型,如text、MP4、HTML等,然后返回给浏览器
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块
server块就相当于一台虚拟主机,拥有独立的资源和程序,可以独立地对外提供服务供用户访问,有三种类型的虚拟主机。
- 基于域名的虚拟主机,根据域名来区别不同的虚拟主机,常用于对外提供服务。
- 基于IP的虚拟主机,根据IP来区别不同的虚拟主机,不常用。
- 基于端口的虚拟主机,根据端口来区别不同的虚拟主机,常用于内部网站。
server {#一个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;
}
}
Nginx部署三个网站
简单实现在nginx中部署a、b、c三个网页,进入nginx.conf配置文件,添加server块。
server {
listen 80;
server_name www.a.com;
location / {
root html/a;
index a.html;
}
access_log logs/a.access.log main;
error_log logs/a.error.log notice;
}
server {
listen 80;
server_name www.b.com;
location / {
root html/b;
index b.html;
}
access_log logs/b.access.log main;
error_log logs/b.error.log notice;
}
server {
listen 80;
server_name www.c.com;
location / {
root html/c;
index c.html;
}
access_log logs/c.access.log main;
error_log logs/c.error.log notice;
}
在html目录下新建各个网页的根目录和首页文件。
[root@localhost html]# pwd
/usr/local/shengxia/html
[root@localhost html]# mkdir a b c
[root@localhost html]# cd a
[root@localhost a]# vim a.html
[root@localhost a]# cd ../b
[root@localhost b]# vim b.html
[root@localhost b]# cd ../c
[root@localhost c]# vim c.html
[root@localhost c]# nginx -s reload
linux访问
因为设置了域名,所以在Linux的/etc/hosts
文件添加域名解析。
[root@localhost c]# vim /etc/hosts
[root@localhost c]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
#填自己虚拟机的IP地址
192.168.40.133 www.a.com
192.168.40.133 www.b.com
192.168.40.133 www.c.com
在Linux做客户机访问,使用curl
命令,出现如下则网页部署成功。
[root@localhost c]# curl www.a.com
This is a web page!
[root@localhost c]# curl www.b.com
This is b web page!
[root@localhost c]# curl www.c.com
This is c web page!
windows访问
在Windows做客户机访问也需要修改文件,进入C:\Windows\System32\drivers\etc\hosts
目录进行修改,也是添加域名解析。
#添加对应的域名解析
192.168.40.133 www.a.com
192.168.40.133 www.b.com
192.168.40.133 www.c.com
Nginx常见错误码
404
404错误是找不到路径,通过以下演示错误。在server开启error_page,然后在html目录新建404.html。
<html>
<head>
<meta charset="utf-8" http-equiv="refresh" content="5;url=https://sheng_xiaxia.gitee.io/personal-blog">
<title>404 error</title>
</head>
<body>
<h1><p>you have met a error,please try again later!</p></h1>
<p><a href="https://sheng_xiaxia.gitee.io/personal-blog">看下我的博客吧</a></p>
<img src="error.jpg" width="400px"/>
</body>
</html>
403
403错误是Forbidden,一般是有路径但是路径里没有首页(index.html)导致的。
503
503错误主要是nginx内部服务限制了客户端的请求连接数或者并发连接,在http块中加入如下代码,然后在server块中使用限制要求,连续请求超过5次,就会出现503错误,如下。
http {
……
#限定访问次数,允许1s中访问一次,开辟一块空间获取用户的IP地址
limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s;
server {
……
limit_req zone=perip burst=5 nodelay;
}
}
502
502错误是因为出现了网关故障或者后端真正的服务器挂了,导致请求到达不了。比如,我这里部署一个负载均衡器,我把后端服务器web1
和web2
关闭,负载均衡器lb1
没有问题,访问lb1就会出现502错误。
#关闭web1
[root@web1 logs]# nginx -s stop
#关闭web2
[root@web2 logs]# nginx -s stop
504
504错误是网关超时,作为网关或者代理工作的服务器执行请求时,未能及时从后端服务器收到响应,通常是因为防火墙开启或者设置了防火墙规则导致代理服务器或者客户端未能收到响应。
其它状态后面再写