Nginx Web快速入门
Nginx 基本概述
nginx简述
Nginx
是一个开源且高性能、可靠的Http Web服务、代理服务。
开源: 直接获取源代码
高性能: 支持海量并发
可靠: 服务稳定
我们为什么选择Nginx服务
Nginx非常轻量 功能模块少 (源代码仅保留http与核心模块代码,其余不够核心代码会作为插件来安装)
代码模块化 (易读,便于二次开发,对于开发人员非常友好)
互联网公司都选择Nginx
1.Nginx技术成熟,具备的功能是企业最常使用而且最需要的
2.适合当前主流架构趋势, 微服务、云架构、中间层
3.统一技术栈, 降低维护成本, 降低技术更新成本。
Nginx采用Epool网络模型,Apache采用Select模型
Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。
Epool: 当用户发起请求,epool模型会直接进行处理,效率高效,并无连接限制。
Nginx的快速安装
1.源码安装=>Nginx (1.版本随意 2.安装复杂 3.升级繁琐 4.规范 5.便于管理)
2.yum安装=>Nginx (1.版本较低 2.安装简单 3.配置不易读)
3.官方仓库安装=>Nginx (1.版本较新 2.安装简单 3.配置易读)
官方仓库安装
# 添加nginx源
vim /etc/yum.repos.d/nginx.repo
## 粘贴以下信息
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
# 安装nginx
yum install -y nginx
Nginx的启停
1.Nginx启动
[root@zls ~]# /usr/sbin/nginx
[root@zls ~]# systemctl start nginx
2.Nginx停止
[root@zls ~]# /usr/sbin/nginx -s stop
[root@zls ~]# systemctl stop nginx
3.Nginx重启
[root@zls ~]# systemctl restart nginx
4.Nginx重载
[root@zls ~]# /usr/sbin/nginx -s reload
[root@zls ~]# systemctl reload nginx
Nginx网站配置
Nginx主配置文件讲解
# 打开nginx配置文件
vim /etc/nginx/nginx.conf
核心模块
-------------核心模块------------------
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# 讲解
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 {
worker_connections 1024;
}
# 讲解
events {
worker_connections 25535; #每个worker进程支持的最大连接数
use epoll; #事件驱动模型, epoll默认
}
http内核模块
---------------http内核模块-------------------------
#http层开始
http {
#包含资源类型文件
include /etc/nginx/mime.types;
#默认以下载方式传输给浏览器(前提是该资源在mime.types中无法找到)
default_type application/octet-stream;
#日志格式
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;
#高效文件传输
sendfile on;
#搭配sendfile使用
#tcp_nopush on;
#长连接超时时间
keepalive_timeout 65;
#是否开启压缩
#gzip on;
#使用Server配置网站, 每个Server{}代表一个网站(简称虚拟主机)
'server' {
listen 80; #监听端口, 默认80
server_name driverzeng.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结束层
Nginx相关文件介绍
1.Nginx主配置文件
路径 | 类型 | 作用 |
---|---|---|
/etc/nginx/nginx.conf | 配置文件 | nginx主配置文件 |
/etc/nginx/conf.d/default.conf | 配置文件 | 默认网站配置文件 |
2.Nginx代理相关参数文件
路径 | 类型 | 作用 |
---|---|---|
/etc/nginx/fastcgi_params | 配置文件 | Fastcgi代理配置文件(php) |
/etc/nginx/scgi_params | 配置文件 | scgi代理配置文件(java) |
/etc/nginx/uwsgi_params | 配置文件 | uwsgi代理配置文件(Python) |
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命令行与终端调试工具 |
nginx -s reload
nginx -s restart
5.Nginx日志相关目录与文件
路径 | 类型 | 作用 |
---|---|---|
/var/log/nginx | 目录 | Nginx默认存放日志目录 |
/etc/logrotate.d/nginx |
搭建小游戏网站
# 下载小游戏包
wget http://test.driverzeng.com/Nginx_Code/h5_games.zip
1.新增nginx配置文件
# 编写配置文件
cd /etc/nginx/conf.d/
vim game.xxx.conf
[root@web01 conf.d]# cat /etc/nginx/conf.d/game.conf
server {
listen 80;
server_name game.xxx.com;
location / {
root /code;
index index.html;
}
}
2.检查nginx的语法是否存在错误
[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
##有这两串英文代表语法正确
3.放置游戏源代码文件至nginx配置文件指定的目录
# 创建站点目录
mkdir /code
# 移动包至/code下
mv h5_games.zip /code/
# 移动至站点目录
cd /code
# 解压包
unzip h5_games.zip
# 删除__MACOSX
rm -fr __MACOSX
# 移动h5_games目录下的所有到/code
mv /code/h5_games/* /code
本地域名解析
Windows + r
输入drivers
# 重启服务
systemctl restart nginx
# 访问 game.xxx.com
Nginx虚拟主机
通常在企业中可能会有很多业务系统,那么多套业务服务如何使用Nginx配置?
如果使用如上方式部署,则需要多台服务器配置Nginx
,但如果使用虚拟主机方式,则在同一个Nginx
上运行多套单独服务,这些服务是相互独立的。简单来说,看似多套业务系统,实则可以运行在一台Nginx
服务上
Nginx
配置虚拟主机有如下三种方式:
方式一、基于主机多IP方式
方式二、基于端口的配置方式
方式三、基于多个hosts名称方式(多域名方式)
基于多IP的方式创建服务
# 配置单网卡多IP的方式
# 添加一个IP
ip addr add 10.0.0.11/24 dev eth0
# 移动到/etc/nginx/conf.d/
cd /etc/nginx/conf.d/
# 修改第一个配置文件
vim game.xxx.conf
server{
listen 80;
server_name 10.0.0.7;
location / {
root /code;
index index.html;
}
}
# 复制并改名文件
cp game.xxx.conf niubi.conf
# 修改第二个配置文件
vim niubi.conf
server{
listen 80;
server_name 10.0.0.11;
location / {
root /test;
index index.html;
}
}
# 根据第二个配置文件创建站点目录
mkdir /test
# 添加html文件
echo 'nbff' > /test/index.html
# 检测语法
nginx -t
# 重启服务
systemctl restart nginx
# 浏览器访问 10.0.0.7 10.0.0.11
基于多端口
# 创建环境目录
mkdir /test/{1,2}
# 配置文件1
vim /etc/nginx/conf.d/xxx.conf
server{
listen 80;
server_name 10.0.0.7;
location / {
root /test;
index index.html;
}
}
# 准备80端口的html页面
echo 'xcff' > /test/index.html
# 配置文件2
vim /etc/nginx/conf.d/yyy.conf
server{
listen 81;
server_name 10.0.0.7;
location / {
root /test/1;
index index.html;
}
}
# 准备81端口的html页面
echo 'xcyd' > /test/1/index.html
# 配置文件3
vim /etc/nginx/conf.d/zzz.conf
server{
listen 82;
server_name 10.0.0.7;
location / {
root /test/2;
index index.html;
}
}
# 准备82端口的html页面
echo 'xcyl' > /test/2/index.html
# 重启服务
systemctl restart nginx
# 浏览器访问
10.0.0.7:80
10.0.0.7:81
10.0.0.7:82
基于多域名方式创建服务
# 编辑配置文件1
vim /etc/nginx/conf.d/xxx.conf
server{
listen 80;
server_name 1.xxx.com;
location / {
root /test;
index index.html;
}
}
# 准备1.xxx.com的html页面
echo 'xcff' > /test/index.html
# 配置文件2
vim /etc/nginx/conf.d/yyy.conf
server{
listen 81;
server_name 1.yyy.conf;
location / {
root /test/1;
index index.html;
}
}
# 准备1.yyy.com的html页面
echo 'xcyd' > /test/1/index.html
# 配置文件3
vim /etc/nginx/conf.d/zzz.conf
server{
listen 80;
server_name 1.zzz.com;
location / {
root /test/2;
index index.html;
}
}
# 准备1.zzz.com的html页面
echo 'xcyl' > /test/2/index.html
# 重启服务
systemctl restart nginx
# 域名解析
win+R
在搜索框内输入 drivers
打开etc下的1hosts.ics
输入10.0.0.7 1.xxx.com 1.yyy.com 1.zzz.com
保存退出
# 浏览器访问
1.xxx.com:80
1.yyy.com:81
1.zzz.com:80
Nginx日志管理
在nginx默认的配置文件中,log_format已经将日志格式 定死,但是我们可不可以修改呢?
1.log_format
的作用是定义日志格式语法
# 配置语法: 包括: error.log access.log
Syntax: log_format name [escape=default|json] string ...;
Default: log_format combined ...;
Context: http
2.nginx默认日志格式语法如下:
log_format main '$remote_addr - $remote_user [$time_local] $request '
'$status $body_bytes_sent $http_referer '
'$http_user_agent $http_x_forwarded_for';
3.Nginx日志格式允许包含的内置变量
$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地址和客户端请求的服务器地址。
日志切割
[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@web02 ~]# cd /var/log/nginx/
[root@web02 nginx]# ll
total 8
-rw-r----- 1 nginx adm 0 Apr 21 03:12 access.log
-rw-r----- 1 nginx adm 1584 Apr 20 12:05 access.log-20230421
-rw-r----- 1 nginx adm 0 Apr 21 03:12 error.log
-rw-r----- 1 nginx adm 1656 Apr 21 03:12 error.log-20230421