nginx实验

nginx实验

一Nginx 架构和安装

1.Nginx 安装

准备需要的必要环境和软件

 dnf install gcc pcre-devel zlib-devel openssl-devel -y

导入nginx1.24.0压缩包,解压并编译

tar zxf nginx-1.24.0.tar.gz
useradd -s /sbin/nologin -M nginx

./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module

make && make install
验证版本及编译参数
vim ~/.bash_profile
export PATH=$PATH:/usr/local/nginx/sbin
source ~/.bash_profile
nginx -V

在这里插入图片描述

Nginx 启动文件
vim /lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start nginx
2.平滑升级和回滚

导入nginx1.26.0压缩包重复之前步骤

tar zxf nginx-1.26.1.tar.gz
./configure --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
make
#查看两个版本
ll objs/nginx /usr/local/nginx/sbin/nginx

在这里插入图片描述

#把之前的旧版的nginx命令备份
cd /usr/local/nginx/sbin/
cp nginx nginx.24

#把新版本的nginx命令复制过去
\cp -f /root/nginx-1.26.1/objs/nginx /usr/local/nginx/sbin

#检测一下有没有问题
nginx -t

kill -USR2 34076

#回收旧版本
kill -WINCH 34076
curl -I localhost

#回滚
#如果升级的版本发现问题需要回滚,可以重新拉起旧版本的worker
[root@Nginx sbin]# cp nginx nginx.26
[root@Nginx sbin]# ls
nginx nginx.24 nginx.26
[root@Nginx sbin]# mv nginx.24 nginx
mv: overwrite 'nginx'? y
[root@Nginx sbin]# kill -HUP 48732
[root@Nginx sbin]# ps aux | grep nginx
root 48732 0.0 0.1 9868 2436 ? Ss 14:17 0:00 nginx: master
process /usr/local/nginx/sbin/nginx
root 52075 0.0 0.3 9876 6528 ? S 15:41 0:00 nginx: master
process /usr/local/nginx/sbin/nginx
nobody 52076 0.0 0.2 14208 5124 ? S 15:41 0:00 nginx: worker
process
nobody 52130 0.0 0.2 14200 4868 ? S 16:30 0:00 nginx: worker
process
[root@Nginx sbin]# kill -WINCH 52075
[root@Nginx sbin]# ps aux | grep nginx
root 48732 0.0 0.1 9868 2436 ? Ss 14:17 0:00 nginx: master
process /usr/local/nginx/sbin/nginx
root 52075 0.0 0.3 9876 6528 ? S 15:41 0:00 nginx: master
process /usr/local/nginx/sbin/nginx
nobody 52130 0.0 0.2 14200 4868 ? S 16:30 0:00 nginx: worker
process
root 52137 0.0 0.1 221664 2176 pts/0 S+ 16:31 0:00 grep --
color=auto nginx

二.Nginx 核心配置详解

实现 nginx 的高并发配置
ulimit -n 102400
ab -c 5000 -n 10000 http://10.0.0.8/

vim /etc/security/limits.conf
* - nproc 100000
vim /apps/nginx/conf/nginx.conf
worker_rlimit_nofile 100000;
systemctl restart nginx
识别php文件为text/html
 vim /usr/local/nginx/html/ysy.php
