一.网页的笼统介绍
web服务包括:nginx, apacheck(httpd), tomcat
网页也分为:静态网页(.html,.htm.xml)
动态网页(.php,.jsp)
nginx和apache默认解析静态网页
二.为什么要用nginx? nginx和Apache的区别?
1.nginx使用的是epoll模型,是异步非阻塞;
Apache使用的是select模型,是同步阻塞
异步非阻塞 | 主进程开多个子进程(异步),CPU在工作,,默认每个进程可以有1024个连接数 |
同步阻塞 | 子进程运行时,其他数据需要排队 |
2.nginx支持负载均衡(既支持4层,又支持7层的)反向代理。配置比Apache简单
3.nginx处理静态要比Apache块,但动态处理能力就不出Apache
三.安装nginx步骤
1.1 安装nginx可以进官网进行下载也可以通过wget命令进行下载
http://nginx.org/download/nginx-1.20.2.tar.gz
[root@localhost ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
1.2 下载完成后开始对包的一系列操作
[root@localhost]# yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
[root@localhost ~]# ls
123.txt anaconda-ks.cfg nginx-1.20.2.tar.gz
[root@localhost ~]# tar zxf nginx-1.20.2.tar.gz -C /usr/local/nginx #将包解压到/usr/local/nginx
[root@localhost ~]# cd /usr/local/nginx/nginx-1.20.2/ #进入解压包
[root@localhost nginx-1.20.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_realip_module #对包进行一些列配置
[root@localhost nginx-1.20.2]# make && make install #编译&&安装
配置包的解释说明
gcc | 可以编译,c,c++,Ada,Object,c和java等语言 |
pcre pcre-devel | 模块可以使用pcre来解析正则表达式 |
zlib zlib-devel | 提供了多种压缩模式与解压方式 |
openssl openssl-devel | 相当于https加密 |
1.3 然后编写启动脚本
vi /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
1.4 将文件加执行权限 chmod +x
完成之后检查一下:/sbin/chkconfig nginx on
2.1 然后就可以访问nginx界面了:
四.nginx配置文件
1.nginx重要配置文件有:
config | nginx配置文件 |
html | nginx的网页文件 |
logs | 日志文件 |
sbin | nginx启动文件 |
2.nginx的配置文件部分
全局部分:Worker_Process 1; ##子进程的古树,最好不要大于主机cpu的内核数量
还可以添加: PID文件 启动用户组
events配置部分:写的是每个进程最大能处理的客户端连接数
可以指定epoll模型
http部分:http例可以包含多个server{ 一个server可以包含多个location{
} }(一个server可以代表一个主机)
include mine.types | 可以解析的类型,后面是文件名,文件里面是各种类型 |
default_type | 默认类型 |
sendfile no; | 零拷贝 |
keepalive_time | 超时时间,默认65秒断开 |
listen 80 | 默认监听端口号 |
server name localhost | 一般为域名 |
location | 匹配文件夹中根下找index等文件 |
error_page | 500 502 503 504,状态码,返回这些值就找/50x.html |
3.nginx访问日志
head -1 access_log先拿一行最新的访问日志
显示:
183.202.144.5 - - [13/Apr/2022:14:55:46 +800] "GEI/HTTP/1.1"200 30" -" Moailla/5.0(windows
NT 6.1;win64;x64;rv;74.0)Gecko/20100101 Firefox/74.0
#客户端ip
第一个"-"是邮箱编号
第二个"-"是用什么用户登录的
后面是时间:2022年14时55分46秒 +800在东八区
GET意思是用户登录是为了下载
HTTP请求的协议
"200 30" 200是状态码,30为包的大小
后面就是Windows系统 win64位
Firefox使用火狐访问的
4.nginx日志的格式是从哪里来呢?
打开nginx.config文件,就可以看到:
#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 logs/access.log main;
192.168.100.222 - - [10/May/2022:08:30:30 +0800] "GET / HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36 Edg/101.0.1210.39"
$remote_addr | 客户端的ip地址 |
- | 客户端的邮箱 |
$remote_user | 客户端登录服务器时用到的用户名 |
[$time_local] | 显示本地的时间 |
$request | GET(显示传输方式)以及传输协议 |
$status | 显示网站的状态码 |
$body_bytes_sent | 传输包的大小 |
$http_referer | http上⼀级页⾯, 防盗链(相当于从其他网站拿到的图片无法显示,那这个图片就是使用了防盗链)、⽤户⾏为分析 |
$http_user_agent | 客户端的电脑信息,什么系统,多少位,用的什么浏览器访问的 |
$http_X_forwarded_for | http请求携带的http |
虽然上面的数据被加上了#号,但是这些属于注释,默认它已经被执行了
5.如果想查看那些地区访问量最高,进行一个前十名的统计
cat access_log | awk '$1' |sotr -rn |uniq -c |sotr -rn |head -10
五.使用nginx的模块,或者nginx.conf进行扩展实验
1.使用nginx.conf进行创建虚拟主机
1.1 在nginx里的HTML文件中创建两个文件夹----benet----accp
1.2 打开nginx.conf文件
server{
listen 80 ;
server_name www.benet.com; #创建的域名
location / {
root html/benet/; #在html文件下查找benet文件
index index.html index.htm;
}
}
server{
listen 80 ;
server_name accp; #创建的域名
location / {
root html/accp/; #在html文件下查找accp文件
index index.html index.htm;
}
}
创建两个文件并写上两个网页文件
mkdir /uer/local/nginx/html/benet mkdir /uer/local/nginx/html/accp
vim /usr/local/nginx/html/benet/index.html vim /usr/local/nginx/html/accp/index.html
然后重启服务 systemctl restart nginx
进入主机的C盘查找C:\Windows\System32\drivers\etc\hosts文件然后指定域名
然后各自访问一次www.benet.com和www.accp.com
2.Nginx下载站点
Nginx默认是不允许列出整个⽬录浏览下载。如果只是单纯的往html文件中添加压缩文件,网页就会报错,那该怎么才能达到一堆压缩文件都显示在网页呢?
2.1 我们已经将文件放入html
但是访问网页会报错,403是没有权限
2.2 所以开始修改conf文件,咱们在location里面添加一个autoindex on;模块,这次显示文件了!
2.3 但是好像看不懂啊,咱们再添加一个charset utf-8,gbk;,非常nice它显示文字了,这些文件有多大呢?还是看不明白
2.4 继续再加一个autoindex_exact_size off | on
默认为on, 显示出⽂件的确切⼤⼩,单位是bytes。
修改为off,显示出⽂件的⼤概⼤⼩,单位是kB或者MB或者GB。
我感觉还是off好使
2.5 已经写了这么多了,再加一个可以吧,这次好像就剩时间了autoindex_localtime on | off
默认为off,显示的⽂件时间为GMT时间
修改为on, 显示的⽂件时间为⽂件的服务器时间(这个是文件上传的时间)
这次我选择on
3.nginx访问限制
limit_conn_module | 模块可以限制nginx服务器所承载的单个客户端单个ip地址在单一时间所发起的连接数量 |
limit_req_module | 模块可以限制nginx服务器所承载的单个客户端单个ip地址在单一时间所发起的请求数量; |
3.1高连接没法实验,因为我没有那么多的主机或虚拟机进行实验
3.2使用高请求进行试验:
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=30r/m; #先进行定义,req请求,
#zone区域限制请求的区域
#$binary_remote_addr客户端的地址
#zone=req_zone:10m限制的区域
#rate=30r/m;设置每分钟最大30次,相当于两秒统一访问一次
server{
listen 80 ;
server_name localhost;
location / {
limit_req zone=req_zone; #引用上面的定义,在哪个网站,就写在哪个网站下
root html;
index index.html index.htm;
}
}
怎么验证呢?
我们安装一个:yum -y install http-tools,然后开始测试
[root@bogon html]# ab -n 1000 -c 100 http://192.168.100.202/
Benchmarking 192.168.100.202 (be patient) #100个100个的发送
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Concurrency Level: 100
Time taken for tests: 0.069 seconds #用了0.069秒
Complete requests: 1000 #发送了1000个请求
Failed requests: 999 #接受了1个请求
4.nginx访问控制(拒绝某一个ip或网段)|(进入网站时需要用户密码验证)
4.1只拒绝一个ip或者网段
location / {
root html;
index index.html index.htm;
deny 192.168.100.222;
allow all; ####只拒绝192.168.100.222一个ip,其他全部允许
allow 192.168.100.0/24;
deny all; ####只允许一个网段,其他全部拒绝
######################记得systemctl restart nginx
http_access_module局限性(限制客户端ip)
4.2 让用户登录网站自动弹出账号密码
[root@bogon html]# htpasswd -c /usr/local/nginx/passwd/users zhangsan
###创建一个文件,里面专放用户的用户名和密码
###########然后编辑nginx.conf文件
location / {
auth_basic "Auth access Blog Input your Passwd!";
auth_basic_user_file /usr/local/nginx/passwd/users; ###指定用户和密码的文件在哪里
root html;
index index.html index.htm;
}
}
重启服务后就可以看到:
5.nginx进行反向代理
5.1 location和proxy_pass都带/,则真实地址不带location匹配目录
server {
listen 80;
server_name localhost;
location /api/ { #访问192.168.100.203下的api时
proxy_pass http://192.168.100.202/; #将把网页跳转到http://192.168.100.202/
root html;
index index.html index.htm;
}
http://192.168.100.203/api/upload/------->http://192.168.100.202:/upload/
5.2 location不带/,proxy_pass带/,则真实地址会带/
server {
listen 80;
server_name localhost;
location /api {
proxy_pass http://192.168.100.202/;
root html;
index index.html index.htm;
}
http://192.168.100.210/api/upload/------->http://127.0.0.1:8080//upload/http://192.168.100.203/api/upload/------->http://192.168.100.202://upload/
http://192.168.100.210/api/upload/------->http://127.0.0.1:8080//upload/
5.3 location带/,proxy_pass不带/,则真实地址会带location匹配目录/api/
server {
listen 80;
server_name localhost;
location /api/ {
proxy_pass http://192.168.100.202;
root html;
index index.html index.htm;
}
http://192.168.100.203/api/upload/------->http://192.168.100.202/api/upload/
5.4 location和proxy_pass都不带/,则真实地址会带location匹配目录/api/
server {
listen 80;
server_name localhost;
location /api {
proxy_pass http://192.168.100.202;
root html;
index index.html index.htm;
}
http://192.168.100.203/api/upload/------->http://192.168.100.202/api/upload/