Nginx-安装-正向代理-反向代理-进程-配置文件-虚拟主机
一、Nginx介绍-安装-常用命令
1.1 什么是nginx
nginx是一个HTTP服务器,一个邮件服务器,反向代理服务器(负载均衡,缓存)第7层负载均衡,第4层的负载均衡
1.2 nginx的安装
环境:centos 7.9.2009 系统
[root@nginx ~]# cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)
nginx版本:nginx-1.21.6
https://nginx.org/download/nginx-1.21.6.tar.gz
1.3 yum安装和编译安装的区别
yum安装的优点和缺点
yum安装的优点:
安装东西,方便快捷,不用考虑包依赖关系。
yum安装的缺点:
安装过程人为无法干预,不能按需安装。源里面有什么就安装什么。
yum安装目录不集中,但基本遵循Linux文件夹的作用去划分文件,比如配置文件通常在/etc下。
日志文件通常在/var/log下面。
编译安装的优点和缺点
源码安装的优点:
编译安装过程,可以设定参数,按照需求,进行安装,并且安装的版本,可以自己选择,灵活性比较大。
编译安装目录集中,都在我们制定的安装路径下面。
源码安装的缺点:
由于安装包过新或者是其他问题,导致依赖的包没有,或者版本过低。这个时候就要解决包的依赖问题(可能需要花很多时间来踩坑解决包依赖关系),linux系统中有的包,一个依赖一个,可能装一个小东西,就要解决一堆包的依赖问题,花很多时间解决包的依赖问题,得不尝失。源码安装的多了,不敢升级系统,升级系统,可能会导致以前手动装的东西,不能用。
1.4 nginx编译安装脚本
#install nginx
#version:1.21.6
#!/bin/bash
#install nginx
#version:1.21.6
#author: zengkaijie
#mail: 123456789@qq.com
#time: 2022-06-10
#create user zengkaijie
useradd -s /sbin/nologin zengkaijie
#download nginx
mkdir -p /lianxi/zengkaijie
cd /lianxi/zengkaijie
curl -O https://nginx.org/download/nginx-1.21.6.tar.gz
#uncompress nginx source
tar xf nginx-1.21.6.tar.gz
cd nginx-1.21.6
#resolution dependency
yum install pcre2 pcre2-devel zlib zlib-devel openssl openssl-devel -y
#configure
./configure --prefix=/usr/local/zengkaijie --user=zengkaijie --with-threads --with-http_ssl_module --with-http_v2_module
#make
make -j 2
#install
make install
#path variable
PATH=/usr/local/zengkaijie/sbin/:$PATH
echo 'PATH=/usr/local/zengkaijie/sbin/:$PATH' >>/root/.bashrc
#start nginx
if pidof nginx &>/dev/null ;then
echo "nginx is running"
#kill old nginx process
killall -9 nginx
#start new nginx process
nginx
else
nginx
fi
编写systemctl unit
添加nginx的service服务
[root@nginx system]# pwd
/usr/lib/systemd/system
[root@nginx system]# vim nginx.service
[root@nginx system]# cat 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=/usr/local/zengkaijie/logs/nginx.pid
ExecStart=/usr/local/zengkaijie/sbin/nginx -c /usr/local/zengkaijie/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
参考博客:https://blog.csdn.net/qq_41671629/article/details/114903060
1.5 nginx的常用模块
--prefix=/usr/local/scliming99 指定nginx的安装路径
--user=liming 指定启动nginx的用户
--group=liming 指定启动nginx的组
--with-threads 启动线性池的使用
--with-file-aio 支持在FreeBSD和linux上使用异步文件 I/O(aio)
--with-http_ssl_module 启用构建将HTTPS协议支持添加到HTTP服务器的模块的功能,默认情况下未构建此模块,需要OpenSSL库来构建和运行此模块
--with-http_v2_module 支持构建提供对HTTP/2支持的模块,默认情况下未构建此模块
--with-http_stub_status_module 支持构建ngx_http_stub_status_module 模块,该模块提供对基本状态信息的访问,默认情况下未构建此模块
--with-stream 支持构建用于通过TCP/UDP代理和负载平衡的流模块,默认情况下未构建此模块
1.6 怎么样判断nginx是否启动
看端口
netst8t -anplut命令
[root@nginx nginx]# netstat -anplut | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 14070/nginx: master
lsof -i:80 命令
[root@nginx nginx]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 14070 root 6u IPv4 41226 0t0 TCP *:http (LISTEN)
nginx 14071 zengkaijie 6u IPv4 41226 0t0 TCP *:http (LISTEN)
ss -anplut命令
[root@nginx nginx]# ss -anplut | grep nginx
tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=14071,fd=6),("nginx",pid=14070,fd=6))
看进程
ps -aux | grep nginx
[root@nginx nginx]# ps aux | grep nginx
root 14070 0.0 0.1 48656 1164 ? Ss 11:54 0:00 nginx: master process nginx
zengkaijie 14071 0.0 0.1 49116 1932 ? S 11:54 0:00 nginx: worker process
root 14074 0.0 0.0 112824 988 pts/0 R+ 11:57 0:00 grep --color=auto nginx
1.7 直接访问web服务器
可以访问本机ip地址,看见如下图就表示nginx已经启动成功!
1.8 nginx常用命令
添加环境变量
让nginx变成一个命令去执行
[root@nginx ~]# vim /root/.bashrc
# 添加环境变量
PATH=/usr/local/zengkaijie/sbin/:$PATH
[root@nginx ~]# which nginx
/usr/local/zengkaijie/sbin/nginx
nginx -v 显示版本号
[root@nginx lianxi]# nginx -v
nginx version: nginx/1.21.6
nginx -V 显示nginx的版本号和编译信息
[root@nginx lianxi]# nginx -V
nginx version: nginx/1.21.6
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=/usr/local/scliming99 --user=liming --group=liming --with-http_ssl_module --with-threads --with-http_v2_module --with-http_stub_status_module --with-stream --with-http_geoip_module --with-http_gunzip_module
nginx -s
-s是一种信号(signal),是进程与进程之间通信的一种方式
nginx -s stop 关闭nginx进程
-s stop命令给正在运行的Nginx master进程发送TERM信号量来快速的关闭Nginx服务
收到TERM信号量的进程会自我终结
nginx -s quit 关闭nginx进程
-s quit命令告诉Nginx正常处理完所有任务(关闭监听接口,停止接收新的链接,把当前的所有连接全部处理完)再停止进程。也就是说nginx会先处理完当前的任务再结束进程
nginx -s reload 重启nginx服务
修改了nginx的配置文件
相当于刷新服务
启用新的配置
不会中断业务 --》工作中一般使用重启
nginx -s reopen 重新生成一个日志文件
nginx -t
测试nginx.conf 配置文件是否有错误
[root@nginx lianxi]# nginx -t
nginx: the configuration file /usr/local/zengkaijie/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/zengkaijie/conf/nginx.conf test is successful
1.9 进程与进程之间的几种通信方式:
信号
管道
socket
信号量
消息队列
共享内存
1.10 捕捉信号脚本
当捕捉到 1 2 15 这三种信号的时候,执行命令 echo i am busy
[root@nginx signal]# cat sig.sh
i=1
trap "echo i am busy" 1 2 15
while :
do
((i++))
echo $i
sleep 1
done
二、正向代理vs反向代理
2.1 正向代理
代理客户端
2.2 反向代理
代理服务器
隐藏后端真实服务器
三、nginx-进程-配置文件-虚拟主机
3.1 nginx的master进程和worker进程
[root@nginx system]# ps aux | grep nginx
root 14507 0.0 0.1 48656 1164 ? Ss 15:29 0:00 nginx: master process nginx
xieshan 14508 0.0 0.1 49116 1932 ? S 15:29 0:00 nginx: worker process
root 14511 0.0 0.0 112824 988 pts/1 R+ 15:30 0:00 grep --color=auto nginx
master是worker的父进程
当我们的客户端连接过来是连接到worker进程上面的,master进程是管理worker进程的,master进程不管连接
master进程管理worker进程,当worker进程死掉会重启一个,
当master进程死掉,worker进程依然会存在(会屏蔽hup信号),nginx服务还可以正常访问,但是没有master进程以后再杀死worker进程,worker进程不会再重启进程,
3.2 配置文件
主配置文件 nginx.conf
主配置文件的作用:
就是给nginx进程提供参数的,管理员希望nginx进程按照我们的要求去工作,所以我们更改了配置文件要刷新一下服务,让nginx进程重新加载配置文件,按照修改了的配置文件的内容去工作。
主配置文件的一些常用配置
[root@nginx conf]# cat nginx.conf
#user nobody;
# 开启worker进程的数量,一般和cpu核心数量一致
worker_processes 2;
# 将nginx的master进程号记录到 nginx.pid文件里面
pid logs/nginx.pid;
# 表示一个worker进程启动2048 个线程,并发数量
events {
worker_connections 2048;
}
# http协议相关的。
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
# 启用压缩功能 --》加快传输的速度的
#gzip on;
# 提供web服务的配置 --》虚拟主机--》网站
server {
# 监听80端口
listen 80;
# 网站服务的域名
server_name www.zegnkaijie.com;
#charset koi8-r;
# 访问日志的路径和格式
access_log logs/zengkaijie.access.log main;
# 提供某个路由的根目录 -->/ 访问网站的根目录
location / {
# html是存放网页的根目录,访问网站会到这个目录下面去html文件
root html;
# 指定首页,优先级从左到右
index index.html index.htm;
}
# 错误页面,访问不到网页的时候会给用户返回这个页面
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
# 出现500 502 503 504 错误的时候返回这个页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
日志格式
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] 访问时间
$request url
$status 状态码
$body_bytes_sent nginx发送了多少数据
$http_referer 从哪个网站跳转,引流过来的
$http_user_agent 用的什么浏览器
$http_x_forwarded_for 是否有代理,转发
日志文件 logs
access.log -->记录正常的访问
error.log --> 记录错误的访问
nginx.pid–> 记录master进程的pid号
网页文件 html
404.html :出现404错误显示这个页面
50x.html:出现50错误显示这个页面
index.html: 网页根目录,访问nginx web服务器会默认访问这个页面
3.3 虚拟主机
基于IP的虚拟主机 :一个网站对应一个公网IP
基于端口的虚拟主机 : 一个网站对应一个端口
基于域名的虚拟主机
**优点:**节省服务器,省钱
**缺点:**一台虚拟服务器收到攻击,其他的会受到牵连,共用cpu,内存,磁盘,带宽,如果一台服务器的访问量特别大,会导致其他·服务器访问受到影响
基于域名的虚拟主机配置
示例:添加域名 www.zkj.com
修改配置文件,添加www.zkj.com的配置
在nginx.conf主配置文件里面添加如下配置:
有几个server就有几个虚拟主机:基于域名的虚拟主机
虚拟主机:通过不同的域名区分开访问的网站
但是这些网站都共用一个公网ip
server {
listen 80;
server_name www.zkj.com;
access_log logs/zkj.access.log main;
location / {
root html;
index index.html index.htm;
}
新建相关目录和文件
检测语法,刷新nginx服务
使用nginx -t 命令
[root@www html]# nginx -t
nginx: the configuration file /usr/local/zengkaijie/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/zengkaijie/conf/nginx.conf test is successful
nginx -s reload 刷新服务
[root@nginx html]# nginx -s reload
修改hosts文件
在linux系统的/etc/hosts下面添加域名配置 :
[root@www html]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.40.138 www.zkj.com
然后使用浏览器访问域名
[root@www html]# curl www.zkj.com
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
windows系统 hosts目录下面添加域名配置:
hosts目录:
C:\Windows\System32\drivers\etc
最后,使用浏览器访问域名
直接访问 www.zkj.com,就能成功看见页面了。