架构期day8-nginx搭建与部署

一、http响应

#正常页面
200			访问成功,ok
301			永久重定向
302			临时重定向
304			本地缓存
307			内部跳转

#客户端错误
400			客户端错误
401			认证失败
403			没有文件或者权限不足
404			没有找到文件

#服务端错误
500			后端错误
502			连接不到后端
503			后端服务器过载
504			连接后端服务器超时

二、Nginx简介

1.nginx特点

1.高性能
2.高并发
3.轻量
4.可扩展性
5.高可靠性
6.支持热部署
7.nginx使用epoll网络模型
8.nginx功能全面

2.nginx使用场景

在这里插入图片描述

三、Nginx安装

1.安装方式

1.epol源安装
2.官方源安装
3.源码包安装

2.epol源安装

[root@web03 ~]# yum install -y nginx

3.官方源安装

1)配置官方源
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
2)安装依赖
yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree
3)安装nginx
[root@web01 ~]# yum install -y nginx
4)配置nginx
[root@web01 ~]# vim /etc/nginx/nginx.conf
user  www;
5)启动服务
1.方法一:
[root@web01 ~]# systemctl start nginx

2.方法二:
[root@web01 ~]# nginx
6)检查启动
1.方式一
[root@web01 ~]# systemctl status nginx

2.方式二:
[root@web01 ~]# ps -ef | grep nginx

3.方式三:
[root@web01 ~]# netstat -lntp | grep 80

4.方式四:
直接访问网站  http://10.0.0.7/

5.方式五
[root@web01 ~]# curl 10.0.0.7

6.方式六:
[root@web01 ~]# nginx -v
7)nginx常用命令
1.nginx启动
    1)方法一:
    [root@web01 ~]# systemctl start nginx
    2)方法二:
    [root@web01 ~]# nginx
    
#注意:使用什么方式启动的,就使用对应的方式关闭
2.nginx停止
    1)方法一:
    [root@web01 ~]# systemctl stop nginx
    2)方法二:
    [root@web01 ~]# nginx -s stop
    
3.nginx重启
	1)方法一:
	[root@web01 ~]# systemctl restart nginx
	
4.nginx重载,重新加载配置文件
	1)方法一:
	[root@web01 ~]# systemctl reload nginx
	2)方法二:
	[root@web01 ~]# nginx -s reload
	
5.加入开机自启
	[root@web01 ~]# systemctl enable nginx
	
#Centos6:
	启动:nginx
		service nginx start
		/etc/init.d/nginx start
	加入开机自启:
		chkconfig nginx on

4.源码包安装

1)下载安装包
[root@web02 ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
2)解压
[root@web02 ~]# tar xf nginx-1.16.1.tar.gz
3)创建用户
[root@web02 ~]# groupadd www -g 666
[root@web02 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
4)生成
[root@web02 ~]# cd nginx-1.16.1
[root@web02 nginx-1.16.1]# ./configure --prefix=/usr/local/nginx-1.16.1 --user=www --group=www --without-http_gzip_module
5)编译安装
[root@web02 nginx-1.16.1]# make && make install
6)做软连接
[root@web02 nginx-1.16.1]# ln -s /usr/local/nginx-1.16.1 /usr/local/nginx
7)配置环境变量
[root@web02 ~]# vim /etc/profile.d/nginx.sh
export PATH=$PATH:/usr/local/nginx/sbin

