day07 Nginx入门

Nginx简介

Nginx是一个开源且高性能、可靠的http web服务、代理服务
 
开源:直接获取源代码
高性能:支持海量开发
可靠:服务稳定

特点:
	1、高性能、高并发:nginx支持很高的并发,nginx在处理大量并发的情况下比其他web服务要快
	2、轻量且高扩展性:
        轻量:功能模块少,只保留核心模块,其他代码模块化 (易读,便于二次开发,对于开发人员非常友好)
        高扩展性:需要什么模块再安装模块,不需要全部安装,并且还支持第三方模块
	3、高可靠性:
		1)只要不过分几乎不会出现问题
		2)其他的web服务需要每隔一段时间进行重启,nginx不需要
		3)nginx的宕机时间,是99999级别
	4、支持热部署:nginx可以再运行期间,更新迭代,代码部署
	5、大部分公司都在用:
        1)Nginx技术成熟,具备的功能是企业最常使用而且最需要的
        2)适合当前主流架构趋势, 微服务、云架构、中间层
        3)统一技术栈, 降低维护成本, 降低技术更新成本。

Nginx是什么?

Nginx是什么?
    Nginx是web服务软件。
    Nginx是一个开源且高性能、可靠的http web服务、代理服务的软件
    1、开源
    2、Nginx web服务器
    3、Nginx是俄罗斯一个程序员
    4、Nginx还是代理

Nginx和Apache之间对比

网络模型:
    select		性能最低
    poll		性能稍好
    epoll 		性能最强

    Apache   : select 
    Nginx 	 :  epoll

安装Nginx

官网网址:https://nginx.org/

安装Nginx的两种种方式

安装Nginx的两种种方式:
	1、yum安装
		官方源:https://nginx.org/
		epel源:wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
	2、源代码编译安装

yum安装(官方源)

1、编辑官方源
	[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
-----------------------------------------------------------------------------------------------------

2、清除源
	[root@web01 ~]# yum clean all   # 清除缓存
	[root@web01 ~]# yum makecache   # 重新生成源
	
3、如果用的是官方源,要把epel源注释掉,不然会影响测试
	[root@web01 ~]# cd /etc/yum.repos.d
	[root@web01 yum.repos.d]# gzip epel.repo   # 给它换了个名字
	[root@web01 yum.repos.d]# ll
	-rw-r--r--  1 root root  254 Oct 18 18:49 epel.repo.gz    # 换成epel.repo.gz
	
4、安装nginx
	[root@web01 ~]# yum install nginx

5、查看nginx配置文件
	[root@web01 ~]# rpm -qc nginx
	[root@web01 ~]# vim /etc/nginx/nginx.conf

yum安装(epel源)

1、下载epel源:
	[root@web02 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

2、清除源
	[root@web01 ~]# yum clean all   # 清除缓存
	[root@web01 ~]# yum makecache   # 重新生成源
	
3、安装nginx
	[root@web02 ~]# yum install nginx -y
	
4、查看nginx配置文件
	[root@web01 ~]# rpm -qc nginx
	[root@web01 ~]# vim /etc/nginx/nginx.conf

源代码编译安装

源代码包网址:https://nginx.org/download/nginx-1.20.1.tar.gz

1、下载源代码包
	[root@web03 ~]# wget https://nginx.org/download/nginx-1.20.1.tar.gz
	
2、解压源代码包
	[root@web03 ~]# tar -xf nginx-1.20.1.tar.gz 
	
3、进入nginx目录并且设置系统配置参数
	1)、进入目录
	[root@web03 ~]# cd nginx-1.20.1
	-rwxr-xr-x 1 1001 1001   2590 May 25 20:35 configure   # 这个文件是设置参数的
	[root@web03 nginx-1.20.1]# ./configure --help          # 查看参数
	
	2)、需要设置系统配置的参数
	[root@web03 nginx-1.20.1]# ./configure --with-http_ssl_module --with-http_v2_module --with-stream
        --with-http_ssl_module		# 配置HTTPS时使用
        --with-http_v2_module		# 配置GOLANG语言时使用
        --with-stream			    # 启用TCP/UDP代理服务
        
	3)、出现错误并解决
		./configure: error: the HTTP rewrite module requires the PCRE library.  # 缺失OpenSSL安装包
		[root@web03 nginx-1.20.1]# yum install pcre pcre-devel -y   # 安装pcre,最好加上pcre-devel
		
        ./configure: error: SSL modules require the OpenSSL library.     # 缺失OpenSSL安装包
        [root@web03 nginx-1.20.1]# yum install openssl openssl-devel -y # -devel:扩展包
        
4、开始编译
	[root@web03 nginx-1.20.1]# make
	
5、安装
	[root@web03 nginx-1.20.1]# make install
	
6、 加入环境变量
	1)、查看默认安装路径
        [root@web03 nginx-1.20.1]# cd /usr/local/nginx/  
        [root@web03 nginx]# ll    # 安装之后加到这里了,不在环境变量,需要加到环境变量

	2)、加入环境变量
        [root@web03 ~]# vim /etc/profile   # 环境变量路径
        export PATH=$PATH:/usr/local/nginx/sbin    # 在最底下加上这段
        
	3)、重载文件
    	[root@web03 ~]# source /etc/profile  # 重载文件
    	
    4)、测试
        [root@web03 ~]# nginx -v
        nginx version: nginx/1.20.1
        
