Nginx概述


Nginx是一个开源且高性能、可靠的Http web服务、代理服务

开源:直接获取源代码

高性能:支持海量开发

可靠:服务稳定

Nginx应用场景


web基础Nginx入门_配置文件

Nginx快速安装


  • rpm安装(yum安装)
  • epel仓库(阿里云)
  • 官方仓库
  • 源码安装

使用官方原

Nginx官方网站:​TP​

web基础Nginx入门_nginx_02

web基础Nginx入门_html_03

web基础Nginx入门_配置文件_04

web基础Nginx入门_配置文件_05

# 1.添加nginx官方源
[root@web01 ~]# 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

# 2.安装nginx
[root@web01 ~]# yum install -y nginx

# 3.启动nginx并加入开机自启
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx

# 4.查看nginx的版本
[root@web01 ~]# nginx -v
nginx version: nginx/1.22.0

# 5.查看nginx的版本和源码安装生成步骤的参数有哪些
[root@web01 ~]# nginx -V

nginx version: nginx/1.22.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --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'


## 源码安装生成参数
--prefix=`/application/`nginx-1.22.0 --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'

Nginx启停

# 1.启动
systemctl start nginx
nginx
`/app/`nginx/sbin/nginx

# 2.停止
systemctl stop nginx
nginx -s stop
`/app/`nginx/sbin/nginx -s stop

# 3.重新加载
systemctl reload nginx
nginx -s reload
`/app/`nginx/sbin/nginx -s reload

-c:指定配置文件的路径
-t:检查配置文件的语法(无法检测单词拼写)
-s:启停重载,服务操作
-v:查看版本号
-V:查看版本和编译参数

## nginx启动脚本systemd管理
[root@web01 ~]# vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"

[Install]
WantedBy=multi-user.target

## 源码安装,使用fpm打包,脚本内容:
# 先写脚本
[root@web01 ~]# vim post_install_nginx.sh
ln -s /application/nginx-1.20.2 /opt/nginx
echo 'PATH="/usr/local/nginx/sbin:$PATH"' > /etc/profile.d/nginx.sh
cat >> /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/application/nginx/nginx.pid
ExecStart=/application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /application/nginx/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /application/nginx/nginx.pid)"
[Install]
WantedBy=multi-user.target
EOF

Nginx配置文件


1.Nginx主配置文件

路径

类型

作用

/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默认的日志切割

Nginx配置文件详解

## nginx主配置文件
[root@web02 nginx]# grep -Ev '^$|#' /etc/nginx/nginx.conf
## 注意:nginx配置文件,每一行,都';'结尾

[root@web01 ~]# grep -Ev '^$|#' /etc/nginx/nginx.conf
## 核心层(核心模块)、全局配置
# nginx启动用户配置
user nginx;
# nginx工作线程数量(cpu亲和)
worker_processes auto;(auto 自动根据cpu的核心数来启动对应的工作进程数)
# 错误日志 日志路径 日志级别
error_log /var/log/nginx/error.log notice;
# 程序启动进程号(pid号)存放路径
pid /var/run/nginx.pid;

## 事件层(事件模块)
events {
# 一个worker进程的最大连接数
worker_connections 1024;
}

