今日内容概要

  • 了解web服务
  • 部署nginx
  • nginx和apache的对比

今日内容详细

1. 什么是web服务

web就是B/S架构

2. web服务器软件

1. apache

# 网络模型
	select
    poll
    epoll
    
XML JSON YAML

2. Nginx
官网:https://nginx.org/
软件:https://nginx.org/download/

3. 部署nginx

# 路径在web


1. yum安装# 采用官方的源(配置简单,好装)
vim /etc/yum.repos.d/nginx.repo
# 粘贴
yum install nginx -y 

systemctl stop httpd

systemctl start nginx


2. 二进制安装


3. 编译安装
下载源代码包
nginx-1.20.2 pgp 复制链接
https://nginx.org/download/nginx-1.20.2.tar.gz
1. wget 链接
2. tar -xf nginx-1.20.2.tar.gz
3. cd nginx-1.20.2
4. ./configure
5. make
6. make install
# 初学者不建议使用编译安装

4. 平滑增加nginx模块

(编译安装才行)

# 查看nginx模块 
nginx -V

/usr/local/nginx/sbin/nginx -V

# 增加模块必须重新编译
下载先
1. tar -xf nginx-1.20.2.tar.gz
2. cd nginx-1.20.2
3. ./ocnfigure --with-http_ssl_module
# 报错的时候,缺什么安装什么
# yum install openssl openssl-devel

4. make
5. make install

5. nginx的命令

-v  打印版本号
-V  显示版本号和配置选项
-t  测试(检查)配置文件并退出
-T  测试配置文件,并且运行
-q  打印错误日志
-s  操作进程(发送信号到主进程)
	stop    停止,停止但不是立即停止
    quit    退出,马上停止退出
    reopen  重启
    reload  重新加载
-p  指定nginx的工作目录默认/etc/nginx
-e  指定错误日志路径

-c  指定配置文件的路径

-g  设置一个全局的nginx参数(配置项)

6. nginx配置文件

systemctl start nginx
cd /etc/nginx

# 全局配置和模块配置两种
在外面是全局配置,在大括号中是模块配置

1. 全局配置
	1.user  # 指定nginx的启动用户
    
    2.worker_processes  # 定义nginx的worker进程数
    	auto # 等价于 cpu数量
    
    3.error_log  # 错误日志路径
    
    4.pid  # pid进程号的文件存放路径
    
    5.events # 事件模块配置
    	5.1 worker_connections 每一个worker进程最多同时接入多少个请求
        5.2 use  指定nginx的网络模型(没必要自己指定)
        
    6.http  # 配置web服务的模块
    	6.1 include # 加载外部的配置项
        6.2 default_type # 如果找不到文件的类型,则按照指定的默认类型处理
        6.3 log_format # 定义日志格式的
        log_format json '{"@timestamp":"$time_iso8601",'
                  '"host":"$server_addr",'
                  '"service":"nginxTest",'
                  '"trace":"$upstream_http_ctx_transaction_id",'
                  '"log":"log",'
                  '"clientip":"$remote_addr",'
                  '"remote_user":"$remote_user",'
                  '"request":"$request",'
                  '"http_user_agent":"$http_user_agent",'
                  '"size":$body_bytes_sent,'
                  '"responsetime":$request_time,'
                  '"upstreamtime":"$upstream_response_time",'
                  '"upstreamhost":"$upstream_addr",'
                  '"http_host":"$host",'
                  '"url":"$uri",'
                  '"domain":"$host",'
                  '"xff":"$http_x_forwarded_for",'
                  '"referer":"$http_referer",'
                  '"status":"$status"}';
    		access_log /var/log/nginx/access.log json ;
		6.4 sendfile # 高效读取文件
        6.5 keepalive_timeout # 长链接保持链接的时间(一般定义3-5s)
        	HTTP 1.0 短连接
            HTTP 1.1 长连接 # 降低了连接的时长
        6.6 server  # 网站模块
        	6.6.1 listen # 监听的端口
            6.6.2 server_name # 定义域名
            6.6.3 location # 访问路径
            	6.6.3.1 root # 指定网站路径
                6.6.3.2 index # 指定网站的索引文件

7. 超级玛丽和象棋

1. 上传代码
cd /opt/
mkdir Super_Marie
开始上传

2. 编辑配置文件
vim /etc/nginx/conf.d/game.conf
server {
    listen 80;
    server_name game.test.com;
    location / {
        root /opt/Super_Marie;
        index index.html;
    }
}

3. 测试配置文件是否正常
nginx -t

4. 重启nginx
systemctl restart nginx

5. 域名解析
C:\Windows\System32\drivers\etc\hosts
172.16.1.7 game.test.com