<?php
phpinfo();
?>
curl -I 172.25.254.100/ysy.php
vim /usr/local/nginx/conf/nginx.conf
新建一个 PC web 站点
mkdir /usr/local/nginx/conf.d/
vim /usr/local/nginx/conf/nginx.conf
http {
......
include /usr/local/nginx/conf.d/*.conf;
}

vim /usr/local/nginx/conf.d/vhosts.conf
server {
  listen 80;
  server_name ysy.ysy.org;
    location / {
      root /webdata/nginx/ysy.org/ysy/html;
    }
}
mkdir -p /webdata/nginx/ysy.org/ysy/html
echo ysy.ysy.org > /webdata/nginx/ysy.org/ysy/html/index.html
nginx -s reload

在这里插入图片描述

root 与 alias

root

server {
listen 80;
server_name ysy.ysy.org;
location / {
root /webdata/nginx/ysy.org/ysy/html;
}
location /dirtest { 
root /mnt;
}
}

alias

server {
listen 80;
server_name ysy.ysy.org;
location / {
root /webdata/nginx/ysy.org/ysy/html;
}
location /dirtest {
root /mnt;
}
location /alias {
alias /mnt/dirtest;
}
}
location 的详细使用

location [ = | ~ | ~* | ^~ ] uri { … }

= #用于标准uri前,需要请求字串与uri精确匹配,大小敏感,如果匹配成功就停止向下匹配并立 即处理请求

^~ #用于标准uri前,表示包含正则表达式,并且匹配以指定的正则表达式开头 #对uri的最左边部分做匹配检查,不区分字符大小写
#用于标准uri前,表示包含正则表达式,并且区分大小写

~* #用于标准uri前,表示包含正则表达式,并且不区分大写

不带符号 #匹配起始于此uri的所有的uri

\ #用于标准uri前,表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号

匹配案例-区分大小写
server {
listen 80;
server_name lee.timinglee.org;
location / {
root /webdata/nginx/timinglee.org/lee/html;
}
location ~ /logo.PNG {
root /webdata/nginx/timinglee.org/lee/images;
}
}
匹配案例-不区分大小写
server {
listen 80;
server_name lee.timinglee.org;
location / {
root /webdata/nginx/timinglee.org/lee/html;
}
location ~* /logo.PNG {
root /webdata/nginx/timinglee.org/lee/images;
}
}
匹配案例-URI开始
server {
listen 80;
server_name lee.timinglee.org;
location / {
root /webdata/nginx/timinglee.org/lee/html;
}
location ^~ /images {
root /webdata/nginx/timinglee.org/lee/images;
index index.html;
}
location /images1 {
root /webdata/nginx/timinglee.org/lee/images;
}
}
匹配案例-文件名后缀
listen 80;
server_name lee.timinglee.org;
location / {
root /webdata/nginx/timinglee.org/lee/html;
}
location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js|css)$ {
root /webdata/nginx/timinglee.org/lee/images;
index index.html;
}
}
匹配案例-优先级
server {
listen 80;
server_name lee.timinglee.org;
location / {
root /webdata/nginx/timinglee.org/lee/html;
}
location ^~ /images {
root /webdata/nginx/timinglee.org/lee/images;
index index.html;
}
location /images1 {
root /webdata/nginx/timinglee.org/lee/images;
}
location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ {
root /data/nginx/static3;
index index.html;
}
}
生产使用案例
location = /index.html {
......;
}
location / {
......;
}
Nginx 账户认证功能

由 ngx_http_auth_basic_module 模块提供此功能

htpasswd -cmb /usr/local/nginx/conf/.htpasswd admin ysy
htpasswd -mb /usr/local/nginx/conf/.htpasswd ysy ysy
mkdir /webdata/nginx/ysy.org/ysy/login
echo login > /webdata/nginx/ysy.org/ysy/login/index.html
vim /usr/local/nginx/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location /login {
root /webdata/nginx/timinglee.org/lee;
index index.html;
auth_basic "login password";
auth_basic_user_file "/usr/local/nginx/conf/.htpasswd";
}
}

在这里插入图片描述

自定义错误页面

自 定义错误页,同时也可以用指定的响应状态码进行响应, 可用位置:http, server, location, if in location

error_page code ... [=[response]] uri;

示例:

listen 80;
server_name www.timinglee.org;
error_page 500 502 503 504 /error.html;
location = /error.html {
root /data/nginx/html;
}
mkdir /webdata/nginx/ysy.org/ysy/errors -p
echo error page > /webdata/nginx/ysy.org/ysy/errors/40x.html

自定义错误页面

自 定义错误页,同时也可以用指定的响应状态码进行响应, 可用位置:http, server, location, if in location

error_page code ... [=[response]] uri;

示例:

listen 80;
server_name www.timinglee.org;
error_page 500 502 503 504 /error.html;
location = /error.html {
root /data/nginx/html;
}
mkdir /webdata/nginx/ysy.org/ysy/errors -p
echo error page > /webdata/nginx/ysy.org/ysy/errors/40x.html

在这里插入图片描述

自定义错误日志
mkdir "/var/log/nginx" -p
vim /usr/local/nginx/conf.d/vhosts.conf
server {
listen 80;
server_name ysy.ysy.org;
error_page 404 /40x.html;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location = /40x.html {
root /webdata/nginx/ysy.org/ysy/errors;
}
}

在这里插入图片描述

检测文件是否存在
echo "index.html is not exist" > /webdata/nginx/timinglee.org/lee/error/default.html
vim /usr/local/nginx/conf.d/vhosts.conf
server {
listen 80;
server_name ysy.ysy.org;
root /webdata/nginx/ysy.org/ysy;
error_page 404 /40x.html;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
try_files $uri $uri.html $uri/index.html /error/default.html;
location = /40x.html {
root /webdata/nginx/ysy.org/ysy/errors;
}
}

在这里插入图片描述

长连接配置
telnet ysy.ysy.org 80
Trying 172.25.254.200...
Connected to lee.timinglee.org.
Escape character is '^]'.
GET / HTTP/1.1 ##输入动作
HOST: lee.timinglee.org ##输入访问HOST
##输入回车
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Sat, 20 Jul 2024 12:54:16 GMT
Content-Type: text/html
Content-Length: 15
Last-Modified: Sat, 20 Jul 2024 08:49:12 GMT
Connection: keep-alive
ETag: "669b7a08-f"
Accept-Ranges: bytes
172.25.254.200
GET / HTTP/1.1 #第二次操作
HOST: lee.timinglee.org #第二次操作
#第二次操作
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Sat, 20 Jul 2024 12:54:25 GMT
Content-Type: text/html
Content-Length: 15
Last-Modified: Sat, 20 Jul 2024 08:49:12 GMT
Connection: close
ETag: "669b7a08-f"
Accept-Ranges: bytes
172.25.254.200
Connection closed by foreign host. #自动断开链接
作为下载服务器配置
mkdir -p /webdata/nginx/ysy.org/ysy/download
cp /root/anaconda-ks.cfg /webdata/nginx/ysy.org/ysy/download
vim /usr/local/nginx/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
root /webdata/nginx/timinglee.org/lee;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
try_files $uri $uri.html $uri/index.html /error/default.html;
location = /40x.html {
root /webdata/nginx/timinglee/lee/errors;
}
location /download {
autoindex on; 
autoindex_exact_size on;
autoindex_localtime on;
limit_rate 1024k;
}
}

三.Nginx 高级配置

#配置示例:
location /nginx_status {
stub_status;
auth_basic "auth login";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
allow 192.168.0.0/16;
allow 127.0.0.1;
deny all;
}
#状态页用于输出nginx的基本状态信息:
#输出信息示例:
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
上面三个数字分别对应accepts,handled,requests三个值
Reading: 6 Writing: 179 Waiting: 106
Active connections: #当前处于活动状态的客户端连接数
#包括连接等待空闲连接数=reading+writing+waiting
accepts: #统计总值,Nginx自启动后已经接受的客户端请求连接的总数。
handled: #统计总值,Nginx自启动后已经处理完成的客户端请求连接总数
#通常等于accepts,除非有因worker_connections限制等被拒绝的
连接
requests: #统计总值,Nginx自启动后客户端发来的总的请求数
4.2 Nginx 压缩功能
Nginx支持对指定类型的文件进行压缩然后再传输给客户端,而且压缩还可以设置压缩比例,压缩后的文
件大小将比源文件显著变小,样有助于降低出口带宽的利用率,降低企业的IT支出,不过会占用相
应的CPU资源。
Nginx对文件的压缩功能是依赖于模块 ngx_http_gzip_module,默认是内置模块
配置指令如下:
示例:
Reading: #当前状态,正在读取客户端请求报文首部的连接的连接数
#数值越大,说明排队现象严重,性能不足
Writing: #当前状态,正在向客户端发送响应报文过程中的连接数,数值越大,说明
访问量很大
Waiting: #当前状态,正在等待客户端发出请求的空闲连接数
开启 keep-alive的情况下,这个值等于active –
(reading+writing)
Nginx 压缩功能
[root@Nginx ~]# mkdir /webdata/nginx/timinglee.org/lee/data
[root@Nginx ~]# cp /usr/local/nginx/logs/access.log
/webdata/nginx/timinglee.org/lee/data/data.txt
[root@Nginx ~]# echo test > /webdata/nginx/timinglee.org/lee/data/test.html #
小于1k的文件测试是否会压缩
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
@@@@省略内容@@@@
gzip on;
gzip_comp_level 5;
gzip_min_length 1k;
gzip_types text/plain application/javascript application/x-javascript text/css
application/xml text/javascript application/x-httpd-php image/gif image/png;
gzip_vary on;
#重启Nginx并访问测试:
[root@client ~]# curl --head --compressed lee.timinglee.org/data/test.html
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Sun, 21 Jul 2024 15:42:46 GMT
Content-Type: text/html
Content-Length: 5
Last-Modified: Sun, 21 Jul 2024 15:40:35 GMT
Connection: keep-alive
ETag: "669d2bf3-5"
Accept-Ranges: bytes
[root@client ~]# curl --head --compressed lee.timinglee.org/data/data.txt
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Sun, 21 Jul 2024 15:43:17 GMT
Content-Type: text/plain
Last-Modified: Sun, 21 Jul 2024 15:40:13 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: W/"669d2bdd-3e25b5"
Content-Encoding: gzip
Nginx的版本隐藏
[root@Nginx nginx-1.26.1]# vim src/core/nginx.h
#define nginx_version 1026001
#define NGINX_VERSION "1.0"
#define NGINX_VER "HAHA/" NGINX_VERSION
Nginx 变量使用
内置变量
$remote_addr;
#存放了客户端的地址,注意是客户端的公网IP
$args;
#变量中存放了URL中的所有参数
#例如:https://search.jd.com/Search?keyword=手机&enc=utf-8
#返回结果为: keyword=手机&enc=utf-8
$is_args
#如果有参数为? 否则为空
$document_root;
#保存了针对当前资源的请求的系统根目录,例如:/webdata/nginx/timinglee.org/lee。
$document_uri;
#保存了当前请求中不包含参数的URI,注意是不包含请求的指令
#比如:http://lee.timinglee.org/var?\id=11111会被定义为/var
#返回结果为:/var
$host;
#存放了请求的host名称
limit_rate 10240;
echo $limit_rate;
#如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没有设置, 则显示0
$remote_port;
#客户端请求Nginx服务器时随机打开的端口,这是每个客户端自己的端口
$remote_user;
#已经经过Auth Basic Module验证的用户名
$request_body_file;
#做反向代理时发给后端服务器的本地资源的名称
$request_method;
示例:
#请求资源的方式,GET/PUT/DELETE等
$request_filename;
#当前请求的资源文件的磁盘路径,由root或alias指令与URI请求生成的文件绝对路径,
#如:webdata/nginx/timinglee.org/lee/var/index.html
$request_uri;
#包含请求参数的原始URI,不包含主机名,相当于:$document_uri?$args,
#例如:/main/index.do?id=20190221&partner=search
$scheme;
#请求的协议,例如:http,https,ftp等
$server_protocol;
#保存了客户端请求资源使用的协议的版本,例如:HTTP/1.0,HTTP/1.1,HTTP/2.0等
$server_addr;
#保存了服务器的IP地址
$server_name;
#虚拟主机的主机名
$server_port;
#虚拟主机的端口号
$http_user_agent;
#客户端浏览器的详细信息
$http_cookie;
#客户端的所有cookie信息
$cookie_<name>
#name为任意请求报文首部字部cookie的key名
$http_<name>
#name为任意请求报文首部字段,表示记录请求报文的首部字段,ame的对应的首部字段名需要为小写,如果有
横线需要替换为下划线
#示例:
echo $http_user_agent;
echo $http_host;
$sent_http_<name>
#name为响应报文的首部字段,name的对应的首部字段名需要为小写,如果有横线需要替换为下划线,此变量有
问题
echo $sent_http_server;
$arg_<name>
#此变量存放了URL中的指定参数,name为请求url中指定的参数
echo $arg_id;
[root@Nginx ~]# vim /usr/local/nginx/conf.d/vhosts.conf
server {
listen 80;
server_name ysy.ysy.org;
root /webdata/nginx/ysy.org/ysy;
location /var {
default_type text/html;
echo $remote_addr;
echo $args;
echo $document_root;
echo $document_uri;
echo $host;
echo $http_user_agent;
echo $request_filename;
echo $scheme;
echo $scheme://$host$document_uri?$args;
echo $http_cookie;
echo $cookie_key2;
echo $http_Accept;
}
}
[root@client ~]# curl -b "title=ysy;key1=ysy1,key2=ysy2"
"ysy.ysy.org/var?search=lee&&id=666666"
172.25.254.20
search=lee&&id=666666
/webdata/nginx/ysy.org/ysy
/var
lee.timinglee.org
curl/7.29.0
/webdata/nginx/ysy.org/ysy/var
http
http://lee.timinglee.org/var?search=lee&&id=666666
title=lee;key1=lee,key2=timinglee
ysy
*/*
自定义变量
set $name ysy;
echo $name;
set $my_port $server_port;
echo $my_port;
echo "$server_name:$server_port";
[root@Nginx ~]# vim /usr/local/nginx/conf.d/vhosts.conf
server {
listen 80;
server_name ysy.ysy.org;
root /webdata/nginx/ysy.org/ysy;
location /var {
default_type text/html;
set $name ysy;
echo $name;
set $web_port $server_port;
echo $web_port;
}
}
测试输出
[root@client ~]# curl ysy.ysy.org/var
ysy
80
Nginx Rewrite 相关功能
if 指令
location /test {
index index.html;
default_type text/html;
if ( $scheme = http ){
echo "if ---------> $scheme";
}
if ( $scheme = https ){
echo "if ---------> $scheme";
}
}
location /test2 {
if ( !-e $request_filename ){
echo "$request_filename is not exist";
return 409}
}
测试:
curl ysy.ysy.org/test/
if ---------> http
curl ysy.ysy.org/test2/test
/webdata/nginx/ysy.org/ysy/test2/test is not exist
set 指令
vim /usr/local/nginx/conf.d/vhosts.conf
server {
listen 80;
server_name ysy.ysy.org;
root /webdata/nginx/ysy.org/ysy;
location /test3{
set $name ysy;
echo $name;
}
}
测试:
curl ysy.ysy.org/test3
ysy
break 指令
vim /usr/local/nginx/conf.d/vhosts.conf
server {
listen 80;
server_name ysy.ysy.org;
root /webdata/nginx/ysy.org/ysy;
location /break{
default_type text/html;
set $name ysy;
echo $name;
break;
set $port $server_port;
echo $port;
}
}
curl ysy.ysy.org/break
ysy
return 指令
server {
listen 80;
server_name ysy.ysy.org;
root /webdata/nginx/ysy.org/ysy;
location /return {
default_type text/html;
if ( !-e $request_filename){
return 301 http://www.baidu.com;
#return 666 "$request_filename is not exist";
}
echo "$request_filename is exist";
}
}
curl ysy.ysy.org/return
/webdata/nginx/ysy.org/ysy/return is exist
curl ysy.ysy.org/return1
/webdata/nginx/ysy.org/ysy/return1 is not exist

其余具体见pdf

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值