day031-二阶段-web服务nginx

4次阅读
没有评论

01.HTTP

1.网站的访问流程
DNS-->tcp-->http请求--http响应-->tcp四次挥手

2.pv、uv、ip
PV:刷新一次算一个PV
UV:每台设备算一个UV 用户数量
IP:独立的公网IP地址

02.Nginx

WEB服务软件:
Nginx
Apache
Tomcat
------------
1.安装
    1.编译安装
    2.yum默认epel仓库安装
    3.官方yum仓库安装
2.Nginx配置
3.配置Server
    1.基于域名
    2.基于IP+端口
    3.基于多IP的业务
1.安装
安装Nginx服务方法
方法1:编译安装
方法2:直接yum安装,默认通过epel仓库安装,版本较低
[root@web01 ~]# nginx -v
nginx version: nginx/1.21.5

方法3:通过官方的yum仓库进行安装

#第一步:先配置官方的仓库
打开Nginx官网Nginx.org->找到documentation-》Installing nginx-》packages-》找到对应版本-》创建Nginx.repo文件->复制配置内容到文件中
[root@web01 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx-stable]          #仓库名称
name=nginx stable repo  #描述
baseurl=https://nginx.org/packages/centos/7/$basearch/  #链接地址
gpgcheck=0              #开启校验 1是开启 0是关闭
enabled=1               #是否开启此仓库 1是开启 0是关闭
gpgkey=https://nginx.org/keys/nginx_signing.key     # 类似md5值在官网

#测试一下gpgkey
curl https://nginx.org/keys/nginx_signing.key

#第二步:yum安装Nginx
[root@web01 ~]# yum -y install nginx
查看版本号
[root@web01 ~]# nginx -v
nginx version: nginx/1.26.1

#第三步:配置Nginx

#第四步:启动Nginx、加入开机自启
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx

检查是否启动成功
[root@web01 ~]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      5945/nginx: master 

在浏览器测试访问 10.0.0.7
2.Nginx配置
1.主配置文件:/etc/nginx/nginx.conf
2.配置文件详解
[root@web01 ~]# cat /etc/nginx/nginx.conf
#核心区块
user  nginx;                #启动Nginx的用户
worker_processes  auto;     #Nginx启动进程的数量,以CPU核心为准,核心有几个启动几个进程

error_log  /var/log/nginx/error.log notice; #错误日志
pid        /var/run/nginx.pid;              #进程的PID号

