11-26

一、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源安装
yum install -y nginx

3.官方源安装
1)配置官方源
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
yum install -y nginx

4)配置nginx
vim /etc/nginx/nginx.conf
user www;

5)启动服务
1.方法一:
systemctl start nginx
2.方法二:
nginx

6)检查启动
1.方式一
systemctl status nginx
2.方式二:
ps -ef | grep nginx
3.方式三:
netstat -lntp | grep 80
4.方式四:
直接访问网站 http://10.0.0.7/
5.方式五
curl 10.0.0.7
6.方式六:
nginx -v

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

4.源码包安装
1)下载安装包
wget http://nginx.org/download/nginx-1.16.1.tar.gz
2)解压
tar xf nginx-1.16.1.tar.gz
3)创建用户
groupadd www -g 666
useradd www -u 666 -g 666 -s /sbin/nologin -M
4)生成
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 - 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=/var/run/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
#启动
[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

四、Nginx相关文件

1.Nginx主配置文件
在这里插入图片描述

2.Nginx代理相关参数文件
在这里插入图片描述

3.Nginx编码相关配置文件
在这里插入图片描述

4.Nginx管理相关命令

在这里插入图片描述

5.Nginx日志相关目录与文件
在这里插入图片描述

五、nginx配置文件

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 - r e m o t e u s e r [ remote_user [ remoteuser[time_local] “KaTeX parse error: Double superscript at position 12: request" ' '̲status b o d y b y t e s s e n t " body_bytes_sent " bodybytessent"http_referer” ’
‘“ h t t p u s e r a g e n t " " http_user_agent" " httpuseragent""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 /etc/nginx/conf.d/game.conf
server {
lisetn 80;
server_name localhost;
#server_name www.game.com;
location / {
root /code/tuixiangzi;
index index.html;
}
}
2.检查配置文件
nginx -t
#因为网站冲突
[root@web01 code]# mv /etc/nginx/conf.d/default.conf /tmp/

3.创建站点目录
mkdir /code/

4.上传代码包
[root@web01 code]# rz
[root@web01 code]# unzip tuixiangzi.zip
[root@web01 code]# mv HTML5 canvas小人推箱子小游戏 tuixinagzi

5.重载nginx
systemctl restart nginx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值