7、加入system系统管理(重要:一定要顶格写)
	[root@web03 nginx]# pwd   # 修改文件看看文件路径和自己路径是否一致
    /usr/local/nginx/sbin
    
    [root@web03 sbin]# vim /usr/lib/systemd/system/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/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target
-----------------------------------------------------------------------------------------------------

8、重载system服务并启动
    [root@web03 sbin]# systemctl daemon-reload
    [root@web03 sbin]# systemctl start nginx

Nginx常用命令

nginx -v   :  展示nginx的版本号
nginx -V   :  展示nginx版本号和配置参数
nginx -t   :  测试配置文件
nginx -T   :  测试配置文件并打印
nginx -q   :  静默输出错误信息
nginx -s   :  向nginx发送信号
nginx -p   :  指定运行的家目录
nginx -e   :  设置错误日志的路径
nginx -c   :  设置配置文件
nginx -g   :  临时设置配置项 

Nginx的配置文件

Nginx的进程模型
角色:
    master	: 负责监控worker进程
    worker	:负责处理HTTP请求

1、修改Nginx配置文件

[root@web01 nginx]# vim /etc/nginx/nginx.conf 

2、Nginx配置文件详解

# 工作进程启动用户
user  nginx;
# 启动的worker进程数
worker_processes  auto;
# 指定错误日志的路径
error_log  /var/log/nginx/error.log notice;
# 指定nginx进程的PID
pid        /var/run/nginx.pid;
# 配置时间
events {
    # worker进程最大连接数
    worker_connections  1024;
}

# http配置模块
http {
    # include 将其他文件导入进来
    include       /etc/nginx/mime.types;
    # default_tupe 指定nginx处理文件默认类型
    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压缩
    #gzip  on;

    # 加载其他配置文件
    include /etc/nginx/conf.d/*.conf;
}

3、 拓展网页game.conf配置

# 每一个server都代表一个网站(nginx是支持多个网站)
server {
	# 指定当前网站的域名
	server_name www.baidu.com;
	
	# 指定当前网站的端口
	listen 80;
	
	# 指定一个访问路径
	location / {
		
		# 指定站点目录
		root /opt/html;
		# 指定默认访问的文件
		index index.html;
	}
}

案例1:搭建一个超级玛丽小游戏

1、上传代码
    [root@web01 ~]# cd  /usr/share/nginx/  # 放到这里
	[root@web01 nginx]# rz 
	
2、增加网站的配置
    [root@web01 ~]# vim /etc/nginx/conf.d/game.conf
-----------------------------------------------------------------------------------------------------
server{
        server_name game.test.com;
        listen 80;

        location /{
                root /usr/share/nginx/html5-mario;
                index index.html;
        }

}
-----------------------------------------------------------------------------------------------------

3、测试nginx配置文件是否正确
    [root@web01 conf.d]# nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok   # ok代表成功
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    
4、启动nginx
	[root@web01 ~]# systemctl restart  nginx
	
5、修改本地域名解析
	C:\Windows\System32\drivers\etc\hosts
	192.168.15.7 game.test.com
	
6、去浏览器测试
	game.test.com   # 在浏览器输入域名

案例2:搭建象棋小游戏

1、上传代码
    [root@web01 ~]# cd  /usr/share/nginx/  # 放到这里
	[root@web01 nginx]# rz 
	
2、增加网站的配置
    [root@web01 ~]# vim /etc/nginx/conf.d/mms.conf
-----------------------------------------------------------------------------------------------------
server{
        server_name mms.test.com;
        listen 80;

        location /{
                root /usr/share/nginx/jiaoben1765;
                index index.html;
        }

}
-----------------------------------------------------------------------------------------------------

3、测试nginx配置文件是否正确
    [root@web01 conf.d]# nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok   # ok代表成功
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    
4、启动nginx
	[root@web01 ~]# systemctl restart  nginx
	
5、修改本地域名解析
	C:\Windows\System32\drivers\etc\hosts
	192.168.15.7 mms.test.com
	
6、去浏览器测试
	mms.test.com   # 在浏览器输入域名

排查错误的思维

1、看日志(遇见错误先看日志)
	[root@web01 ~]# vim /etc/nginx/nginx.conf      # 去这里面找错误日志的路径
	# 指定错误日志的路径
	error_log  /var/log/nginx/error.log notice;
	[root@web01 ~]# cat /var/log/nginx/error.log   # 进入错误日志查看错误
	
2、查看端口
	[root@web01 ~]# netstat -nutlp
	tcp        0      0 0.0.0.0:80              0.0.0.0:*      LISTEN      1733/nginx: master
	[root@web01 ~]# ps -ef | grep 1733  过滤下PID
	
3、杀掉所有的相关进程
	[root@web01 ~]# pkill httpd