#事件区块
events {
    worker_connections  1024;       #每个进程最大的连接数
}

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

    keepalive_timeout  65;      #长连接65s

    #gzip  on;                  #是否开启压缩

    include /etc/nginx/conf.d/*.conf;   #相当于将conf.d下所有的配置剪切到当前的位置
}

#如果可以touch a.txt路径不是conf.d下而是在/etc/nginx/a.txt
[root@web01 ~]# vim /etc/nginx/conf.d/game.conf
server {
        listen 80;
        server_name www.oldboy.com;

        touch a.txt------->a.txt绝对路径是哪里? /etc/nginx/a.txt
}

#每个域名都会用单个配置文件来管理
/etc/nginx/conf.d/www.conf
/etc/nginx/conf.d/news.conf
/etc/nginx/conf.d/game.conf


###都写到一起
vim /etc/nginx/nginx.conf
...
    server {
        listen 80;                      # 监听端口
        server_name www.oldboy.com;     # 自己注册域名需要配置到这个位置

        location / {                    # 当用户访问www.oldboy.com/
        root /code;                     # 指定用户去哪个目录下拿内容 /code
        index index.html;               # 默认给他返回一个index.html页面
        }

    }
...

#检查语法
[root@web01 ~]# 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 ~]# systemctl restart nginx
HTTP状态码:
200 ok
403 有代码目录、里面是空的
404 没有目录找不到资源
#给一个静态首页文件,不需要重启,直接生效
[root@web01 ~]# echo hehe > /code/index.html
[root@web01 ~]# cat /code/index.html 
hehe

工作中写入到/etc/nginx/conf.d目录下以域名来命名配置文件
[root@web01 conf.d]# cat www.oldboy.conf 
server{
    listen 80;
    server_name www.oldboy.com;

    location / {
        root /code/;
        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

用户访问www.oldboy.com/a.txt 实际服务器路径是/code/a.txt
用户访问www.oldboy.com/test/a.txt 实际服务器路径是/code/test/a.txt
3.配置Server
3.1.基于域名方式配置业务
#百度搜索下载 html源码小游戏
xbw.ym.com--->小霸王
dazi.ym.com--->打字游戏
qw.ym.com--->青蛙吃虫子

www.baidu.com
news.baidu.com
image.baidu.com
tieba.baidu.com
----------------------
#配置小霸王业务
第一步:配置server
[root@web01 conf.d]# cat xbw.ym.com 
server{
    listen 80;
    server_name xbw.ym.com;

    location / {
        root /code/xbw/;
        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 xbw]# ll game.zip 
-rw-r--r-- 1 root root 7890373 12月  6  2024 game.zip

#解压游戏代码
[root@web01 xbw]# unzip game.zip

第三步:本地hosts解析
C:\Windows\System32\drivers\etc
10.0.0.7 xbw.ym.com

第四步:浏览器输入域名访问业务
注意:/code/下不能有index.html,不然会导致无法访问


#配置第二个业务 打字游戏
第一步:配置server
[root@web01 conf.d]# cat dazi.ym.conf 
server {
    listen 80;
    server_name dazi.ym.com;

    location / {
    root /code/dazi;
    index index.html;
    }
}
[root@web01 conf.d]# mkdir /code/dazi
[root@web01 conf.d]# cd /code/dazi/
[root@web01 dazi]# 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 dazi]# systemctl restart nginx

第二步:上传解压游戏代码
[root@web01 dazi]# ll
总用量 11364
-rw-r--r-- 1 root root 11633693  4月 20 14:48 dazi.zip

第三步:本地hosts做域名解析
10.0.0.7 xbw.ym.com dazi.ym.com

第四步:浏览器访问
dazi.ym.com

#配置第三个业务
第一步:配置server
[root@web01 conf.d]# cat qw.ym.conf 
server {
    listen 80;
    server_name qw.ym.com;

    location / {
    root /code/qw;
    index index.html;
    }
}
[root@web01 conf.d]# mkdir /code/qw
[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 conf.d]# cd /code/qw/
[root@web01 qw]# ll
总用量 972
-rw-r--r-- 1 root root 991554  4月 20 14:45 qw.zip

第三步:hosts本地解析
10.0.0.7 xbw.ym.com dazi.ym.com qw.ym.com

第四步:访问业务
qw.ym.com
3.2.基于端口方式配置业务
83 小霸王
81 打字
82 植物大战僵尸

#83对应的小霸王
[root@web01 conf.d]# cat xbw.80.conf 
server {
    listen 83;
    server_name _;

    location / {
    root /code/xbw;
    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

#81对应的打字
[root@web01 conf.d]# cat dazi.81.conf 
server {
    listen 81;
    server_name _;

    location / {
    root /code/dazi;
    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

#82对应的植物大战僵尸
[root@web01 js]# mkdir /code/js
[root@web01 conf.d]# cat js.82.conf 
server {
    listen 82;
    server_name _;

    location / {
    root /code/js;
    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

访问测试
10.0.0.7:83
10.0.0.7:81
10.0.0.7:82
3.3.基于多IP的业务(较少)
10.0.0.7--->小霸王
10.0.0.100--->打字
10.0.0.101--->植物大战僵尸

第一步:给网卡临时配置多个IP地址、重启网卡失效,企业中网卡是真实存在的
[root@web01 ~]# ip add add 10.0.0.100/24 dev ens33
[root@web01 ~]# ip add add 10.0.0.101/24 dev ens33

第二步:配置server
[root@web01 conf.d]# cat 7.xbw.conf 
server {
    listen 10.0.0.7:80;
    server_name _;

    location / {
    root /code/xbw;
    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

配置第二个业务 10.0.0.100
[root@web01 conf.d]# cat 100.dazi.conf 
server {
    listen 10.0.0.100:80;
    server_name _;

    location / {
    root /code/dazi;
    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 conf.d]# cat 101.js.conf 
server {
    listen 10.0.0.101:80;
    server_name _;

    location / {
    root /code/js;
    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

测试访问:
10.0.0.7
10.0.0.100
10.0.0.101
重点:
1.网站的访问流程
DNS-->tcp-->http请求-->http响应-->tcp四次挥手

2.nginx安装
默认epel 1.20.1
配置官网稳定版本的1.26.1

3.nginx每行配置含义

4.server的配置

5.基于三种方式配置业务 本地DNS解析
基于域名重要
基于端口重要
基于IP 了解
正文完
 0
评论(没有评论)