一、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 ~]
3.官方源安装
1)配置官方源
[ root@web01 ~]
[ 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 ~]
4)配置nginx
[ root@web01 ~]
user www;
5)启动服务
1.方法一:
[ root@web01 ~]
2.方法二:
[ root@web01 ~]
6)检查启动
1.方式一
[ root@web01 ~]
2.方式二:
[ root@web01 ~]
3.方式三:
[ root@web01 ~]
4.方式四:
直接访问网站 http://10.0.0.7/
5.方式五
[ root@web01 ~]
6.方式六:
[ root@web01 ~]
7)nginx常用命令
1.nginx启动
1)方法一:
[ root@web01 ~]
2)方法二:
[ root@web01 ~]
2.nginx停止
1)方法一:
[ root@web01 ~]
2)方法二:
[ root@web01 ~]
3.nginx重启
1)方法一:
[ root@web01 ~]
4.nginx重载,重新加载配置文件
1)方法一:
[ root@web01 ~]
2)方法二:
[ root@web01 ~]
5.加入开机自启
[ root@web01 ~]
启动:nginx
service nginx start
/etc/init.d/nginx start
加入开机自启:
chkconfig nginx on
4.源码包安装
1)下载安装包
[ root@web02 ~]
2)解压
[ root@web02 ~]
3)创建用户
[ root@web02 ~]
[ root@web02 ~]
4)生成
[ root@web02 ~]
[ root@web02 nginx-1.16.1]
5)编译安装
[ root@web02 nginx-1.16.1]
6)做软连接
[ root@web02 nginx-1.16.1]
7)配置环境变量
[ root@web02 ~]
export PATH= $PATH :/usr/local/nginx/sbin
[ root@web02 ~]
8)启动nginx
[ root@web02 ~]
[ 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 ~]
5.nginx服务添加模块
1.安装依赖
[ root@web02 nginx-1.16.1]
2.再生成一次
[ root@web02 nginx-1.16.1]
3.安装
[ root@web02 nginx-1.16.1]
4.重做软连接
[ root@web02 ~]
5.重启服务
[ root@web02 nginx-1.16.1]
6.nginx升级
1.下载新版本的包
[ root@web02 ~]
2.解压
[ root@web02 ~]
3.生成
[ root@web02 nginx-1.18.0]
[ root@web02 nginx-1.18.0]
4.编译安装
[ root@web02 nginx-1.18.0]
5.重做软连接
[ root@web02 ~]
6.重启服务
[ root@web02 ~]
7.脚本编译安装合成
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 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 ~]
user www;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
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;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
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;
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;
default_type application/octet-stream;
"/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.编辑配置文件
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
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上分别搭建三个小游戏