定义
Nginx(enginee x) 是一个高性能的HTTP和反向代理服务器,同时也提供IMAP/POP3/SMTP服务(这三个是邮件相关的)
功能
- 反向代理
- 通过配置文件实现集群和负载均衡
- 静态资源虚拟化(通过浏览器访问静态资源)
- 热加载
正向代理和反向代理
-
正向代理(访问哪台服务器是自己决定的)
image -
反向代理(访问哪台服务器是代理服务器决定的)
image
安装基于 nginx-1.18.0
- 安装gcc环境
yum install gcc-c++ - 安装PCRE库,用于解析正则表达式
yum install -y pcre pcre-devel - zlib压缩和解压缩依赖
yum install -y zlib zlib-devel - SSL 安全的加密的套接字协议层,用于HTTP安全传输,也就是https
yum install -y openssl openssl-devel - 解压
tar -zxvf nginx-1.18.0.tar.gz - 编译之前,先创建nginx临时目录,如果不创建,在启动nginx的过程中会报错
mkdir /var/temp/nginx -p - 在nginx目录,输入如下命令进行配置,目的是为了创建makefile文件
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access/log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--with-http_ssl_module
- 编译
make - 安装
make install - 进入sbin目录启动nginx
./nginx - 停止nginx
./nginx -s stop - 重新加载
./nginx -s reload - 打开浏览器,访问虚拟机所处内网ip即可打开nginx默认页面
nginx文件配置conf/nginx.conf文件
- 所有指令以分号结尾,#注释,$参数
- 所属用户
# 默认nobody 可以根据情况设置root 可以拥有操作更多文件权限
user nobody;
- nginx 只有一个主进程 默认1个工作进程 根据自身机器情况调节一般CPU有几个设置几
worker_processes 1;
- 错误日志级别 debug、info、notice、warn、error、crit 对应安装时候配置的-error-log-path
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info; - 进程id号 对应安装时候配置的-pid-path
pid logs/nginx.pid
- events 工作模式和连接数
events {
# Linux下默认使用epoll 可以不加
use epoll;
# 每个worker进程允许连接的客户端最大连接数
worker_connections 1024;
}
- http相关配置
include mine.types; #包含mine.types下的文件类型 include可以导入配置文件 default_type application/octet-stream;默认类型 sendfile on; #文件是否高效传输 tcp_nopush on; #允许http response header和文件的开始放在一个文件里发布,减少网络报文段的数量 keepalive_timeout 60; #每一个Http最多可以保持长连接的时间为了复用但是会消耗内存(单位秒) gzip on; #开启压缩 减少传输但是会消耗服务器cpu性能 gzip_min_length 1; #限制最小压缩 小于1字节的不会压缩 gzip_comp_level 3; #压缩比 1-9 值越大压缩比越大 cpu占用越多 gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif application/json # 定义压缩文件类型
-
server可以配置多个 虚拟主机
listener 80; #监听端口号 server_name localhost; #ip/备案好的域名
- location 路由规则表达式
location / { # / 会拼接在root值的后方 root html; index index.html index.htm; alias 别名 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
- upstream 集群 内网服务器 负载均衡规则
-
错误集合
[error] open() "/var/run/nginx/nginx.pid" failed (2:No such file or direction)
解决
mkdir /var/run/nginx
[error] invalid PID number "" in /var/run/nginx/nginx.pid
解决
./nginx -c /usr/local/nginx/conf/nginx.conf
常用命令集合
- ./nginx -s stop 暴力关闭
- ./nginx -s quit 如果有用户访问 会维持该用户请求 直到不再请求关闭(针对Http请求)
- ./nginx -t 检测配置文件语法是否正确
- ./nginx -v 查看当前版本
- ./nginx -V 查看GCC和congigure配置参数
- ./nginx -h 或者 ./nginx -? 帮助
- ./nginx -c 指定核心配置文件 切换配置
location匹配规则
- 案例设置静态资源服务器
server {
listen 8000;
server_name localhost;
location / {
root /home/images;
}
}
通过ip + :8000 + 文件名 可以访问服务器上/home/images下的文件
- = /uri 普通字符精确匹配
- ^~ /uri 对URL路径进行前缀匹配,并且在正则之前,一般用来匹配目录
- ~ pattern 波浪线表示执行一个正则匹配,区分大小写
- ~* pattern 表示执行一个正则匹配,不区分大小写
- /uri 不带任何修饰符,也表示前缀匹配,但是在正则匹配之后
- / 通用匹配,相当于switch中的default
- @ 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files
- = > ^~ > ~ > ~* > /
- 前缀匹配时,nginx 不对 url 做编码,因此请求为 /static/20%/a,可以被规则 ^~ /static/ /a 匹配到(注意是空格)
location = / {} #规则A
location /login {} #规则B
location ^~ /static/ {} #规则C
location ~ \.(gif|jpg|png|js|css)$ {} #规则D
location ~* \.png$ {} #规则E
location / {} #规则F
- 举例
http://localhost/ 将匹配规则 A
http://localhost/login 将匹配规则 B
http://localhost/register 则匹配规则 F
http://localhost/static/a.html 匹配规则 C
http://localhost/a.gif 匹配规则 D
http://localhost/static/c.png 匹配规则 C
http://localhost/a.PNG 匹配规则 E
http://localhost/category/id/1111 匹配规则 F