Nginx是一个开源且高性能、可靠的Http Web服务、代理服务。
开源: 直接获取源代码
高性能: 支持海量并发
可靠: 服务稳定
咱们为何选择 Nginx服务
Nginx很是轻量
功能模块少 (源代码仅保留http与核心模块代码,其他不够核心代码会做为插件来安装)
代码模块化 (易读,便于二次开发,对于开发人员很是友好)
互联网公司都选择Nginx
1.Nginx技术成熟,具有的功能是企业最常使用并且最须要的
2.适合当前主流架构趋势, 微服务、云架构、中间层
3.统一技术栈, 下降维护成本, 下降技术更新成本。
Nginx采用Epool网络模型,Apache采用Select模型
Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而致使性能低下。
Epool: 当用户发起请求,epool模型会直接进行处理,效率高效,并没有链接限制。
Nginx 典型应用场景
html
Nginx软件安装的方式有不少种
1.源码编译=>Nginx (1.版本随意 2.安装复杂 3.升级繁琐)
2.epel仓库=>Nginx (1.版本较低 2.安装简单 3.配置不易读)
3.官方仓库=>Nginx (1.版本较新 2.安装简单 3.配置易读,推荐)html5
1.安装Nginx软件所需依赖包nginx
[root@web ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree
2.配置nginx官方yum源c++
[root@web ~]# vim /etc/yum.repos.d/nginx.repo [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1
3.安装Nginx服务,启动并加入开机自启web
[root@web ~]# yum install nginx -y [root@web ~]# systemctl enable nginx [root@web ~]# systemctl start nginx
4.经过浏览器访问该服务器ip或url地址 10.0.0.7
json
5.检查Nginx软件版本以及编译参数vim
[root@web ~]# nginx -v nginx version: nginx/1.14.0 [root@web ~]# nginx -V
6.为了让你们更清晰的了解Nginx软件的全貌,可以使用rpm -ql nginx查看总体的目录结构及对应的功能,以下表格整理了Nginx比较重要的配置文件
1.Nginx主配置文件centos
路径 | 类型 | 做用 |
---|---|---|
/etc/nginx/nginx.conf | 配置文件 | nginx主配置文件 |
/etc/nginx/conf.d/default.conf | 配置文件 | 默认网站配置文件 |
2.Nginx代理相关参数文件浏览器
路径 | 类型 | 做用 |
---|---|---|
/etc/nginx/fastcgi_params | 配置文件 | Fastcgi代理配置文件 |
/etc/nginx/scgi_params | 配置文件 | scgi代理配置文件 |
/etc/nginx/uwsgi_params | 配置文件 | uwsgi代理配置文件 |
3.Nginx编码相关配置文件服务器
路径 | 类型 | 做用 |
---|---|---|
/etc/nginx/win-utf | 配置文件 | Nginx编码转换映射文件 |
/etc/nginx/koi-utf | 配置文件 | Nginx编码转换映射文件 |
/etc/nginx/koi-win | 配置文件 | Nginx编码转换映射文件 |
/etc/nginx/mime.types | 配置文件 | Content-Type与扩展名 |
4.Nginx管理相关命令
路径 | 类型 | 做用 |
---|---|---|
/usr/sbin/nginx | 命令 | Nginx命令行管理终端工具 |
/usr/sbin/nginx-debug | 命令 | Nginx命令行与终端调试工具 |
5.Nginx日志相关目录与文件
路径 | 类型 | 做用 |
---|---|---|
/var/log/nginx | 目录 | Nginx默认存放日志目录 |
/etc/logrotate.d/nginx | 配置文件 | Nginx默认的日志切割 |
获取官网tar包并解压 [root@web01 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz [root@web01 ~]# tar xf nginx-1.14.2.tar.gz 进入nginx-1.14.2/目录,编译 [root@web01 ~]# cd nginx-1.14.2/ [root@web01 nginx-1.14.2]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' [root@web01 nginx-1.14.2]# make && make install
Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。通常,每一个区块以一对大括号{}来表示开始与结束。
Nginx主配置文件总体分为三块进行学习,分别是CoreModule(核心模块),EventModule(事件驱动模块),HttpCoreModule(http内核模块)
CoreModule核心模块
user www; #Nginx进程所使用的用户 worker_processes 1; #Nginx运行的work进程数量(建议与CPU数量一致或auto) error_log /log/nginx/error.log #Nginx错误日志存放路径 pid /var/run/nginx.pid #Nginx服务运行后产生的pid进程号
events事件模块
events { worker_connections 25535; #每一个worker进程支持的最大链接数 use epoll; #事件驱动模型, epoll默认 }
http内核模块
http { #http层开始 ... #使用Server配置网站, 每一个Server{}表明一个网站(简称虚拟主机) server { listen 80; #监听端口, 默认80 server_name bgx.com; #提供的域名 access_log access.log; #该网站的访问日志 #控制网站访问路径 location / { root /usr/share/nginx/html; #存放网站源代码的位置 index index.html index.htm; #默认返回网站的文件 } } ... #第二个虚拟主机配置 'server' { ... } include /etc/nginx/conf.d/*.conf; #包含/etc/nginx/conf.d/目录下全部以.conf结尾的文件 } #http层结束
http server location扩展了解项
http{}层下容许有多个Server{}层,一个Server{}层下又容许有多个Location http{} 标签主要用来解决用户的请求与响应。 server{} 标签主要用来响应具体的某一个网站。 location{} 标签主要用于匹配网站具体URL路径。
[root@web01 conf.d]# cat /etc/nginx/conf.d/game.conf server { listen 80; server_name game.oldboy.com; location / { root /code; index index.html; } }
[root@web01 conf.d]# mkdir /code && cd /code [root@web01 code]# rz html5.zip [root@web01 code]# unzip html5.zip [root@web01 code]# ls ceshi game html5.zip img index.html readme.txt
[root@web01 code]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 code]# systemctl reload nginx
一般在企业中可能会有不少业务系统,那么多套业务服务如何使用Nginx配置?
若是使用如上方式部署,则须要多台服务器配置Nginx,但若是使用虚拟主机方式,则在同一个Nginx上运行多套单独服务,这些服务是相互独立的。简单来讲,看似多套业务系统,实则能够运行在一台Nginx服务上
Nginx配置虚拟主机有以下三种方式:
方式1、基于主机多IP方式 方式2、基于端口的配置方式 方式3、基于多个hosts名称方式(多域名方式)
那么基于多IP的方式,有以下两种方式:
1.配置多网卡多IP的方式
server { ... listen 10.0.0.10:80; ... } server { ... listen 10.0.0.11:80; ... }
2.配置单网卡多IP的方式
#添加一个IP [root@web01 ~]# ip addr add 10.0.0.11/24 dev eth0 # 虚拟机配置方案 [root@web01 ~]# cat /etc/nginx/conf.d/addr1.conf server { ... listen 10.0.0.10:80; ... } [root@web01 ~]# cat /etc/nginx/conf.d/addr2.conf server { ... listen 10.0.0.11:80; ... }
Nginx多端口虚拟主机方式,具体配置以下
#仅修改listen监听端口便可, 但不能和系统端口出现冲突 [root@web01 ~]# cat /etc/nginx/conf.d/port1.conf server { ... listen 80; ... } [root@web01 ~]# cat /etc/nginx/conf.d/port2.conf server { ... listen 81; ... } [root@web01 ~]# cat /etc/nginx/conf.d/port3.conf server { ... listen 82; ... }
1.建立对应的web站点目录以及程序代码
[root@web01 ~]# mkdir /soft/code/{server1,server2} [root@web01 ~]# echo "server1" > /code/server1/index.html [root@web01 ~]# echo "server2" > /code/server2/index.html
2.配置不一样域名的虚拟主机
[root@web02 ~]# cat /etc/nginx/conf.d/server1.conf server { listen 80; server_name 1.xuliangwei.com; root /code/server1; index index.html; ... } [root@web01 ~]# cat /etc/nginx/conf.d/server2.conf server { ... listen 80; server_name 2.xuliangwei.com; root /code/server2; index index.html; }
Nginx有很是灵活的日志记录模式,每一个级别的配置能够有各自独立的访问日志。日志格式经过log_format命令定义格式。
# 配置语法: 包括: error.log access.log Syntax: log_format name [escape=default|json] string ...; Default: log_format combined "..."; Context: http
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
$remote_addr # 记录客户端IP地址 $remote_user # 记录客户端用户名 $time_local # 记录通用的本地时间 $time_iso8601 # 记录ISO8601标准格式下的本地时间 $request # 记录请求的方法以及请求的http协议 $status # 记录请求状态码(用于定位错误信息) $body_bytes_sent # 发送给客户端的资源字节数,不包括响应头的大小 $bytes_sent # 发送给客户端的总字节数 $msec # 日志写入时间。单位为秒,精度是毫秒。 $http_referer # 记录从哪一个页面连接访问过来的 $http_user_agent # 记录客户端浏览器相关信息 $http_x_forwarded_for #记录客户端IP地址 $request_length # 请求的长度(包括请求行, 请求头和请求正文)。 $request_time # 请求花费的时间,单位为秒,精度毫秒 # 注:若是Nginx位于负载均衡器,nginx反向代理以后, web服务器没法直接获取到客 户端真实的IP地址。 # $remote_addr获取的是反向代理的IP地址。 反向代理服务器在转发请求的http头信息中, # 增长X-Forwarded-For信息,用来记录客户端IP地址和客户端请求的服务器地址。
Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]; access_log off; Default: access_log logs/access.log combined; Context: http, server, location, if in location, limit_except
server { listen 80; server_name code.oldboy.com; #将当前的server网站的访问日志记录至对应的目录,使用main格式 access_log /var/log/nginx/code.oldboy.com.log main; location / { root /code; } #当有人请求改favicon.ico时,不记录日志 location /favicon.ico { access_log off; return 200; } }
[root@nginx conf.d]# cat /etc/logrotate.d/nginx /var/log/nginx/*.log { daily # 天天切割日志 missingok # 日志丢失忽略 rotate 52 # 日志保留52天 compress # 日志文件压缩 delaycompress # 延迟压缩日志 notifempty # 不切割空文件 create 640 nginx adm # 日志文件权限 sharedscripts postrotate # 切割日志执行的命令 if [ -f /var/run/nginx.pid ]; then kill -USR1 `cat /var/run/nginx.pid` fi endscript }
[root@bgx ~]# ll /var/log/nginx/ total 4044 -rw-r----- 1 www adm 54438 Oct 12 03:28 access.log-20181012.gz -rw-r----- 1 www adm 28657 Oct 13 03:48 access.log-20181013.gz -rw-r----- 1 www adm 10135 Oct 12 03:28 error.log-20181130.gz -rw-r----- 1 www adm 7452 Oct 13 03:48 error.log-20181201.gz