day07 Nginx

day07 Nginx

1.昨日复习http协议

1、HTTP协议的流程

	1、DNS解析
	2、给对应的服务器建立对应的连接
	3、发送请求报文
	4、响应请求报文,同时发送请求主体
	5、断开TCP连接

2、HTTP协议中的报文包含哪几部分?

	1、基础信息
	2、请求报文
	3、响应报文
	4、响应主体

3、常见的请求方式

	GET		: 读的过程
	POST	: 写的过程
	PUT		: 改的过程
	DELETE	: 删的过程

4、常见的响应状态码

	1xx 	: 通知信息
	2xx		: 成功
	3xx		: 重定向
	4xx		: 客户端错误
	5xx		: 服务端错误

5、请解释pv、uv以及独立IP

	pv : 页面浏览量
	uv :独立用户(设备)
	ip : 独立IP

2.Nginx是什么

Nginx是web服务软件
Nginx是一个开源且高性能、可靠的http web服务、代理服务
 
#特点
    开源:直接获取源代码(免费的)
    高性能:支持海量开发
    可靠:服务稳定
    Nginx是俄罗斯的一个程序员开发的
	Nginx是代理服务(python服务,可以用nginx代理,把它变成一个web服务软件)

3.Nginx和apache之间对比

Nginx(使用epoll)

apache(开发的版本比较早)select

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

4.安装Nginx

4.1 yum安装(推荐) web01安装
	官网:nginx.org
	4.1.1 官方源(推荐)
		①sudo yum install yum-utils(可做可不做)
		②vim /etc/yum.repos.d/nginx.repo,创建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
       ③yum clean all(清空缓存)
       ④如果有epel源记得给他注释掉(改个名字),否则会影响nginx官方源
       ⑤yum makecache(从新生成缓存)
       ⑥yum install nginx  下载nginx
       		 Repository  显示    nginx-stable 证明从nginx官方源下载的
       ⑦rpm -qc nginx 查看nginx的配置文件
       		[root@web01 nginx]# rpm -qc nginx
                /etc/logrotate.d/nginx
                /etc/nginx/conf.d/default.conf
                /etc/nginx/fastcgi_params
                /etc/nginx/mime.types
                /etc/nginx/nginx.conf          #这个是最重要的所有配置文件都在这里配置
                /etc/nginx/scgi_params
	4.1.2 epel源 web02安装
		①cd /etc/yum.repos.d这个目录下如果没有epel源 去编辑epel.repo,内容如下 ,然后清空缓存并从新生成
            [epel]
            name="epel源"
            baseurl=https://repo.huaweicloud.com/epel/7/x86_64/
            gpgcheck=0
        ②如果有epel 执行 yum install nginx
        	Repository   :epel 从epel仓库下载的
        ③执行rpm -qc nginx
            [root@web02 ~]# rpm -qc nginx
            /etc/logrotate.d/nginx
            /etc/nginx/fastcgi.conf
            /etc/nginx/fastcgi.conf.default
            /etc/nginx/fastcgi_params
            /etc/nginx/fastcgi_params.default
            /etc/nginx/koi-utf
            /etc/nginx/koi-win
            /etc/nginx/mime.types
            /etc/nginx/mime.types.default
            /etc/nginx/nginx.conf                     #重要的
            /etc/nginx/nginx.conf.default
            /etc/nginx/scgi_params
            /etc/nginx/scgi_params.default
            /etc/nginx/uwsgi_params
            /etc/nginx/uwsgi_params.default
            /etc/nginx/win-utf
