Nginx
Nginx基本简述
Nginx是⼀个开源且⾼性能、可靠的HTTP Web服务器中间件、代理服务、负载均衡。
开源: 直接获取源代码
⾼性能: ⽀持海量并发
可靠: 服务稳定
常⻅的HTTP服务
1.HTTPD -> Apache基⾦会
2.IIS -> 微软 Windows
3.GWS -> Google
4.openrestry OpenResty
5.tengline -> 淘宝基于Nginx开发
Nginx应⽤场景
静态处理
反向代理
负载均衡
资源缓存
安全防护
访问限制
访问认证
Nginx优秀特性
1. ⾼性能和⾼并发。
2. 处理静态⽂件效率⾼。
3. 轻量级和低内存消耗。
4. 可扩展性强。
5. ⽀持反向代理和负载均衡。
6. 社区活跃和丰富的⽂档资源。
IO 和多路复⽤的概念
IO是指计算机系统中⽤于输⼊/输出的技术,在⾼负载的应⽤场景中,IO会成为系统 资源瓶颈 ,因为IO操作通常是
阻塞的 ,即应⽤程序等待IO操作完成后才能继续执⾏。这意味着应⽤程序在等待IO时会被“挂起”,不能充分地利⽤
处理器等其他资源,从⽽导致性能下降。
多路复⽤是⼀种技术,允许单个进程从多个套接字中同时等待IO事件。多路复⽤充分利⽤了计算机的并发特性和现
代操作系统的特殊API,可以⼤⼤提⾼IO性能和系统吞吐量,减少资源浪费和系统延迟。
Nginx快速安装
# yum安装
Mainline version 开发版
Stable version 稳定版
Legacy version 历史版本
基础环境准备:
Centos7
//确认系统⽹络
[root@Nginx ~]# ping baidu.com
//关闭firewalld
[root@Nginx ~]# systemctl stop firewalld
[root@Nginx ~]# systemctl disable firewalld
//临时关闭selinux
[root@Nginx ~]# setenforce 0
//初始化基本⽬录
[root@Nginx ~]# mkdir /soft/{code,logs,package,backup} -p
//基本安装包
[root@Nginx ~]# yum install -y gcc gcc-c++ autoconf \
pcre pcre-devel make automake wget httpd-tools vim tree
//配置Nginx官⽅Yum源
[root@Nginx ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
//安装Nginx
[root@Nginx ~]# yum install nginx -y
//查看Nginx当前版本
[root@Nginx ~]# nginx -v
nginx version: nginx/1.24.0
ngx_http_core_module 包含⼀些核⼼的http参数配置,对应Nginx的配置区块部分
ngx_http_access_module 访问控制模块,⽤来控制⽹站⽤户对Nginx的访问
ngx_http_gzip_module 压缩模块,对Nginx返回的数据压缩,属于性能优化模块
ngx_http_fastcgi_module fastci模块,和动态应⽤相关的模块,例如PHP
ngx_http_proxy_module proxy代理模块
ngx_http_upstream_module 负载均衡模块,可以实现⽹站的负载均衡功能及节点的健康检查。
ngx_http_rewrite_module URL地址重写模块
ngx_http_limit_conn_module 限制⽤户并发连接数及请求数模块
ngx_http_limit_req_module 限制Nginx request processing rate根据定义的key
ngx_http_log_module 访问⽇志模块,以指定的格式记录Nginx客户访问⽇志等信息
ngx_http_auth_basic_module Web认证模块,设置Web⽤户通过账号密码访问Nginx
nginx_http_ssl_module ssl模块,⽤于加密的http连接,如https
# Nginx内置变量
$uri: 当前请求的uri,不带参数 ⽐如访问:xx.com/url?name=wing 获取到 xx.com/url
$request_uri: 请求的uri,带完整参数 ⽐如访问:xx.com/url?name=wing 获取到 xx.com/url?name=wing
$host: http请求报⽂中host⾸部,如果没有则以处理此请求的虚拟主机的主机名代替
$hostname: nginx服务运⾏在主机的主机名
$remote_addr: 客户端IP
$remote_port: 客户端端⼝
$remote_user: 使⽤⽤户认证时客户端⽤户输⼊的⽤户名
$request_filename: ⽤户请求中的URI经过本地root或alias转换后映射的本地⽂件路径
$request_method: 请求⽅法, GET POST PUT DELET
$server_addr: 服务器地址
$server_name: 服务器名称
$server_port: 服务器端⼝
$server_protocol: 服务器向客户端发送响应时的协议, 如http/1.1 http/1.0
$scheme:在请求中使⽤scheme, 如http://xxx.com中的http
$http_HEADER: 匹配请求报⽂中指定的HEADER
$http_host: 匹配请求报⽂中的host⾸部
$document_root: 当前请求映射到的root配置
# Nginx编译安装
//安装依赖
prce(重定向⽀持)和openssl(https⽀持,如果不需要https可以不安装。)
yum install -y pcre-devel
yum -y install gcc make gcc-c++ wget
yum -y install openssl openssl-devel
wget http://nginx.org/download/nginx-1.23.3.tar.gz
# 解压压缩包
tar zxf nginx-1.23.3.tar.gz
# Nginx编译安装
cd nginx-1.23.3
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-pcre \
--with-pcre-jit \
--with-threads \
--with-file-aio \
--with-http_sub_module
....
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
安装报错误的话⽐如:“C compiler cc is not found”,这个就是缺少编译环境,安装⼀下就可以了 yum -y install
gcc make gcc-c++ openssl-devel
如果没有error信息,就可以执⾏下边的安装了:
make
make install
nginx测试
运⾏下⾯命令会出现两个结果,⼀般情况nginx会安装在 /usr/local/nginx ⽬录中
cd /usr/local/nginx/sbin/
./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
# 运维Nginx
# 启动
/usr/local/nginx/sbin/nginx
# 重新配置
/usr/local/nginx/sbin/nginx -s reload
# 关闭进程
/usr/local/nginx/sbin/nginx -s stop
# 平滑关闭nginx
/usr/local/nginx/sbin/nginx -s quit
# 查看nginx的安装信息模块
/usr/local/nginx/sbin/nginx -V
# 编译/⼆进制安装卸载
#停⽌nginx
nginx -s stop
#删除
rm -r /usr/local/nginx
rm /usr/lib/systemd/system/nginx.service
编译安装,删除/usr/local/nginx⽬录即可 如果配置了⾃启动脚本,也需要删除。
# yum安装卸载
yum remove nginx

浙公网安备 33010602011771号