## http层,http模块、网站配置
http {
# 浏览器中,默认可以解析的格式(不需要下载的格式)
include /etc/nginx/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;
#tcp_nopush on;

# 场链接,超时时间 65s
keepalive_timeout 65;

# 数据传输过程中,使用gzip压缩
#gzip on;

# 包含 nginx其他子配置文件(网站虚拟主机配置文件server)
include /etc/nginx/conf.d/*.conf;
}

## 日志格式
$remote_addr:远端的IP(上一个节点的IP)
$remote_user:登录的用户
[$time_local]:时间
"$request":请求方式、请求uri、HTTP协议版本号
$status:状态码
$body_bytes_sent:流量
"$http_referer":跳转地址(从哪个网站跳转过来的)
"$http_user_agent":客户端信息
"$http_x_forwarded_for":记录透传IP地址(获取用户的真实IP)

## 虚拟主机配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/default.conf

server {
## 该网站的监听端口 80
listen 80;
## 该网站的主机IP或域名
server_name localhost;
## 该网站的日志路径及日志格式
#access_log /var/log/nginx/host.access.log main;
## uri跳转
location / {
## 站点目录
root /usr/share/nginx/html;
## 默认首页,索引页面
index index.html index.htm;
}
}

## server层是网站配置层,包含在http层中
http{
server {
location / {
location /{}
}
location / {}
location / {}
}
server {

}
...
}


## 多虚拟主机
http{
access_log /var/log/nginx/access.log main;

server {
listen 80;
server_name www.baidu.com;
...
access_log /var/log/nginx/www.baidu.com.access.log main;
}
server {
listen 80;
server_name zhidao.baidu.com;
...
access_log /var/log/nginx/zhidao.baidu.com.access.log main;
}
server {
listen 80;
server_name map.baidu.com;
...
access_log /var/log/nginx/map.baidu.com.access.log main;
}
server {
listen 80;
server_name fanyi.baidu.com;
...
access_log /var/log/nginx/famyi.baidu.com.access.log main;
}
}

多虚拟主机(多web网站配置)


在企业中,是不可能用一个nginx对应一套业务,多个网站都在一个nginx中配置

web基础Nginx入门_nginx_06

基于IP的多虚拟主机

web基础Nginx入门_配置文件_07

## 增加虚拟IP
[root@web01 ~]# ifconfig eth0:0 10.0.0.10
[root@web01 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.7 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::eced:47f2:16dc:d443 prefixlen 64 scopeid 0x20<link>
inet6 fe80::af6d:7e1e:72b0:d0f6 prefixlen 64 scopeid 0x20<link>
inet6 fe80::d3c7:5aac:1849:9097 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:37:98:1c txqueuelen 1000 (Ethernet)
RX packets 55629 bytes 54355236 (51.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 29502 bytes 2710004 (2.5 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.10 netmask 255.0.0.0 broadcast 10.255.255.255
ether 00:0c:29:37:98:1c txqueuelen 1000 (Ethernet)

## 增加虚拟主机配置文件
[root@web01 conf.d]# vim 10.0.0.10.conf

server {
listen 80;
server_name 10.0.0.10;

#access_log /var/log/nginx/host.access.log main;

location / {
root /code;
index index.html index.htm;
}
}

## 创建站点目录
[root@web01 ~]# mkdir /code
## 创建索引页面
[root@web01 jin]# vim index.html
www.jin.com

----------------------------------------

[root@web01 ~]# vim /etc/nginx/conf.d/jinnan.conf

server {
listen 80;
server_name 10.0.0.7;

#access_log /var/log/nginx/host.access.log main;

location / {
root /jin;
index index.html index.htm;
}
}

## 创建站点目录
[root@web01 ~]# mkdir /jin
## 创建索引页面
[root@web01 jin]# vim index.html
jin199717

## 重新加载Nginx
[root@web01 ~]# systemctl reload nginx

web基础Nginx入门_nginx_08

web基础Nginx入门_html_09

基于多端口的虚拟主机

web基础Nginx入门_配置文件_10

[root@web01 conf.d]# vim jinnan.conf 

server {
listen 81;
server_name 10.0.0.7;

#access_log /var/log/nginx/host.access.log main;

location / {
root /jin;
index index.html index.htm;
}
}

## 创建站点目录
[root@web01 ~]# mkdir /jin
## 创建索引页面
[root@web01 jin]# vim index.html
jin199717

----------------------------------------

[root@web01 conf.d]# vim 10.0.0.10.conf

server {
listen 82;
server_name 10.0.0.7;

#access_log /var/log/nginx/host.access.log main;

location / {
root /code;
index index.html index.htm;
}
}

## 创建站点目录
[root@web01 ~]# mkdir /code
## 创建索引页面
[root@web01 jin]# vim index.html
www.jin.com

## 重新加载Nginx
[root@web01 ~]# systemctl reload nginx

web基础Nginx入门_配置文件_11

web基础Nginx入门_配置文件_12

基于多域名的虚拟主机

web基础Nginx入门_配置文件_13[root@web01 conf.d]# vim

[root@web01 conf.d]# vim jinnan.conf 

server {
listen 80;
server_name 1.oldboyedu.com;

#access_log /var/log/nginx/host.access.log main;

location / {
root /jin;
index index.html index.htm;
}
}

## 创建站点目录
[root@web01 ~]# mkdir /jin
## 创建索引页面
[root@web01 jin]# vim index.html
jin199717

----------------------------------------

[root@web01 conf.d]# vim 10.0.0.10.conf

server {
listen 80;
server_name 2.oldboyedu.com;

#access_log /var/log/nginx/host.access.log main;

location / {
root /jin;
index index.html index.htm;
}
}

## 创建站点目录
[root@web01 ~]# mkdir /code
## 创建索引页面
[root@web01 jin]# vim index.html
www.jin.com

## 重新加载Nginx
[root@web01 ~]# systemctl reload nginx

在windows系统中,配置本地的DNS

1.按win+r打开运行

web基础Nginx入门_html_14

2.输入:drivers

web基础Nginx入门_html_15

3.进入与etc目录

web基础Nginx入门_nginx_16

4.使用notepad++打开hosts文件

web基础Nginx入门_html_17

5.添加域名解析

web基础Nginx入门_html_18

## 重新加载Nginx
[root@web01 ~]# systemctl reload nginx

web基础Nginx入门_html_19

web基础Nginx入门_配置文件_20

日志管理


$remote_addr       # 记录客户端IP地址
$remote_user # 记录客户端用户名
$time_local # 记录通用的本地时间
$time_iso8601 # 记录ISO8601标准格式下的本地时间
$request # 记录请求的方法以及请求的http协议 'GET /index.html HTTP 1.1'
$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 localhost;

access_log /var/log/nginx/host.access.log main;

location / {
root /code;
index index.html index.htm;
}
location /jin {
alias /code;
index index.html index.htm;

}

alias和root的区别


[root@web01 nginx]# vim /etc/nginx/conf.d/wyk.conf
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/host.access.log main;

location / {
root /code;
index index.html index.htm;
}
location /jin {
root /code;
index index.html index.htm;

}
## 访问/code/目录里面的文件时,ningx会去/code/jin目录找文件。

[root@web01 nginx]# vim /etc/nginx/conf.d/wyk.conf
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/host.access.log main;

location / {
root /code;
index index.html index.htm;
}
location /jin {
alias /code;
index index.html index.htm;

}
## 访问/code/目录里面的文件时,ningx会自动去/code目录找文件。

日志切割


[root@web01 ~]# 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
}