4.2 源代码编译安装(自定义安装)web03安装 需要源代码包http://nginx.org/download/nginx-1.20.1.tar.gz
	4.2.1 下载源代码包
		[root@web03 ~]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
	4.2.2解压
		[root@web03 ~]# tar -xf nginx-1.20.1.tar.gz 
	4.2.3 进入解压目录设置系统参数
		[root@web03 ~]# cd nginx-1.20.1
		[root@web03 nginx-1.20.1]# ./configure --help
		[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  	 配置golong使用
		 --with-stream 				配置tcp/udp,
		 后面跟什么就启用什么
		 
	4.2.4可能出现的错误
		1、./configure: error: SSL modules require the OpenSSL library.
			yum install openssl openssl-devel -y
		2、./configure: error: HTTP rewite module require the PCRE library.
			yum install pcre pcre-devel -y 
		前面下载的openssl 是它本身的包,后面的openssl-devel是他的扩展包
	4.2.5 make编译 (会消耗cpu)
		[root@web03 nginx-1.20.1]# make
	4.2.6 make install 编译下载
		[root@web03 nginx-1.20.1]# make install
	4.2.7 现在执行nginx显示找不到命令,需要加入到环境变量
		vim /etc/profile
		export PATH=$PATH:/usr/local/nginx/sbin
		[root@web03 nginx]# source /etc/profile  然后重载一下这个文件   source命令通常用于重新执行刚修改的初始化文件,使之立即生效,而不必注销并重新登录。该命令通常用命令“.”来替代。
		[root@web03 ~]# nginx -v                 测试一下成功否,显示了版本,成功了
            nginx version: nginx/1.20.1	
	4.2.8 加入system系统管理 
	[root@web03 ~]# 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
        #/usr/local/nginx/sbin路径记得写正确
        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
   4.2.9 重载一下system服务并启动
   		[root@web03 ~]# systemctl daemon-reload
		[root@web03 ~]# systemctl start nginx

5.Nginx常用命令详讲

[root@web03 ~]# nginx -h   
nginx version: nginx/1.20.1
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
             [-e filename] [-c filename] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -e filename   : set error log file (default: logs/error.log)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file
------------------------------------------------------------- 
  nginx -h      :  打开帮助信息       
  nginx -v   	:  展示nginx的版本号				#常用
  nginx -V		: 展示nginx版本号和配置参数
  nginx -t  	:  测试配置文件					#常用
  nginx -T   	:  测试配置文件并打印
  nginx -q   	:  静默输出错误信息
  nginx -s   	:  向nginx发送信号(stop 停止,关机,quit 退出,reopen重新打开nginx(先关在开),reload重载配置文件)		#常用
  nginx -p   	:  指定运行的家目录 (默认是:/usr/share/nginx/)
  nginx -e   	:  设置错误日志的路径 
  nginx -c   	:  设置配置文件	(默认是:/etc/nginx/nginx.conf)	#常用
  nginx -g   	:  临时设置配置项(给配置文件设置一个全局的配置项) 			#常用
------------------------------------------------------------------------------------  
[root@web02 ~]# nginx -V        #显示版本和配置选项信息,然后退出
nginx version: nginx/1.18.0     
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie
 
############################内容解析#######################################
 
nginx version: nginx/1.18.0                 #nginx版本
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)    #
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments:
 --prefix=/etc/nginx            #nginx文件的安装路径,其它选项如果使用相对路径,那么以此路径为基础路径
 --sbin-path=/usr/sbin/nginx    #二进制程序的安装目录
 --modules-path=/usr/lib64/nginx/modules    #模块安装路径
 --conf-path=/etc/nginx/nginx.conf  #设置nginx的conf文件路径
 --error-log-path=/var/log/nginx/error.log  #设置nginx错误日志路径
 --http-log-path=/var/log/nginx/access.log  #设置nginx的访问日志路径
 --pid-path=/var/run/nginx.pid  #设置nginx的pid文件路径
 --lock-path=/var/run/nginx.lock    #设置lock文件临时存放的路径
 --http-client-body-temp-path=/var/cache/nginx/client_temp  #设置http客户端主体临时缓存路径
 --http-proxy-temp-path=/var/cache/nginx/proxy_temp     #设置http反向代理临时路径
 --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp     #设置http的fastcgi临时缓存路径
 --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp #设置uwsgi临时目录
 --http-scgi-temp-path=/var/cache/nginx/scgi_temp   #scgi临时存放目录
 --user=nginx   #设置启动 worker 进程时所使用的非特权用户名
 --group=nginx  #设置启动 worker 进程时所使用的非特权用户组名
 --with-compat
 --with-file-aio
 --with-threads
 --with-http_addition_module
 --with-http_auth_request_module
 --with-http_dav_module
 --with-http_flv_module
 --with-http_gunzip_module
 --with-http_gzip_static_module
 --with-http_mp4_module
 --with-http_random_index_module
 --with-http_realip_module
 --with-http_secure_link_module
 --with-http_slice_module
 --with-http_ssl_module
 --with-http_stub_status_module
 --with-http_sub_module
 --with-http_v2_module
 --with-mail
 --with-mail_ssl_module
 --with-stream
 --with-stream_realip_module
 --with-stream_ssl_module
 --with-stream_ssl_preread_module
 --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong
 --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC'
 --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

6.Nginx的配置文件

Nginx的进程模型
		
			角色:
			
				master	: 负责监控worker进程
				worker	:负责处理HTTP请求
[root@web02 ~]# vim /etc/nginx/nginx.conf		
# 工作进程启动用户
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_type 指定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;
	
	# 长连接的超时时间(连接上最多65秒还不释放,自动给你释放)
	keepalive_timeout  65;

	# 设置GZIP压缩
    #gzip  on;

	# 加载其他的配置文件,包含网站的配置文件
    include /etc/nginx/conf.d/*.conf;
}

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

7.搭建小游戏

1.上传代码到/usr/share/nginx/
	[root@web02 nginx]# pwd
     /usr/share/nginx
     [root@web02 nginx]# ll
     total 0
     drwxr-xr-x 3 root root 136 Oct 26 15:48 html
     drwxr-xr-x 3 root root 118 Oct 26 18:37 html5-mario
     drwxr-xr-x 5 root root  56 Oct 26 18:37 jiaoben1765
2.增加网站配置(/etc/nginx/conf.d/)
[root@web02 conf.d]# cat game.conf 
    server{
        server_name=cjml.com;
        listen=80;

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

    }
3.测试nginx配置文件是否正确
	#出现ok  就是成功
	[root@web02 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
	
5.修改本机host文件dns解析
	C:\Windows\System32\drivers\etc\hosts
	加入
	192.168.15.8 cjml.com

另一个
	1.上传代码
	
	2.增加网站配置(/etc/nginx/conf.d/)
	[root@web02 conf.d]# cat game2.conf 
	server{
        server_name qi.com;
        listen 80;

        location / {
                root /usr/share/nginx/jiaoben1765;
                index index.html;
        }
}
	3.测试nginx配置文件是否正确
	[root@web02 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.修改本机hosts
        192.168.15.8 cjml.com

        192.168.15.8 qi.com
    5.重启nginx
    	systemctl restart nginx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值