jgx0

本期内容概要

  • 了解web服务
  • Nginx和Apache的对比
  • 部署Nginx

内容详细

1.什么是web服务

	Web服务是一种服务导向架构的技术,通过标准的Web协议提供服务,目的是保证不同平台的应用服务可以互操作

	根据W3C的定义,Web服务应当是一个软件系统,用以支持网络间不同机器的互动操作。
网络服务通常是许多应用程序接口所组成的,它们透过网络,例如国际互联网的远程服务器端,执行客户所提交服务的请求

	web就是B/S架构	

2.web服务器软件

# 网络模型
	01 select
	02 poll
	03 epoll

1.apache
	仅支持 select网络模型
    
2.Nginx
	部署在Linux中 使用 epoll网络模型
	官网:https://nginx.org/
	软件下载:https://nginx.org/download/

image

image

image

image

3.部署Nginx

1.安装方式
	01 yum安装(从官网安装 以 web01服务器为例)
	[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

	[nginx-mainline]
	name=nginx mainline repo
	baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
	gpgcheck=1
	enabled=0
	gpgkey=https://nginx.org/keys/nginx_signing.key
	module_hotfixes=true
    
	[root@web01 ~]# yum install nginx -y
	[root@web01 ~]# systemctl stop httpd
	[root@web01 ~]# systemctl start nginx
	安装完成过后 直接在浏览器输入客户端IP连接测试
    
	02 二进制安装
	详见 rpm安装
	https://www.cnblogs.com/jgx0/p/15700136.html
        
	03 编译安装(以 web02服务器为例)
	先从官网下载 nginx-1.20.2.tar.gz 压缩包
	[root@web02 ~]# wget https://nginx.org/download/nginx-1.20.2.tar.gz
	[root@web02 ~]# tar -xf nginx-1.20.2.tar.gz
	[root@web02 ~]# 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

	[nginx-mainline]
	name=nginx mainline repo
	baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
	gpgcheck=1
	enabled=0
	gpgkey=https://nginx.org/keys/nginx_signing.key
	module_hotfixes=true
    
	[root@web02 ~]# cd nginx-1.20.2
	[root@web02 nginx-1.20.2]# ./configure
	[root@web02 nginx-1.20.2]# make
	[root@web02 nginx-1.20.2]# make install

	因为没有在环境变量 直接用会提示未安装 所以要用绝对路径执行nginx
	[root@web02 nginx]# /usr/local/nginx/sbin/nginx -v
  • nginx启动成功

image

4.平滑增加Nginx模块

'''
增加模块必须重新编译
'''

[root@web02 ~]# rm -rf nginx-1.20.2
[root@web02 ~]# tar -xf nginx-1.20.2.tar.gz
[root@web02 ~]# cd nginx-1.20.2
[root@web02 nginx-1.20.2]# ./configure --with-http_ssl_module

一般会出现报错 安装报错提示解决:
	[root@web02 nginx-1.20.2]# yum install openssl openssl-devel -y
	[root@web02 nginx-1.20.2]# ./configure --with-http_ssl_module
    
[root@web02 nginx-1.20.2]# make
[root@web02 nginx-1.20.2]# make install
[root@web02 nginx-1.20.2]# /usr/local/nginx/sbin/nginx -V
会显示 --with-http_ssl_module 模块已经加入

5.Nginx的命令

1. -v :	打印版本号
	[root@web01 ~]# nginx -v
	nginx version: nginx/1.20.2
        
2. -V :	打印版本号和配置项
    
3. -t :	检查配置文件
    
4. -T :	测试配置文件并启动
    
5. -q :	打印错误日志
    
6. -s :	操作进程
	stop   :	停止
	quit   :	退出
	reopen :	重启(关机之后在启动)
	reload :	重载(重载配置文件)
    
7. -p  :	指定nginx的工作目录
    
8. -e  :	指定错误日志路径
    
9. -c  :	指定配置文件的路径
    
10. -g :	设置一个全局的Nginx配置项
	[root@web01 ~]# nginx -g 'daemon off;'

6.Nginx配置文件

分为:
	全局配置(全局的) 和 模块配置(大括号里面的)
    
[root@web01 ~]# cat /etc/nginx/nginx.conf
#####
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
#####

1.全局配置
	01 user :	指定Nginx的启动用户
        
	02 worker_processes :	定义Nginx的worker进程数
		auto : CPU数量
            
	03 error_log :	错误日志路径
        
	04 pid :	pid的存放文件路径
        
	05 events :	模块配置
		worker_connections :每一个worker进程最多同时接入多少个请求
		use : 指定Nginx的网络模型
            
	06 http :	web服务的模块
		include      :	加载外部的配置项
		default_type :	如果找不到文件的类型,则按照指定默认类型处理
		log_format   :	定义日志格式
		例如改为json格式日志:
		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 ;
            
	07 sendfile :	高效读取文件
        
	08 keepalive_timeout :	长连接保持连接的
		HTTP 1.0 短链接
		HTTP 1.1 长连接
        
	09 server :	网址模块
		listen      :	监听的端口
		server_name :	定义域名
		location    :	访问路径
			root  :	指定网址路径
			index :	指定网址的索引文件

7.超级玛丽和象棋游戏搭建

1.上传代码

2.编辑配置文件
	创建游戏目录:
	[root@web01 opt]# mkdir /opt/Super_Marie
	[root@web01 opt]# mkdir /opt/Chinese_chess
    
	[root@web01 opt]# cd /etc/nginx/conf.d
	[root@web01 conf.d]# vim game.conf
	写入以下内容:
	server {
		listen 80;
		server_name game.Marie.com;
		location / {
			root /opt/Super_Marie;
			index index.html;
		}
	}
    
	[root@web01 conf.d]# vim game1.conf
	写入以下内容:
	server {
		listen 80;
		server_name game.chess.com;
		location / {
			root /opt/Chinese_chess;
			index index.html;
		}
	}
    
3.测试配置文件是否正常
	[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
        
4.重启Nginx
	[root@web01 conf.d]# systemctl restart nginx
    
5.Windows域名解析
	目录: C:\Windows\System32\drivers\etc\hosts
	文本内写入并保存(如果没有该文件:hosts 直接创建普通文本即可):
		192.168.15.7 game.Marie.com
		192.168.15.7 game.chess.com
        
6.测试
	浏览器输入网址:
	game.Marie.com
	game.chess.com
	正常打开游戏就没问题

image

分类:

技术点:

相关文章: