day31-Nginx

发布于:2025-08-13 ⋅ 阅读:(23) ⋅ 点赞:(0)

1.每日复盘与今日内容

1.1复盘

  • HTTP协议:请求和响应
  • IP-PV-UV
  • 访问网站流程🍟🍟🍟🍟🍟

1.2今日内容

  • Nginx安装、配置、开启
  • 部署游戏平台
  • 三种业务平台
  • 错误日志

2.Nginx

1.基本介绍

  • 定义:是一个开源且高性能、可靠的Http Web服务、代理服务。

  • 特点:1.Nginx技术成熟,具备的功能是企业最常使用而且最需要的

2.适合当前主流架构趋势, 微服务、云架构、中间层

3.统一技术栈, 降低维护成本,降低技术更新成本。

4.Nginx采用Epool网络模型,Apache采用Select模型

(ps:Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。

Epool: 当用户发起请求,epool模型会直接进行处理,效率高效,并无连接限制。)

3.WEB类软件

静态(图片 视频 js html 无交互(直接存储到磁盘上))

动态(用户名 密码 手机号 家庭住址 订单信息 文字(直接存储到数据库))

Nginx

Tomcat

apache

Resin

IIS

weblogic

lighttpd

Jboss

tengine

openresty-nginx

4.Nginx安装

1.yum安装(自动)

yum -y install nginx

2.Nginx官网仓库安装(手动配置)

第一步: 配置官网yum仓库
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo		# nginx的官网配置文件
[nginx-stable]										# 仓库的名称
name=nginx stable repo					 		    # 名称
baseurl=http://nginx.org/packages/centos/7/$basearch/  # 安装地址
gpgcheck=0										   # 检查软件  0 为不检查
enabled=1										   # 是否启用此仓库 1为开启 0为关闭
gpgkey=https://nginx.org/keys/nginx_signing.key		   #检查MD5校验信息地址

第二步:安装Nginx
[root@web01 ~]# yum -y install nginx

第三步:查看Nginx版本信息
[root@web01 ~]# rpm -qa nginx
nginx-1.26.1-2.el7.ngx.x86_64

3.安装Nginx

下载软件
配置.configure
编译make
安装make install

5.Nginx配置文件详解

  • 主配置文件
/etc/nginx/nginx.conf
[root@web01 nginx]# cat nginx.conf 
# 核心区块
user  nginx;					# 启动用户
worker_processes  auto;			# 启动的进程数量 以内核核心数相同

error_log  /var/log/nginx/error.log notice;	  # 错误日志位置(notice代表文件详细程度)
pid        /var/run/nginx.pid;				  # PID所在的文件

# 事件模块
events {								    
    worker_connections  65535;   # 每个进程最大的连接数量 默认为1024
}


