当前位置: 首页 > news >正文

企业高性能WEB服务器—Nginx

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) 客户端测试

http://www.xdnf.cn/news/361117.html

相关文章:

  • 【论文阅读】基于客户端数据子空间主角度的聚类联邦学习分布相似性高效识别
  • 深度解析动态IP业务核心场景:从技术演进到行业实践
  • 住宅IP的深度解析与合理运用
  • 探索Stream流:高效数据处理的秘密武器
  • TOGAF 企业架构介绍(4A架构)
  • [javascript]取消异步请求
  • 26考研——中央处理器_指令执行过程(5)
  • qiankun微前端任意位置子应用
  • Kubernetes调度策略深度解析:NodeSelector与NodeAffinity的正确打开方式
  • 网络安全体系架构:核心框架与关键机制解析
  • kubernetes服务自动伸缩-HPA
  • C++ 访问者模式详解
  • Redis面试题
  • 力扣26——删除有序数组中的重复项
  • 【推荐笔记工具】思源笔记 - 隐私优先的个人知识管理系统,支持 Markdown 排版、块级引用和双向链接
  • Qt 的原理及使用(1)——qt的背景及安装
  • 在另一个省发布抖音作品,IP属地会随之变化吗?
  • 【数据结构】1. 时间/空间复杂度
  • 2025数维杯数学建模A题完整论文模型代码:空中芭蕾
  • SpringBoot统一功能处理
  • 13.原生测试框架Unittest解决用例组织问题 与测试套件的使用
  • H5 移动端适配最佳实践落地指南。
  • 影楼精修-牙齿美型修复算法解析
  • 数据类型:List
  • robotframe启动ride.py
  • C++ Dll创建与调用 查看dll函数 MFC 单对话框应用程序(EXE 工程)改为 DLL 工程
  • C#学习——继承、封装、多态
  • 安科瑞DJSF1352-RN直流电能表的技术特点与应用
  • ZYNQ笔记(十九):VDMA VGA 输出分辨率可调
  • 各类音频放大器电路原理简析