Nginx详解
Nginx与Apache一样,都是web服务器,但是Nginx比Apache多一些功能,比如Nginx可以做代理,可以做负载均衡……
1. Nginx关键特性
- 支持高并发
- 单机Nginx可支持十万级别的并发连接,经过优化后可支持百万级别并发
- 内存资源消耗低
- 在同级web中,Nginx占用的内存最少,一万非活跃的http长连接仅消耗2.5M内存
- 高扩展性
- 和Apache一样,Nginx采用模块化设计,并支持非常多丰富的第三方模块
- 高可靠性
- Nginx采用master-worker模式,如果worker出现故障,master可以快速开启一个新的worker来提供服务
2. Nginx配置
[root@ceph conf.d]# grep -Ev "^#|^$|#" /etc/nginx/nginx.conf |cat -n 1 user nginx; 2 worker_processes auto; 3 error_log /var/log/nginx/error.log; 4 pid /run/nginx.pid; 5 include /usr/share/nginx/modules/*.conf; 6 events { 7 worker_connections 1024; 8 } 9 http { 10 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 11 '$status $body_bytes_sent "$http_referer" ' 12 '"$http_user_agent" "$http_x_forwarded_for"'; 13 access_log /var/log/nginx/access.log main; 14 sendfile on; 15 tcp_nopush on; 16 tcp_nodelay on; 17 keepalive_timeout 65; 18 types_hash_max_size 4096; 19 include /etc/nginx/mime.types; 20 default_type application/octet-stream; 21 include /etc/nginx/conf.d/*.conf; 22 server { 23 listen 80; 24 listen [::]:80; 25 server_name _; 26 root /usr/share/nginx/html; 27 include /etc/nginx/default.d/*.conf; 28 error_page 404 /404.html; 29 location = /40x.html { 30 } 31 error_page 500 502 503 504 /50x.html; 32 location = /50x.html { 33 } 34 } 35 }
由于之前很详细的写过Apache的配置了,这里就说一下这俩服务之间的差异,nginx的配置文件每行配置结束必须以分号;结尾
- Nginx也可以配置启动的用户,默认为nginx,可以修改
- Nginx工作进程,默认为auto(与CPU核心数保持一致),也可以手动修改
- 定义nginx的错误日志
这两个都是Apache服务里面也有的,这里就简答说一下
2.1 event
第六行event这个是指定nginx的工作模式,默认的,Apache也有这个模式,这个段落里面的内容
events {
worker_connections 1024; 指定单个进程的连接数
}
2.2 http
这个段落里面就是一些http的配置
2.2.1 log_format
首先就是定义日志的格式log_format,然后就是access.log这个就不细写
2.2.2 sendfile
sendfile on;这个代表高效传输,也就是可以将文件直接从磁盘传输到网络中,而不先读到内存
2.2.3 tcp_nopush
tcp_nopush on 立刻回应tcp的请求
2.2.4 tcp_nodelay
tcp_nodelay on 对于小数据包直接回应
2.2.5 keepalive_timeout
keepalive_timeout 65,这个通过名字就可以知道,是长连接的超时时间
2.2.6 include
我跳过了types_hash_max_size,这个是hash散列表,我也不知道是干嘛的
include /etc/nginx/mime.types; 这里是载入nginx能够识别的类型,你可以打开这个文件去看这里面都定义了什么,打开就一目了然了
2.2.7 default_type
default_type application/octet-stream; 二进制的文件可以直接下载
2.2.8 server
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
这里的server对应的就是Apache的虚拟主机
listen 监听的端口,他这里默认监听了2个,一个是IPv4,另一个是IPv6
server_name:对外提供的服务名,也就是域名
root:网站根目录
include: 定义虚拟主机的特性,可以将一些其他的特性卸载conf.d/下之后他会自动加载配置
error_page 404 :这个是错误页,当404出现的时候,nginx会返回网站根目录下的404.html
locatioin: 这个是nginx的重头戏,叫做路由规则,你可以将路由策略写在这,他就会根据你的策略来处理流量,这个我们稍后细讲
error_page 500当服务发生500以上的错误的时候,nginx返回网站根目录下的50x.html
3. 配置Nginx虚拟主机
Nginx也支持3种虚拟主机,与Apache一样
- 基于端口的虚拟主机
- 基于IP的虚拟主机
- 基于域名的虚拟主机
3.1 基于端口
虚拟主机我们可以在conf.d下进行配置
[root@ceph conf.d]# vim virthost.conf server { listen 81; root /web1; } server { listen 82; root /web2; } [root@ceph conf.d]# echo 81 > /web1/index.html [root@ceph conf.d]# echo 82 > /web2/index.html [root@ceph conf.d]# systemctl restart nginx [root@ceph conf.d]# curl localhost:82 82 [root@ceph conf.d]# curl localhost:81 81
基于端口的虚拟主机这样就配好了
3.2 基于IP
首先配置一个临时IP
[root@ceph conf.d]# sudo ip addr add 192.168.1.100/24 dev ens33 [root@ceph conf.d]# ip a |grep 192.168.1.100 inet 192.168.1.100/24 scope global ens33
虚拟的IP就配置好了,接下来配置nginx
[root@ceph conf.d]# vim virthost.conf server { listen 81; root /web1; } server { listen 82; root /web2; } server { listen 192.168.200.210:80; root /web3; } server { listen 192.168.1.100:80; root /web4; }
然后我们创建对应的网站根目录以及index.html
[root@ceph conf.d]# echo 192.168.200.210 > /web3/index.html [root@ceph conf.d]# echo 192.168.1.100 > /web4/index.html [root@ceph conf.d]# systemctl restart nginx [root@ceph conf.d]# curl 192.168.200.210 192.168.200.210 [root@ceph conf.d]# curl 192.168.1.100 192.168.1.100
可以看到,通过不同的ip访问回显是不同的内容
3.3 基于域名
[root@ceph conf.d]# vim virthost.conf server { listen 80; server_name test.com; root /web5; } server { listen 80; server_name example.com; root /web6; }
创建根目录以及重启服务,还需要做hosts解析
[root@ceph conf.d]# echo web5 > /web5/index.html [root@ceph conf.d]# echo web6 > /web6/index.html [root@ceph conf.d]# systemctl restart nginx [root@ceph conf.d]# vim /etc/hosts 192.168.200.210 test.com 192.168.200.210 example.com [root@ceph conf.d]# curl test.com web5 [root@ceph conf.d]# curl example.com web6
这样也是可以访问到对应的文件的
4. Location
这个是nginx最重要的配置了,在Apache里面访问控制是用Directory和Files来做的,那么在nginx里面的做法就是location了
4.1 拒绝访问
我不想你访问网站根目录下的/test.html,那么我的路由规则就应该这样写,还是在虚拟主机里定义
[root@ceph conf.d]# vim virthost.conf server { listen 80; server_name test.com; root /web5; location /test.html { return 403; } }
我们来创建这个test.html文件,并尝试访问
[root@ceph conf.d]# echo "test file" > /web5/test.html [root@ceph conf.d]# systemctl restart nginx [root@ceph conf.d]# curl 192.168.200.210/test.html <head>403 Forbidden 403 Forbidden
nginx