#http区块 响应请求
http {
    include       /etc/nginx/mime.types;			#支持的媒体类型
    default_type  application/octet-stream;         #不支持的类型默认下载方式下载到windows

    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;   # 文件高效传输

    keepalive_timeout  65; # 超时时间65秒

    #gzip  on;			   # 开启压缩

    include /etc/nginx/conf.d/*.conf;	# 将conf.d下的*.conf引用到当前位置

}

日志错误等级(从右到左依次详细)

debug, info, notice, warn, error, crit, alert, or emerg.

  • 子配置文件
/etc/nginx/conf.d/default.conf
server {
        listen 80;          #监听端口;浏览器访问默认80
        server_name www.oldboy.com;  #自己注册网站的域名

        location / {         #我们访问www.oldboy.com 实际最后都有/---www.oldboy.com/
        root /code/test;     #如果有人访问我的/,我就让其访问/code/test下的内容
        index index.html;    #默认给用户返回一个页面(默认首页) index.html,绝对路径为/code/test/index.html
        }
}
  • 语法检测
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

  • 页面代码

HTTP 404 # 页面找不到 Nginx代码目录不存在

HTTP 403 # 代码目录存在,页面不存在

  • 默认页面位置
#通过yum安装后默认nginx站点目录,或者默认页面位置
/usr/share/nginx/html/index.html

配置默认的首页/code/test/index.html  # 默认此页面不存在。
  • 测试

6.部署游戏平台

1️⃣创建server

第一步: 创建server
[root@web01 conf.d]# cat www.pvz.conf
server {
	listen 80;
	server_name www.pvz.com;

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

}

2️⃣创建代码目录

root@WEB01 /etc/nginx/conf.d# mkdir /code/pvz
root@WEB01 /etc/nginx/conf.d# cd /code/pvz/
root@WEB01 /code/pvz# pwd
/code/pvz
root@WEB01 /code/pvz# rz -E
rz waiting to receive.
root@WEB01 /code/pvz# ll
total 3904
-rw-r--r-- 1 root root 3995312 Aug 11 15:19 植物大战僵尸好用.zip

3️⃣上传游戏代码到代码目录

root@WEB01 /code/pvz# pwd
/code/pvz
root@WEB01 /code/pvz# rz -E
rz waiting to receive.
root@WEB01 /code/pvz# ll
total 3904
-rw-r--r-- 1 root root 3995312 Aug 11 15:19 植物大战僵尸好用.zip
root@WEB01 /code/pvz# unzip 植物大战僵尸好用.zip 

4️⃣解压

unzip  植物大战僵尸好用.zip

5️⃣重启Nginx生效

systemctl reload nginx

6️⃣hosts设置

在windows中进入C:\Windows\System32\drivers\etc下的hosts文件
10.0.0.7 www.pvz.com

  • 测试:windows ping 自己的网站

浏览器访问域名即可:

7.Nginx实现多业务部署

一个域名多个业务

#注册一个域名
blog.oldboy.com
image.oldboy.com
game.oldboy.com
www.oldboy.com  # 首页文件

多域名多业务

#多域名方式
www.test.com
www.oldboy.com
www.hehe.com

第一种方法:基于多个域名方式部署业务

[root@web01 conf.d]# cat bird.oldboy.conf 
server {
	listen 80;
	server_name bird.oldboy.com;

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

}

[root@web01 conf.d]# 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 conf.d]# systemctl reload nginx

创建代码目录 上传游戏代码
[root@web01 conf.d]# mkdir /code/bird
[root@web01 conf.d]# cd /code/bird

解压
tar xf bird.tar.gz

第二种方法:基于多端口(企业内部常用)

80 443   #外部用户直接访问端口服务
88 82 99 # 后台管理

#配置80端口
[root@web01 conf.d]# cat 80.conf
server {
	listen 80;
	server_name _;

	location / {
	root /code/80;
	index index.html;
	}
}
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

#配置81端口
[root@web01 conf.d]# cat 81.conf
server {
	listen 81;
	server_name _;

	location / {
	root /code/81;
	index index.html;
	}
}
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

#配置82端口
[root@web01 conf.d]# cat 82.conf
server {
	listen 82;
	server_name _;

	location / {
	root /code/82;
	index index.html;
	}
}
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

#重启服务
systemctl restart nginx

#准备默认页面
创建三个目录
[root@web01 conf.d]# mkdir /code/{80,81,82}
[root@web01 conf.d]# ll /code/
total 0
drwxr-xr-x 2 root root   6 Aug 11 11:38 80
drwxr-xr-x 2 root root   6 Aug 11 11:38 81
drwxr-xr-x 2 root root   6 Aug 11 11:38 82

[root@web01 conf.d]# echo 80.... > /code/80/index.html
[root@web01 conf.d]# echo 81.... > /code/81/index.html
[root@web01 conf.d]# echo 82.... > /code/82/index.html

#测试

第三种方法:基于多ip配置多业务

#在网卡上配置一个10.0.0.9

#实现目标
10.0.0.7 对应的是植物大战僵尸
10.0.0.9 对应的是愤怒的小鸟

#实际场景如下配置
10.0.0.7 ens33
10.0.0.9 ens34
10.0.0.10 ens35

#在网卡上配置临时增加一个10.0.0.9
[root@web01 ~]# ip add add 10.0.0.9 dev ens33
[root@web01 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:5f:6f:9f brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.7/24 brd 10.0.0.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 10.0.0.9/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe5f:6f9f/64 scope link 

#删除81、82
rm -fr /code/81 /code/82

#配置server  10.0.0.7 对应的pvz
[root@web01 conf.d]# cat 10.0.0.7.conf
server {
	listen 10.0.0.7;
	server_name _;

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

[root@web01 conf.d]# 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 conf.d]# systemctl reload nginx


#配置server  10.0.0.9对应的植物大战僵尸
[root@web01 conf.d]# cat 10.0.0.9.conf
server {
	listen 10.0.0.9;
	server_name _;

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

[root@web01 conf.d]# 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 conf.d]# systemctl restart nginx
root@WEB01 /etc/nginx/conf.d# cd /code/
root@WEB01 /code# ll
total 0
drwxr-xr-x 3 root root 117 Nov 16  2021 bird
drwxr-xr-x 3 root root  74 Aug 11 18:19 pvz
drwxr-xr-x 2 root root  24 Aug 11 17:51 test

#测试

  • 一般重启就是restart比reload有效
  • 指定一个server作为默认页面
[root@web01 conf.d]# cat www.xbw.conf 
server {
	listen 80 default;
	server_name www.xbw.com;

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

}

8.今日总结

  • nginx安装方式
  • nginx配置文件:server区块业务
  • 三种方法配置多业务
  • 日志文件位置


网站公告

今日签到

点亮在社区的每一天
去签到