社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  NGINX

【Linux】循序渐进学运维-服务篇-nginx入门

辛舒展 • 3 年前 • 375 次点击  

nginx 介绍

Nginx (engine x) 是一个轻量的,高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫所研发,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。

第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,Nginx 1.0.4发布。

其特点是占有内存少,并发能力强, nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

在高连接并发的情况下,Nginx是Apache服务器不错的替代品。

[root@gaosh-1 nginx]# curl -I  www.zmkjedu.com
HTTP/1.1 200 OK
Date: Thu, 30 Jul 2020 14:28:24 GMT
Server: Apache/2.4.16 (Unix) OpenSSL/1.0.2k-fips PHP/7.3.6
X-Powered-By: PHP/7.3.6
Link: <https://www.zmkjedu.com/index.php/wp-json/>; rel="https://api.w.org/"
Content-Type: text/html; charset=UTF-8

[root@gaosh-1 nginx]# curl -I  www.taobao.com
HTTP/1.1 301 Moved Permanently
Server: Tengine
Date: Thu, 30 Jul 2020 14:29:01 GMT
Content-Type: text/html
Content-Length: 278
Connection: keep-alive
Location: https://www.taobao.com/
Via: cache4.cn1003[,0]
Timing-Allow-Origin: *
EagleId: b7cb179815961193410258113e
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

Tengine:
Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网、天猫商城等得到了很好的检验。(可以这样理解:淘宝网拿到了Nginx源代码之后,进行了功能的填充、优化等等,然后提交给Nginx官方,但是由于Nginx官方相应慢甚至不响应,加上语言沟通的不顺畅,于是淘宝公司就自己打包,在遵循GPL的原则上进行二次开发,于是就出了现在的Tengine这个版本)。

官网网站:http://tengine.taobao.org/

nginx最新版本号

网址: http://nginx.org/
在这里插入图片描述

nginx与apache的对比

我们前面的n多篇文章其实都是在使用apache,那么apache有nginx有什么区别呢?

1、nginx相对于apache的优点:
  • 轻量级,同样起web 服务,比apache 占用更少的内存及资源

  • 抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下nginx 能保持低资源低消耗高性能

  • 高度模块化的设计,编写模块相对简单

  • 社区活跃,各种高性能模块出品迅速

  • 高并发场景下,nginx可以支持50000个并发连接数的响应

  • nginx适合做静态和反向代理

2. apache 相对于nginx 的优点:
  • rewrite功能 ,比nginx 的rewrite 强大

  • 模块超多,基本想到的都可以找到

  • 少bug ,nginx 的bug 相对较多

  • apache处理动态请求能力比nginx好

但nginx的缺点是只支持http,https,mail协议,对后端的健康检查只能通过端口来检测,不支持通过url检测,不支持session直接保持。

编译安装nginx

1. 安装依赖包

[root@gaosh-1 ~]# yum -y install gcc gcc-c++ pcre-devel openssl-devel wget

2. 下载nginx包

[root@gaosh-1 ~]# wget http://nginx.org/download/nginx-1.12.2.tar.gz

3. 解压安装
a.解压
[root@gaosh-1 ~]# tar xf nginx-1.12.2.tar.gz 
[root@gaosh-1 ~]# cd nginx-1.12.2

[root@gaosh-1 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx   ##环境检测
  
  • 1
  • 2
  • 3
  • 4
  • 5
b. 编译 && 编译安装

[root@gaosh-1 nginx-1.12.2]# make && make install

c. 启动
[root@gaosh-1 nginx-1.12.2]# ln -sv /usr/local/nginx/sbin/nginx /usr/bin/nginx
"/usr/bin/nginx" -> "/usr/local/nginx/sbin/nginx"     ##做软链接设置启动命令
[root@gaosh-1 nginx-1.12.2]# nginx  ## 启动
[root@gaosh-1 nginx-1.12.2]# nginx -t  ## 检查配置文件的语法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@gaosh-1 nginx-1.12.2]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
d. 查看是否启动
[root@gaosh-1 nginx-1.12.2]# ps -ef |grep nginx   ## 查看进程
root      47664      1  0 21:16 ?        00:00:00 nginx: master process nginx
nobody    47665  47664  0 21:16 ?        00:00:00 nginx: worker process
root      47670  45168  0 21:17 pts/2    00:00:00 grep nginx
[root@gaosh-1 nginx-1.12.2]# 

[root@gaosh-1 nginx-1.12.2]# nginx -s stop   ## 关闭nginx
[root@gaosh-1 nginx-1.12.2]# ps -ef |grep nginx
root      47675  45168  0 21:18 pts/2    00:00:00 grep nginx
[root@gaosh-1 nginx-1.12.2]# 
[root@gaosh-1 nginx-1.12.2]# nginx   ## 启动nginx
[root@gaosh-1 nginx-1.12.2]# nginx -s reload   ## 重新加载配置文件
[root@gaosh-1 nginx-1.12.2]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

nginx的目录结构


```objectivec
[root@gaosh-1 nginx-1.12.2]# cd /usr/local/nginx/
[root@gaosh-1 nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述

主要的配置参数

[root@ gaosh-01 ~]# cat /usr/local/nginx/conf/nginx.conf

(1)user  nginx;  #配置运行nginx的用户
(2)worker_processes  2; #初始的子进程数量
(3)worker_connections  1024; #配置单个进程处理的最大请求连接数
(4)server{  #配置虚拟主机
(5)listen	#配置虚拟主机监听端口
(6)server_name #配置服务器域名
(7)location  匹配规则 { }   #配置匹配特定的url
(8)root   #配置网站根目录
(9)index  #配置虚拟主机的默认首页
(10)error_page  404              /404.html; 
#解释:当出现404的时候,要重定向到网站根目录下的404.html页面
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

location匹配规则

= 开头表示精确匹配
^~ 开头表示 uri 以某个常规字符串开头,理解为匹配 url 路径即可。nginx 不对 url 做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)
~ 开头表示区分大小写的正则匹配
~* 开头表示不区分大小写的正则匹配
/ 通用匹配,任何请求都会匹配到
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/71984
 
375 次点击