Nginx的一些基础知识是作为一名前端开发工程师必须要知道的,下面会从nginx的安装、基本命令、配置文件去学习Nginx的基本使用
什么是Nginx
Nginx 是一款高性能的 http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师伊戈尔·西索夫(Igor Sysoev)所开发,官方测试 nginx 能够支支撑 5 万并发链接, 并且 cpu、内存等资源消耗却非常低,运行非常稳定。 由C语言编写 nginx使用场景
- http 服务器。Nginx 是一个 http 服务可以独立提供 http 服务。可以做网页静态服务器。
- 虚拟主机。可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。
- 反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用 nginx 做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。
Nginx的安装
1. 官网下载nginx压缩包,通过ftp工具直接放到服务器
2. 使用wget命令下载,版本自行官网查看
// 下载压缩包
wget -c https://nginx.org/download/nginx-1.11.6.tar.gz
// 解压安装包
tar -zxvf nginx-1.11.6.tar.gz
3. 基于Yum的方式安装Nginx (Yum是CentOS 中使用的默认软件包管理器)
// 查看yum是否存在
yum list | grep nginx yum install nginx nginx -v
4.nginx配置执行
./configure 执行./configure后会报错,添加依赖库
5.安装gcc环境(包括c、c++、Objective-c、Fortran、Java、Ada、GO,编译器)
yum install gcc-c++
6. 安装PCRE依赖库
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。
yum install -y pcre pcre-devel
7. 安装zlib依赖库
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
yum install -y zlib zlib-devel
8. OpenSSL
是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。 nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库
yum install -y openssl openssl-devel
9. 再次执行配置命令
./configure
10. make编译安装
make 是 Linux 开发套件里面自动化编译的一个控制程序,他通过借助 Makefile 里面编写的编译规范进行自动化的调用 gcc 、ld 以及运行某些需要的程序进行编译的程序。
make install
// 查找nginx安装路径 进入 并启动nginx
whereis nginx cd /usr/local/nginx/sbin/
./nginx 开启
./nginx -s stop 停止 此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
./nginx -s quit 此方式停止步骤是待nginx进程处理任务完毕进行停止。
./nginx -s reload
11. 修改端口号
默认端口号为80,因为通常80端口都是服务于tomcat的,所以这里我们需要修改一下端口号。
- 进入配置文件文件夹
cd /usr/local/nginx/conf
- 备份配置文件(备份是个好习惯,可省略)
cp nginx.conf nginx.conf.back
- 编辑nginx.conf配置文件
vi nginx.conf
- 重新启动一下
./nginx -s reload
- 查询nginx进程
ps aux|grep nginx
12. 重启 nginx
./nginx -s quit
./nginx
// 当修改nginx.conf 修改,要想让配置生效需要重启nginx,使用./nginx -s reload不用先停止nginx再启动,即可将配置信息在nginx中生效。
./nginx -s reload
13.设置开机启动
在etc的rc.local增加启动代码就可以了。
vi /etc/rc.local
// 添加语句
/usr/local/nginx/sbin/nginx
设置权限(Linux(AWS Redhat)开机启动workman进程(/etc/rc.local必须是755权限))
chmod 755 /etc/rc.local
14.安装总结命令
wget -c https://nginx.org/download/nginx-1.11.6.tar.gz
tar -zxvf nginx-1.11.6.tar.gz
cd nginx-1.11.6
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
./configure
make
make install
whereis nginx cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.back
vi nginx.conf
cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload
ps aux|grep nginx
15.nginx配置全局环境变量
打开/etc/profile,增加Nginx的环境变量
用vim在文件 /etc/profile 文件中增加变量,该变量将会对 Linux 下所有用户有效,并且是“永久的”
修改文件后要想马上生效还要运行 source /etc/profile 不然只能在下次重新登录后才能生效。
#Nginx enviroment
export NGINX_PATH=/usr/local/nginx
export PATH=$PATH:${NGINX_PATH}/sbin
刷新配置文件,让配置生效:
source
重新测试全局命令
nginx -v
nginx基本命令
ps aux|grep nginx # 查看nginx进程
ps -ef|grep nginx # 查看nginx进程
kill -9 pid # 强制杀死进程
nginx -s reopen #重启Nginx
nginx -s reload #重新加载Nginx配置文件,然后以优雅的方式重启
Nginx nginx -s stop #强制停止Nginx服务
nginx -s quit #优雅地停止Nginx服务(即处理完所有请求后再停止服务)
nginx -t #检测配置文件是否有语法错误,然后退出 nginx -?,-h #打开帮助信息 nginx -v #显示版本信息并退出 nginx -V #显示版本和配置选项信息,然后退出
nginx -t #检测配置文件是否有语法错误,然后退出
nginx -T #检测配置文件是否有语法错误,转储并退出
nginx -q #在检测配置文件期间屏蔽非错误信息
nginx -p prefix #设置前缀路径(默认是:/usr/share/nginx/)
nginx -c filename #设置配置文件(默认是:/etc/nginx/nginx.conf)
nginx -g directives #设置配置文件外的全局指令
killall nginx #杀死所有nginx进程 #启动、停止、重载命令
systemctl start nginx.service
systemctl stop nginx.service
systemctl reload nginx.service
systemctl status nginx.service
systemctl start nginx # 启动 Nginx
systemctl stop nginx # 停止 Nginx
systemctl restart nginx # 重启 Nginx
systemctl reload nginx # 重新加载 Nginx,用于修改配置后
systemctl enable nginx # 设置开机启动 Nginx
systemctl disable nginx # 关闭开机启动 Nginx
systemctl status nginx # 查看 Nginx 运行状态
systemctl status nginx.service # 查看nginx的状态及进程与端口(启动后)
ps aux | grep nginx #(查看nginx进程运行状态)
nginx配置文件
main # 全局配置,对全局生效
├── events # 配置影响 Nginx 服务器或与用户的网络连接
├── http # 配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置
│ ├── upstream # 配置后端服务器具体地址,负载均衡配置不可或缺的部分
│ ├── server # 配置虚拟主机的相关参数,一个 http 块中可以有多个 server 块
│ ├── server │ │ ├── location # server 块可以包含多个 location 块,location 指令用于匹配 uri
│ │ ├── location
│ │ └── ...
│ └── ...
└── ...
一个 Nginx 配置文件的结构就像 nginx.conf 显示的那样,配置文件的语法规则:
1 配置文件由指令与指令块构成;
2 每条指令以 ; 分号结尾,指令与参数间以空格符号分隔;
3 指令块以 {} 大括号将多条指令组织在一起;
4 include 语句允许组合多个配置文件以提升可维护性;
5 使用 # 符号添加注释,提高可读性;
6 使用 $ 符号使用变量;
7 部分指令的参数支持正则表达式;
server块可以包含多个location块,location用于匹配url
location [ = | ~ | ~* | ^~] uri { ... }
指令后面:
= 精确匹配路径,用于不含正则表达式的 uri 前,如果匹配成功,不再进行后续的查找;
^~ 用于不含正则表达式的 uri; 前,表示如果该符号后面的字符是最佳匹配,采用该规则,不再进行后续的查找;
~ 表示用该符号后面的正则去匹配路径,区分大小写;
~* 表示用该符号后面的正则去匹配路径,不区分大小写。跟 ~ 优先级都比较低,如有多个location的正则能匹配的话,则使用正则表达式最长的那个;
如果 uri 包含正则表达式,则必须要有 ~ 或 ~* 标志。
user nginx; # 运行用户,默认即是nginx,可以不进行设置
worker_processes 1; # Nginx 进程数,一般设置为和 CPU 核数一样
error_log /var/log/nginx/error.log warn; # Nginx 的错误日志存放目录
pid /var/run/nginx.pid; # Nginx 服务启动时的 pid 存放位置
events { use epoll; # 使用epoll的I/O模型(如果你不知道Nginx该使用哪种轮询方法,会自动选择一个最适合你操作系统的)
worker_connections 1024; # 每个进程允许最大并发数 }
http { # 配置使用最频繁的部分,代理、缓存、日志定义等绝大多数功能和第三方模块的配置都在这里设置 # 设置日志模式
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; # Nginx访问日志存放位置
sendfile on; # 开启高效传输模式
tcp_nopush on; # 减少网络报文段的数量
tcp_nodelay on;
keepalive_timeout 65; # 保持连接的时间,也叫超时时间,单位秒
types_hash_max_size 2048;
include /etc/nginx/mime.types; # 文件扩展名与类型映射表
default_type application/octet-stream; # 默认文件类型
include /etc/nginx/conf.d/*.conf; # 加载子配置项
server {
listen 80; # 配置监听的端口
server_name localhost; # 配置的域名
location / {
root /usr/share/nginx/html; # 网站根目录
index index.html index.htm; # 默认首页文件
deny 172.168.22.11; # 禁止访问的ip地址,可以为all
allow 172.168.33.44; # 允许访问的ip地址,可以为all
}
error_page 500 502 503 504 /50x.html; # 默认50x对应的访问页面
error_page 400 404 error.html; # 同上
}
}
nginx全局变量
全局变量名功能
$host 请求信息中的 Host,如果请求中没有 Host 行,则等于设置的服务器名,不包含端口 $request_method 客户端请求类型,如 GET、POST $remote_addr 客户端的 IP 地址
$args 请求中的参数
$arg_PARAMETER GET 请求中变量名 PARAMETER 参数的值,例如:$http_user_agent (Uaer-Agent 值),
$http_referer...
$content_length 请求头中的 Content-length 字段
$http_user_agent 客户端agent信息
$http_cookie 客户端cookie信息
$remote_addr 客户端的IP地址
$remote_port 客户端的端口
$http_user_agent 客户端agent信息
$server_protocol 请求使用的协议,如 HTTP/1.0、HTTP/1.1 $server_addr 服务器地址 $server_name 服务器名称
$server_port 服务器的端口号
$schemeHTTP 方法(如http,https)