[root@web02 ~]# source /etc/profile
8)启动nginx
#启动时没有办法使用system管理,需要我们自己配置
[root@web02 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx-1.18.0/logs/nginx.pid
ExecStart=/usr/local/nginx-1.18.0/sbin/nginx
ExecReload=/usr/local/nginx-1.18.0/sbin/nginx -s reload
ExecStop=/usr/local/nginx-1.18.0/sbin/nginx -s stop
PrivateTmp=true
[Install]
#启动
[root@web02 ~]# systemctl start nginx

5.nginx服务添加模块

1.安装依赖
[root@web02 nginx-1.16.1]# yum install -y openssl openssl-devel

2.再生成一次
[root@web02 nginx-1.16.1]# ./configure --prefix=/usr/local/nginx-1.16.1-new --user=www --group=www --without-http_gzip_module --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'

3.安装
[root@web02 nginx-1.16.1]# make && make install

4.重做软连接
[root@web02 ~]# rm -rf /usr/local/nginx && ln -s /usr/local/nginx-1.16.1-new /usr/local/nginx

5.重启服务
[root@web02 nginx-1.16.1]# systemctl restart nginx

6.nginx升级

1.下载新版本的包
[root@web02 ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz

2.解压
[root@web02 ~]# tar xf nginx-1.18.0.tar.gz

3.生成
[root@web02 nginx-1.18.0]# cd nginx-1.18.0
[root@web02 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx-1.18.0 --user=www --group=www --without-http_gzip_module --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

4.编译安装
[root@web02 nginx-1.18.0]# make && make install

5.重做软连接
[root@web02 ~]# rm -rf /usr/local/nginx && ln -s /usr/local/nginx-1.18.0 /usr/local/nginx

6.重启服务
[root@web02 ~]# systemctl restart nginx

7.脚本编译安装合成

#/bin/bash
mkdir /server/nginx -p
wget -O/server/nginx/nginx.tar.gz http://nginx.org/download/nginx-1.18.0.tar.gz &>/dev/null
cd /server/nginx && tar xf nginx.tar.gz -C . && rm -rf nginx.tar.gz
groupadd www -g 666 && useradd www -u 666 -g 666 -M -s /sbin/nologin
cd /server/nginx/nginx-1.18.0
./configure --user=www --group=www --prefix=/usr/local/nginx-1.18.0 --without-http_rewrite_module --without-http_gzip_module
make && make install &>/dev/null
echo 'export PATH=$PATH:/usr/local/nginx-1.18.0/sbin' > /etc/profile.d/nginx.sh && source /etc/profile
source /etc/profile
#需要手动创建配置文件,添加以下内容,可用systemctl管理,启动、开机自启等
#vim /usr/lib/systemd/system/nginx.service
#[Unit]
#Description=nginx
#After=network.target
#[Service]
#Type=forking
#PIDFile=/usr/local/nginx-1.18.0/logs/nginx.pid
#ExecStart=/usr/local/nginx-1.18.0/sbin/nginx
#ExecReload=/usr/local/nginx-1.18.0/sbin/nginx -s reload
#ExecStop=/usr/local/nginx-1.18.0/sbin/nginx -s stop
#PrivateTmp=true
#[Install]
#WantedBy=multi-user.target
systemctl start nginx

四、Nginx相关文件

为了让大家更清晰的了解Nginx软件的全貌,可使用rpm -ql nginx查看整体的目录结构及对应的功能,如下表格整理了Nginx比较重要的配置文件

1.Nginx主配置文件

路径类型作用
/etc/nginx/nginx.conf配置文件nginx主配置文件
/etc/nginx/conf.d/default.conf配置文件默认网站配置文件

2.Nginx代理相关参数文件

路径类型作用
/etc/nginx/fastcgi_params配置文件Fastcgi代理配置文件
/etc/nginx/scgi_params配置文件scgi代理配置文件
/etc/nginx/uwsgi_params配置文件uwsgi代理配置文件

3.Nginx编码相关配置文件

路径类型作用
/etc/nginx/win-utf配置文件Nginx编码转换映射文件
/etc/nginx/koi-utf配置文件Nginx编码转换映射文件
/etc/nginx/koi-win配置文件Nginx编码转换映射文件
/etc/nginx/mime.types配置文件Content-Type与扩展名

4.Nginx管理相关命令

路径类型作用
/usr/sbin/nginx命令Nginx命令行管理终端工具
/usr/sbin/nginx-debug命令Nginx命令行与终端调试工具

5.Nginx日志相关目录与文件

路径类型作用
/var/log/nginx目录Nginx默认存放日志目录
/etc/logrotate.d/nginx配置文件Nginx默认的日志切割

五、nginx配置文件

Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束。

Nginx主配置文件整体分为三块进行学习,分别是CoreModule(核心模块),EventModule(事件驱动模块),HttpCoreModule(http内核模块)

1.配置文件内容

[root@web01 ~]# cat /etc/nginx/nginx.conf 
#########################核心模块####################
#指定启动的用户
user  www;
#nginx的worker进程的数量
worker_processes  1;
#指定错误日志存放的路径以及记录的级别 debug/info/notice/warn/error/emerg
error_log  /var/log/nginx/error.log warn;
#指定pid文件
pid        /var/run/nginx.pid;

########################事件驱动模块#################
events {
	#每个worker工作进程的最大连接数
    worker_connections  1024;
}

######################http内核模块###################
http {
	#包含,nginx可识别的文件类型
    include       /etc/nginx/mime.types;
    #当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  on;
	#包含网站的配置文件
    include /etc/nginx/conf.d/*.conf;
    
    #一个server表示一个网站
    server {
    	#监听端口
        listen       80;
        #网站提供的域名
        server_name  localhost;
        #字符集
        charset utf8;
        #匹配、控制访问的网站站点
        location / {
        	#指定站点目录
            root   /usr/share/nginx/html;
            #指定默认访问的页面
            index  index.html index.htm;
        }
    }
}

六、搭建小游戏

1.编写史上最简单配置

[root@web01 ~] vim /game/game.conf
server {
    lisetn 80;
    server_name localhost;
    #server_name www.game.com;

    location / {
        root /code/tuixiangzi;
        index index.html;
    }
}

2.检查配置文件

[root@web01 game] nginx -t
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

3.创建站点目录、指定配置文件

[root@web01 ~] mkdir /game
[root@web01 ~] vim /usr/local/nginx-1.18.0/conf/nginx.conf 
http {
    include       mime.types;
    include       /game/*.conf; #在此处添加一行include,指定游戏.conf所在的位置
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
"/usr/local/nginx-1.18.0/conf/nginx.conf" 118L, 2684C                             5,1

4.上传代码包

[root@web01 game] rz
[root@web01 game] unzip tuixiangzi.zip
[root@web01 game] mv HTML5 canvas小人推箱子小游戏 tuixinagzi

4)重载nginx

[root@web01 game] systemctl restart nginx

6.访问页面玩游戏成功

七、再搭建一个游戏

1.编辑配置文件

#可在一个.conf文件里添加多个游戏,每个游戏分隔单独server块,或在/game下每个游戏单独建立一个.conf文件,这样的话容易分辨:

1.方式一:单个文件配置多个游戏
[root@web01 code] vim /game/game.conf 
server {
    listen 80;
    server_name www.tank.com;

    location / {
        root /game/tank;
        index index.html;
    }
}

server {
    listen 80;
    server_name www.mario.com;

    location / {
        root /game/mario;
        index index.html;
    }
}

2.方式二:每个文件配置一个游戏
[root@web01 code] vim /game/game1.conf 
server {
    listen 80;
    server_name www.tank.com;

    location / {
        root /game/tank;
        index index.html;
    }
}
[root@web01 code] vim /game/game2.conf 
server {
    listen 80;
    server_name www.mario.com;

    location / {
        root /game/mario;
        index index.html;
    }
}

[root@web01 code] nginx -t 查看nginx配置是否正确
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@web01 code] systemctl restart nginx #重启nginx

2.上传代码

[root@web01 game] rz
[root@web01 game] unzip tankedazhan.zip
[root@web01 game] mv jQuery坦克大战网页小游戏 tank

3.配置windows下的hosts

查找配置文件的两种方式:
1> C:\Windows\System32\drivers\etc\hosts
2> win+r 输入drivers打开etc,hosts文件
配置方式:ip 自定义域名
10.0.0.7 www.game.com www.tank.com

作业:

1.准备三台机器,使用三种方式搭建nginx
2.在三台nginx上分别搭建三个小游戏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋风お亦冷

感觉不错,您可以博主进行打赏噢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值