Nginx介绍
Nginx是一款轻量级的网页服务器、反向代理服务器以及电子邮件代理服务器。
具有高并发(特别是静态资源)、占用系统资源少的特性。它不仅是Web服务软件,还具有反向代理负载均衡功能和缓存服务功能
具备如下基本特性
可针对静态资源高速高并发访问及缓存。
可使用反向代理加速,并且可进行数据缓存。
具有简单负载均衡、节点健康检查和容错功能。
为什么 Nginx 总体性能比 Apache 高 ?
Nginx 使用最新的 epoll ( Linux 2.6 内核)和 kqueue ( freebsd ) 异步网络 I/O 模型,而 Apache 使用 的是传统的 select 模型。目前 Linux 下能够承受高并发访问的 Squid 、Memcached 软件采用的都是 epoll 模型。 处理大量连接的读写时,Apache 所采用的 select 网络 I/O 模型比较低效。
Nginx的安装
RPM包获取:Index of /packages/
源码包获取:Index of /download/
配置文件/etc/nginx/nginx.conf
主目录/usr/share/nginx/html
查看版本 /usr/sbin/nginx -t
配置文件语法检查/usr/sbin/nginx -t
服务启动停止 /etc/init.d/nginx {start | stop | restart | reload | status}
Nginx 软件功能模块说明
( 1 ) Nginx 核心功能模块
Nginx 核心功能模块负责 Nginx 的全局应用,主要对应主配置文件的 Main 区块和Events 区块区域,这 里有很多 Nginx 必须的全局
参数配置。
( 2 ) 标准的 http 功能模块集合
[root@www ~ ] # tree /application/nginx/
# <== 如果 tree 命令找不到需要 yum intall tree -y 安装
配置文件说明
[root@web01 conf]# cat nginx.conf worker_processes 1; ← worker 进程数量 events { ←事件区块 worker_connections 1024; ←每个worker进程可以处理的连接数 } ←事件区块结束 http { ← HTTP 区块 include mime.types; ←支持的媒体文件 default_type application/octet-stream; ←默认的媒体类型 sendfile on; ←高效传输模式 keepalive_timeout 65; ←超时时间 server { ← server 区块 listen 80; ←端口 server_name localhost; ←域名 location / { ←第一个location区块 root html; ←站点目录 index index.html index.htm; ←首页文件 } ←第一个location区块结束 error_page 500 502 503 504 /50x.html; ← 错误信息配置 location = /50x.html { 文件位置 root html; 在哪找:路径 } } ← server 区块结束 } ← HTTP 区块结束
Nginx-web 应用
1.静态页面
[root@openEuler1 html]# cd /usr/share/nginx/html/
[root@openEuler1 html]# echo "test page." >/usr/share/nginx/html/index.html
2.虚拟主机配置
基于域名的虚拟主机--基本对外提供服务的网站使用的都是基于域名的虚拟主机
基于端口的虚拟主机--通过不同的端口来区分不同的虚拟主机,此类虚拟主机对 应的企业应用主要为公司内部网
基于IP的虚拟主机----通过不同的IP区分不同的虚拟主机
-----基于域名虚拟主机示例 1)配置文件添加虚拟主机部分
server { listen 80; server_name bbs.test.com; location / { root html/bbs; index index.html index.htm; } } server { listen 80; server_name blog.test.com; location / { root html/blog; index index.html index.htm; } }
创建站点目录
mkdir /usr/share/nginx/html/blog
mkdir /usr/share/nginx/html/bbs
创建主页文件
echo " blog test" > /usr/share/nginx/html/blog/index.html
echo " bbs test" > /usr/share/nginx/html/bbs/index.html
测试
echo "192.168.190.164 blog.test.com bbs.test.com" >> /etc/hosts
[root@localhost]# curl http://blog.test.com
[root@localhost]# curl http://bbs.test.com日志配置
错误日志:记录nginx运行错误情况信息
访问日志:记录用户访问日志情况
1>配置错误日志。
系统默认配置
error_log /var/log/nginx/error.log
2>配置访问日志。 系统默认配置
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 /var/log/nginx/access.log main;
访问日志说明:
[root@openEuler1 nginx]# tail -1 access.log 192.168.190.164 - - [13/Mar/2025:23:05:45 +0800] "GET / HTTP/1.1" 200 10 "-" "curl/7.79.1" "-" $remote_addr:客户端地址 $remote_user:远程访问用户 [$time_local]:访问时间 $request:请求行信息 $status:状态码 $body_bytes_sent:响应报文主体内容大小 $http_user_agent:客户端浏览页面信息工具 $http_x_forwarded_for:反向代理转发
location指令
location 指令的作用是根据用户请求的URI来执行不同的应用。
location使用的语法为
location [=|~|~*|^~] uri { .... }
~ 匹配内容区分大小写
~* 匹配内容不区分的小写
!~ 取反
^~ 但多个匹配同时存在,优先匹配 ^~匹配的内容;不做正则表达式的检查 (优先处理)
测试location 的访问
#location / { root html; # autoindex on; # index index.html index.htm; #} location / { return 401; } location = / { return 402; } location /documents/ { return 403; } location ^~ /images/ { return 404; } location ~* \.(gif|jpg|jpeg)$ { return 500; }
测试location 的访问 访问测试:
[root@www conf]# curl -I -w "%{http_code}\n" -o /dev/null -s 192.168.150.12/docuements 401 [root@www conf]# curl -I -w "%{http_code}\n" -o /dev/null -s 192.168.150.12 402 [root@www conf]# curl -I -w "%{http_code}\n" -o /dev/null -s http://192.168.150.12/documents/ 403 [root@www conf]# curl -I -w "%{http_code}\n" -o /dev/null -s 192.168.150.12/images/a.jpg 404 [root@www conf]# curl -I -w "%{http_code}\n" -o /dev/null -s 192.168.150.12/docuements/abc.jpg 500
基于地址访问控制
实例:限制主机192.168.190.1 访问www.b.com
借助于之前虚拟主机环境:
1) 修改配置文件
server { listen 192.168.190.146:80; server_name www.b.com; access_log /data/web2/access.log combined; location / { autoindex on; root /data/web2; index index.html index.htm; deny 192.168.190.1; allow 192.168.190.0/24; deny all; }
#提示:从上到下的顺序,类似iptables。匹配到了便跳出。
2) 重启nginx
3) 客户端测试