当前位置: 首页 > 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) 客户端测试

相关文章:

  • golang -- 如何获取变量类型
  • ISP流程介绍(Raw格式阶段)
  • 《Vuejs设计与实现》第 5 章(非原始值响应式方案) 上
  • 发那科机器人5(异常事件和程序备份加载+ROBOGUIDE离线仿真)
  • 二叉树的深度
  • Conda激活环境无效
  • 对称加密以及非对称加密
  • transformer 笔记 tokenizer moe
  • [Windows] 希捷(Seagate)硬盘官方检测工具 - SeaTools(1.4.0.7)
  • 【身份证识别表格】批量识别身份证扫描件或照片保存为Excel表格,怎么大批量将身份证图片转为excel表格?基于WPF和腾讯OCR的识别方案
  • Path to Integer_ABC402分析与解答
  • SCDN是什么?
  • 上班摸鱼远程打游戏,哪款远控软件好用点?
  • 【Bootstrap V4系列】学习入门教程之 组件-表单(Forms)
  • MySQL如何优雅的执行DDL
  • 图解gpt之神经概率语言模型与循环神经网络
  • 【应急响应】- 日志流量如何分析?
  • SecureCRT网络穿透/代理
  • 网络研讨会开发注册中, 5月15日特励达力科,“了解以太网”
  • 深入理解C/C++内存管理:从基础到高级优化实践
  • 经济日报刊文:品牌经营不能让情怀唱“独角戏”
  • 庆祝上海总工会成立100周年暨市模范集体劳动模范和先进工作者表彰大会举行,陈吉宁寄予这些期待
  • 国家发改委:美芯片药品等领域关税影响全球科技发展,损害人类共同利益
  • 以总理内塔尼亚胡称决心彻底击败哈马斯
  • 深入贯彻中央八项规定精神学习教育中央第一指导组指导督导河北省见面会召开
  • 花20万骑自行车?CityRide带火“骑行经济”