nginx全方位讲解

news2024/11/13 9:51:59

安装nginx

[root@nginx ~]# tar zxf nginx-1.24.0.tar.gz   
[root@nginx ~]# cd nginx-1.24.0/
[root@nginx nginx-1.24.0]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
[root@nginx nginx-1.24.0]# dnf install gcc pcre-devel zlib-devel openssl-devel -y
[root@nginx  nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_ssl_module \
> --with-http_v2_module \
> --with-http_realip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --with-pcre \
> --with-stream \
> --with-stream_ssl_module 


[root@nginx  nginx-1.24.0]#./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
[root@nginx nginx-1.24.0]# make && make install
[root@nginx nginx-1.24.0]# ls /usr/local/nginx/
conf  html  logs  sbin
[root@nginx nginx]# vim ~/.bash_profile
export PATH=$PATH:/usr/local/nginx/sbin
[root@nginx nginx]# source ~/.bash_profile
[root@nginx nginx]# nginx

 平滑升级和回滚

[root@nginx ~]# ls
anaconda-ks.cfg  echo-nginx-module-0.63.tar.gz  nginx-1.24.0  nginx-1.24.0.tar.gz  nginx-1.26.1  nginx-1.26.1.tar.gz
[root@nginx ~]# tar zxf nginx-1.26.1.tar.gz 
[root@nginx ~]# tar zxf echo-nginx-module-0.63.tar.gz 
[root@nginx ~]# ls
anaconda-ks.cfg                nginx-1.24.0         nginx-1.26.1.tar.gz
echo-nginx-module-0.63         nginx-1.24.0.tar.gz
echo-nginx-module-0.63.tar.gz  nginx-1.26.1
​
[root@nginx ~]# cd nginx-1.26.1
[root@nginx nginx-1.26.1]# ./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 --add-module=/root/echo-nginx-module-0.63 #添加echo模块
[root@nginx nginx-1.26.1]# make 
[root@nginx objs]# cd /usr/local/nginx/sbin/
[root@nginx sbin]# nginx -s stop
[root@nginx ~]# rm -rf /usr/local/nginx/
[root@nginx ~]# cd /root/nginx-1.24.0/
[root@nginx nginx-1.24.0]# ls
auto        conf       html      man     src
CHANGES     configure  LICENSE   objs
CHANGES.ru  contrib    Makefile  README
[root@nginx nginx-1.24.0]# make install
​
}
​
#先把nginx备份
[root@nginx sbin]# cp nginx nginx.old
[root@nginx sbin]# ls
nginx  nginx.old
​
#把新版本的nginx命令复制过去
[root@nginx sbin]# \cp -f /root/nginx-1.26.1/objs/nginx /usr/local/nginx/sbin
[root@nginx sbin]# killall -9 nginx
[root@nginx sbin]# curl -I 172.25.254.100
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Mon, 19 Aug 2024 23:05:39 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Fri, 16 Aug 2024 13:02:49 GMT
Connection: keep-alive
ETag: "66bf4df9-267"
Accept-Ranges: bytes
[root@nginx sbin]# curl -I 172.25.254.100
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Mon, 19 Aug 2024 23:06:30 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Fri, 16 Aug 2024 13:02:49 GMT
Connection: keep-alive
ETag: "66bf4df9-267"
Accept-Ranges: bytes
 回滚
[root@nginx sbin]# killall -9 nginx
#此时已经回滚成功
[root@nginx sbin]# curl -I 172.25.254.100
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Mon, 19 Aug 2024 23:07:38 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Fri, 16 Aug 2024 13:02:49 GMT
Connection: keep-alive
ETag: "66bf4df9-267"
Accept-Ranges: bytes

 nginx的启动文件

[root@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
[root@Nginx ~]# systemctl daemon-reload
[root@Nginx ~]# systemctl start nginx

 Nginx 核心配置详解

[root@nginx ~]# mkdir -p /usr/local/nginx/conf.d
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {
    listen 80;
    server_name www.timinglee.org;
    root /data/web/html;
    index index.html;
}
​
[root@nginx ~]# mkdir -p /data/web/html
[root@nginx ~]# echo www.jingwen.org > /data/web/html/index.html
[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx ~]# nginx -s reload
#查看日志
[root@nginx ~]# tail -f /usr/local/nginx/logs/error.log 
[root@nginx ~]# mkdir /data/web/test1 -p
​

[root@nginx ~]# vi /usr/local/nginx/conf.d/vhost.conf 
server {
    listen 80;
    server_name www.timinglee.org;
    root /data/web/html;
    index index.html;
    location /test1/ {
        root /data/web;
    }
}
[root@nginx ~]# echo hahaha > /data/web/test1/index.html
[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx ~]# nginx -s reload
[root@nginx ~]# cat /usr/local/nginx/conf.d/vhost.conf 
server {
    listen 80;
    server_name www.timinglee.org;
    root /data/web/html;
    index index.html;
    location /test1/ {
        root /data/web;
    }
    location /test2 {
        alias /data/web/test1;
    }
}
[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local
#判断优先级


[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx ~]# nginx -s reload



[root@nginx ~]# mkdir -p /data/web{1..5}
[root@nginx ~]# mkdir -p /data/web{1..5}/test
[root@nginx ~]# echo web1 > /data/web1/test/index.html
[root@nginx ~]# echo web2 > /data/web2/test/index.html
[root@nginx ~]# echo web3 > /data/web3/test/index.html
[root@nginx ~]# echo web4 > /data/web4/test/index.html
[root@nginx ~]# echo web5 > /data/web5/test/index.html
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf



server {
listen 80;
server_name lee.timinglee.org;
root /data/web/html;
index index.html;
#location /test1 {
#       root /data/web;
#}
#location /test2 {
#        alias /data/web/test1;
#}
location /test {
        root /data/web1;
}

location = /test {
        root /data/web2;
}
location ^~ /t {
        root /data/web3;
}

location  ~* .html$ {
        root /data/web5;
}

location  ~.html$ {
        root /data/web4;
}

}
#自定义错误日志
[root@nginx ~]# mkdir /var/log/timinglee.org
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {
    error_log /var/log/timinglee.org/error.log;
    access_log /var/log/timinglee.org/access.log;
}
​
[root@nginx ~]# nginx -s reload
​
#测试
[root@nginx ~]# curl www.timinglee.org
[root@nginx ~]# cat /var/log/timinglee.org/access.log
[root@nginx ~]# curl www.timinglee.org/aaa
[root@nginx ~]# cat /var/log/timinglee.org/error.log
#检测文件是否存在
[root@nginx ~]# rm -rf /data/web/html/index.html
[root@nginx ~]# rm -rf /data/web/html/error/
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {
    error_log /var/log/timinglee.org/error.log;
    access_log /var/log/timinglee.org/access.log;
    try_files $uri $uri.html $uri/index.html /error/default.html;
​
}
[root@nginx ~]# nginx -s reload;
​
#测试
[root@nginx ~]# curl www.timinglee.org
500
​
[root@nginx ~]# mkdir /data/web/html/error
[root@nginx ~]# echo error default > /data/web/html/error/default.html
​
#测试
[root@nginx ~]# curl www.timinglee.org
error default



#长链接
[root@nginx ~]# echo www.timinglee.org > /data/web/html/index.html
​
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
http {
    keepalive_timeout 65;
    keepalive_requests 2;
}
[root@nginx ~]# nginx -s reload
​
# 长链接测试工具
[root@nginx ~]#dnf install telnet -y
# 测试
[root@nginx ~]#telnet www.timinglee.org 80
GET / HTTP/1.1
Host: www.timinglee.org
​
Server: nginx/1.24.0
Date: Fri, 16 Aug 2024 06:45:42 GMT
Content-Type: text/html
Content-Length: 18
Last-Modified: Fri, 16 Aug 2024 06:45:22 GMT
Connection: keep-alive
ETag: "66bef582-12"
Accept-Ranges: bytes
​
www.timinglee.org

#下载工具
[root@Nginx ~]# mkdir /data/web/download
[root@Nginx ~]# dd if=/dev/zero of=/data/web/download/leefile bs=1M count=100
​
[root@Nginx ~]# vim /usr/local/nginx/conf.d/vhosts.conf
server {
​
    location /download {
        autoindex on; #自动索引功能
        autoindex_exact_size on; 
        autoindex_localtime on;
        limit_rate 1024k; #限速,默认不限速 
    }
}
​
#重启Nginx并访问测试下载页面
http://www.timinglee.org/download/
#nginx压缩
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
http {
    gzip on;
    gzip_comp_level 5;
    gzip_min_length 1k;
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_types text/plain application/javascript application/x-javascript text/css
application/xml text/javascript application/x-httpd-php image/gif image/png;
}
[root@nginx ~]# nginx -s reload
​
[root@nginx ~]# echo hello timinglee >  /data/web/html/small.html
[root@nginx ~]# du -sh /usr/local/nginx/loges/access.log   #查看这个文件多大
[root@nginx ~]# cat /usr/local/nginx/loges/access.log > /data/web/html/big.html
​
​
#测试,查看是否被压缩
[root@nginx ~]# curl --head --compressed 172.25.254.100/small.html
[root@nginx ~]# curl --head --compressed 172.25.254.100/big.html

#nginx 环境变量


#nginx的内置变量
​
server {
    listen 80;
    server_name var.timinglee.org;
    root /data/web/html;
    index index.html;
​
    location /var {
        default_type text/html;
        echo $remote_addr;
        echo $args;
        echo $is_args;
        echo $document_root;
        echo $document_uri;
        echo $host;
        echo $remote_port;
        echo $remote_user;
        echo $request_method;
        echo $request_filename;
        echo $request_uri;
        echo $scheme;
        echo $server_protocol;
        echo $server_addr;
        echo $server_name;
        echo $server_port;
        echo $http_user_agent;
        echo $http_cookie;
        echo $cookie_key2;
    }
}
​
​
#nginx自定义变量
server {
    listen 80;
    server_name var.timinglee.org;
    root /data/web/html;
    index index.html;
​
    location /var {
        default_type text/html;
        set $timinglee lee;
        echo $timinglee;
    }
}
​
​
# 测试
curl -b "key1=lee,key2=lee1" -u lee:lee var.timinglee.org/var?name=lee&&id=6666
​

Nginx Rewrite相关功能

#if判定
[root@nginx ~]# mkdir /data/web/html/test2
[root@nginx ~]# echo test2 > /data/web/html/test2/index.html
​
[root@nginx ~]# vim /location /test2 {
        if ( !-e $request_filename ){
            echo "$request_filename is not exist";
        }
    }
/local/nginx/conf.d/vars.conf
​
[root@nginx ~]# nginx -s reload
​
#测试
[root@nginx ~]# curl var.timinglee.org/test2/index.html 
test2
​
#break指令
[root@nginx ~]# vim /usr/local/nginx/conf.d/vars.conf
 location /break {
        default_type text/html;
        set $name lee;
        echo $name;
        if ( $http_user_agent = "curl/7.76.1" ){
            break;
        }
        set $id 666;
        echo $id;
    }
[root@nginx ~]# nginx -s reload
​
#测试
[root@nginx ~]# curl  var.timinglee.org/break
lee
3、return指令
[root@nginx ~]# vim /usr/local/nginx/conf.d/vars.conf
   location /return {
        default_type text/html;
        if ( !-e $request_filename){
            return 301 http://www.baidu.com;
        }
        echo "$request_filename is exist";
    }
​
[root@nginx ~]# nginx -s reload
​
​
#测试
#return不存在时
[root@nginx ~]# ll /data/web/html
total 24
-rw-r--r-- 1 root root 15423 Aug 16 16:16 big.html
drwxr-xr-x 2 root root    26 Aug 16 14:34 error
-rw-r--r-- 1 root root    18 Aug 18 11:04 index.html
-rw-r--r-- 1 root root    16 Aug 16 16:15 small.html
drwxr-xr-x 2 root root    24 Aug 18 11:45 test2
drwxr-xr-x 2 root root    24 Aug 18 10:41 var
[root@nginx ~]# curl -I var.timinglee.org/return
HTTP/1.1 301 Moved Permanently
Server: nginx/1.26.1
Date: Sun, 18 Aug 2024 03:54:09 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: http://www.baidu.com
[root@nginx ~]# mkdir  -p /data/web/html/return
[root@nginx ~]# curl -I var.timinglee.org/return
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Sun, 18 Aug 2024 03:54:33 GMT
Content-Type: text/html
Connection: keep-alive
​
[root@nginx ~]# curl var.timinglee.org/return
/data/web/html/return is exist


#临时和永久
# 前期准备
[root@nginx ~]# vim /usr/local/nginx/conf.d/vars.conf
server {
    listen 80;
    server_name var.timinglee.org;
    root /data/web/html;
    index index.html;
    location / {
        root /data/web/var;
        index index.html;
        #rewrite / http://www.timinglee.com permanent;
        #rewrite / http://www.timinglee.com redirect;
    }
}
[root@nginx ~]# nginx -s reload
[root@nginx ~]# echo var page > /data/web/var/index.html
[root@nginx conf.d]# curl www.timinglee.org
www.timinglee.com
[root@nginx conf.d]# curl var.timinglee.org
var page
​
​
​
# 永久重定向301
[root@nginx ~]# vim /usr/local/nginx/conf.d/vars.conf
location / {
        root /data/web/var;
        index index.html;
        rewrite / http://www.timinglee.com permanent;
        #rewrite / http://www.timinglee.com redirect;
    }
[root@nginx ~]# nginx -s reload
​
[root@nginx conf.d]# curl -I var.timinglee.org
HTTP/1.1 301 Moved Permanently
Server: nginx/1.26.1
Date: Sun, 18 Aug 2024 12:13:09 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: http://www.timinglee.com
​
#临时重定向302
[root@nginx ~]# vim /usr/local/nginx/conf.d/vars.conf
location / {
        root /data/web/var;
        index index.html;
        #rewrite / http://www.timinglee.com permanent;
        rewrite / http://www.timinglee.com redirect;
    }
[root@nginx ~]# nginx -s reload
​
[root@nginx conf.d]# curl -I var.timinglee.org
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.26.1
Date: Sun, 18 Aug 2024 12:13:41 GMT
Content-Type: text/html
Content-Length: 145
Connection: keep-alive
Location: http://www.timinglee.com
# break 和last
[root@nginx ~]# mkdir  /data/web/html/{test1,test2,break,last} -p[root@nginx ~]# echo test1 > /data/web/html/test1/index.html
[root@nginx ~]# echo test2 > /data/web/html/test2/index.html
[root@nginx ~]# echo last > /data/web/html/last/index.html
[root@nginx ~]# echo break > /data/web/html/break/index.html
[root@nginx ~]# vim /usr/local/nginx/conf.d/vars.conf
server {
    listen 80;
    server_name var.timinglee.org;
    root /data/web/html;
    index index.html;
​
    location /break {
        root /data/web/html;
        rewrite ^/break/(.*) /test1/$1 break;
        rewrite ^/test1/(.*) /test1/$2;
    }
    location /last {
        root /data/web/html;
        rewrite ^/last/(.*) /test1/$1 last;
        rewrite ^/test1/(.*) /test2/$1;
    }
    location /test1 {
        default_type test/html;
        return 666 "test1 hahahahahaha ";
    }
    location /test2 {
        root /data/web/html;
    }
}
​
[root@nginx ~]# nginx -s reload
​
#测试
[root@nginx ~]# curl var.timinglee.org/break/index.html
test1
[root@nginx ~]# curl var.timinglee.org/last/index.html
test1 hahahahahaha 


#加密
cd /usr/local/nginx/
mkdir certs
openssl req  -newkey  rsa:2048 -nodes -sha256 -keyout /usr/local/nginx/certs/timinglee.org.key \ -x509  -days 365 -out /usr/local/nginx/certs/timinglee.org.crt
[root@nginx ~]# cd /usr/local/nginx/certs/
[root@nginx certs]# ls
timinglee.org.crt  timinglee.org.key
[root@nginx certs]# cd ..
[root@nginx nginx]# cd conf.d/
[root@nginx conf.d]# vim vhosts.conf 
server {
    listen 80;
    listen 443 ssl;
    server_name www.timinglee.org;
    root /data/web/html;
    index index.html;
    ssl_certificate /usr/local/nginx/certs/timinglee.org.crt;
    ssl_certificate_key /usr/local/nginx/certs/timinglee.org.key;
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
 
}
[root@nginx conf.d]# nginx -s reload
[root@nginx conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful


#盗链的实现
[root@nginx ~]# mkdir -p /data/web/html/images
[root@nginx ~]# mv daolian.png /data/web/html
[root@nginx ~]# mv lee.png /data/web/html/images/
​
#node1
[root@node1 ~]# dnf install httpd -y
[root@node1 ~]# cd /var/www/html
[root@node1 html]# vim index.html
<html>
<head>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<title>盗链</title>
</head>
<body>
<img src="http://www.timinglee.org/images/lee.png" >
<h1 style="color:red">欢迎大家</h1>
<p><a href=http://www.timinglee.org>狂点老李</a>出门见喜</p>
</body>
</html>
# 防盗链
[root@nginx ~]# vim /usr/local/nginx/conf.d/vars.conf
server {
    listen 80;
    listen 443 ssl;
    server_name www.timinglee.org;
    root /data/web/html;
    index index.html;
    ssl_certificate /usr/local/nginx/certs/timinglee.org.crt;
    ssl_certificate_key /usr/local/nginx/certs/timinglee.org.key;
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
​
    location /images {
        valid_referers none blocked server_names *.timinglee.org ~/.baidu/.;
        if ( $invalid_referer ) {
            return 404;
        }
    }
}
[root@nginx ~]# nginx -s reload

HTTP反向代理
# node1、node2
[root@node1 ~]# dnf install httpd -y
[root@node1 ~]# systemctl enable --now httpd
[root@node1 ~]# echo 172.25.254.10 > /var/www/html/index.html
[root@node2 ~]# systemctl enable --now httpd
[root@node2 ~]# echo 172.25.254.20 > /var/www/html/index.html
[root@node2 ~]# vim /etc/httpd/conf/httpd.conf
Listen 8080
[root@node2 ~]# systemctl restart httpd
​
​
# 测试
[root@nginx ~]# curl 172.25.254.10
172.25.254.10
[root@nginx ~]# curl 172.25.254.20
172.25.254.20
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {
    listen 80;
    server_name www.timinglee.org;
​
    location / {
        proxy_pass http://172.25.254.10:80;
    }
​
    location /static {
        proxy_pass http://172.25.254.20:8080;
    }
}
​
[root@nginx ~]# nginx -s reload
​
#测试
[root@nginx ~]# curl www.timinglee.org
172.25.254.10
[root@nginx ~]# curl www.timinglee.org/static/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
</body></html>
(node2没有static目录)
​
​
[root@node2 ~]# mkdir -p /var/www/html/static
[root@node2 ~]# echo static 172.25.254.20 > /var/www/html/static/index.html
​
#测试
[root@nginx ~]# curl www.timinglee.org
172.25.254.10
[root@nginx ~]# curl www.timinglee.org/static/
static 172.25.254.20





#缓存功能
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
#gzip  on;    
proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2:2 keys_zone=proxycache:20m inactive=120s max_size=1g;
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
location /static {
        proxy_pass http://172.25.254.20:8080;
        proxy_cache proxycache;
        proxy_cache_key $request_uri;
        proxy_cache_valid 200 302 301 10m;
        proxy_cache_valid any 1m;
    }
[root@nginx ~]# nginx -s reload
[root@node1 ~]# vim /etc/hosts
​
172.25.254.10   node1.timinglee.org www.timinglee.org
​
#测试
[root@node1 ~]# ab -n1000 -c100 http://www.timinglee.org/static/index.html
Requests per second:    8908.92 [#/sec] (mean) (未做缓存)
Requests per second:    12933.77 [#/sec] (mean)


#http反向代理负载均衡
#反向代理示例:后端多台web服务器
#机器
172.25.254.100 #Nginx 代理服务器
172.25.254.10 #后端web A,Apache部署
172.25.254.20 #后端web B,Apache部署
# 部署后端 Apache服务器
[root@node1 ~]# dnf install httpd -y
[root@node1 ~]# echo "172.25.254.10" > /var/www/html/index.html
[root@node1 ~]# systemctl enable --now httpd
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
upstream webcluster {
    server 172.25.254.10:80 fail_timeout=15s max_fails=3;
    server 172.25.254.20:8080 fail_timeout=15s max_fails=3;
    server 172.25.254.100:80 backup;
}
​
server {
    listen 80;
    server_name www.timinglee.org;
​
    location / {
        proxy_pass http://webcluster;
    }
​
}
​
[root@nginx ~]# nginx -s reload
​
#测试
[root@nginx ~]# curl www.timinglee.org
172.25.254.10
[root@nginx ~]# curl www.timinglee.org
172.25.254.20
upstream webcluster {
    ip_hash;
    server 172.25.254.10:80 fail_timeout=15s max_fails=3;
    server 172.25.254.20:8080 fail_timeout=15s max_fails=3;
    #server 172.25.254.100:80 backup;
}
[root@nginx ~]# curl www.timinglee.org
172.25.254.10
[root@nginx ~]# curl www.timinglee.org
172.25.254.10
upstream webcluster {
    #ip_hash;
    hash $request_uri consistent;
    server 172.25.254.10:80 fail_timeout=15s max_fails=3;
    server 172.25.254.20:8080 fail_timeout=15s max_fails=3;
    #server 172.25.254.100:80 backup;
}
[root@nginx ~]# curl www.timinglee.org
172.25.254.20
[root@nginx ~]# curl www.timinglee.org
172.25.254.20
​
# 基于Cookie 实现会话绑定
upstream webcluster {
    #ip_hash;
    #hash $request_uri consistent;
    hash $cookie_lee;
    server 172.25.254.10:80 fail_timeout=15s max_fails=3;
    server 172.25.254.20:8080 fail_timeout=15s max_fails=3;
    #server 172.25.254.100:80 backup;
}
​
​
[root@nginx ~]# curl www.timinglee.org
172.25.254.10
[root@nginx ~]# curl www.timinglee.org
172.25.254.20
[root@nginx ~]# curl -b "lee=1" www.timinglee.org
172.25.254.10
[root@nginx ~]# curl -b "lee=1" www.timinglee.org
172.25.254.10
#Nginx四层负载均衡
#DNS
# 环境 node1\node2
[root@node1 ~]# dnf install bind -y
[root@node1 ~]# vim /etc/named.conf 
//      listen-on port 53 { 127.0.0.1; };
//      listen-on-v6 port 53 { ::1; };
//      allow-query     { localhost; };
​
dnssec-validation no;
[root@node1 ~]# vim /etc/named.rfc1912.zones 
zone "timinglee.org" IN {
        type master;
        file "timinglee.org.zone";
        allow-update { none; };
};
​
[root@node1 ~]# cd /var/named
[root@node1 named]# cp named.localhost timinglee.org.zone -p
[root@node1 named]# vim timinglee.org.zone
$TTL 1D
@       IN SOA  ns.timinglee.org root.timinglee.org. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      ns.timinglee.org.
NS      A       172.25.254.10
www     A       172.25.254.10
​
[root@node1 named]# systemctl start named
[root@node1 named]# dig www.timinglee.org @172.25.254.10
[root@node1 named]# scp -p /etc/named.{conf,rfc1912.zones} root@172.25.254.20:/etc/
[root@node1 named]# scp -p /var/named/timinglee.org.zone root@172.25.254.20:/var/named/timinglee.org.zone
[root@node2 named]# chgrp named timinglee.org.zone 
[root@node2 named]# systemctl restart named
[root@node2 named]# dig www.timinglee.org @172.25.254.20
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
include "/usr/local/nginx/tcpconf.d/*.conf";
[root@nginx ~]# mkdir -p /usr/local/nginx/tcpconf.d
[root@nginx ~]# vim /usr/local/nginx/tcpconf.d/dns.conf 
stream {
    upstream dns {
        server 172.25.254.10:53 fail_timeout=15s max_fails=3;
        server 172.25.254.20:53 fail_timeout=15s max_fails=3;
    }
​
    server {
        listen 53 udp reuseport;
        proxy_timeout 20s;
        proxy_pass dns;
    }
}
[root@nginx ~]# nginx -s reload
​
​
#测试
[root@nginx ~]# dig www.timinglee.org @172.25.254.100
​
; <<>> DiG 9.16.23-RH <<>> www.timinglee.org @172.25.254.100
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 28013
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
​
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: b7555ae27da74d590100000066c216da67395c8bb9e26570 (good)
;; QUESTION SECTION:
;www.timinglee.org.     IN  A
​
;; ANSWER SECTION:
www.timinglee.org.  86400   IN  A   172.25.254.10
​
[root@nginx ~]# dig www.timinglee.org @172.25.254.100
; <<>> DiG 9.16.23-RH <<>> www.timinglee.org @172.25.254.100
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 56262
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
​
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 2a17fdf418eb234d0100000066c216e6f94e6503ec327810 (good)
;; QUESTION SECTION:
;www.timinglee.org.     IN  A
​
;; ANSWER SECTION:
www.timinglee.org.  86400   IN  A   172.25.254.20
​
#数据库
# node1\node2
[root@node1 ~]# dnf install mariadb-server -y
[root@node1 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=10
[root@node1 ~]# systemctl start mariadb.service 
# 授权
MariaDB [(none)]> CREATE USER lee@'%' identified by 'lee';
Query OK, 0 rows affected (0.002 sec)
​
MariaDB [(none)]> GRANT ALL ON *.* to lee@'%';
Query OK, 0 rows affected (0.001 sec)
[root@nginx ~]# vim /usr/local/nginx/tcpconf.d/dns.conf 
stream {
    upstream dns {
        server 172.25.254.10:53 fail_timeout=15s max_fails=3;
        server 172.25.254.20:53 fail_timeout=15s max_fails=3;
    }
    upstream mysql {
        server 172.25.254.10:3306 fail_timeout=15s max_fails=3;
        server 172.25.254.20:3306 fail_timeout=15s max_fails=3;
​
    }
​
    server {
        listen 3306;
        proxy_timeout 60s;
        proxy_pass mysql;
    }
​
    server {
        listen 53 udp reuseport;
        proxy_timeout 20s;
        proxy_pass dns;
    }
}
[root@nginx ~]# nginx -s reload
[root@nginx ~]# dnf install mariadb -y #下载数据库的客户端
​
​
# 测试
[root@nginx ~]# mysql -u lee -p -h 172.25.254.100
lee
MariaDB [(none)]> SELECT @@server_id
    -> ;
+-------------+
| @@server_id |
+-------------+
|          20 |
+-------------+
1 row in set (0.001 sec)
[root@nginx ~]# mysql -u lee -p -h 172.25.254.100
lee
MariaDB [(none)]> SELECT @@server_id;
+-------------+
| @@server_id |
+-------------+
|          10 |
+-------------+
1 row in set (0.002 sec)
​
#实现FastCGI
为什么会有FastCGI?

CGI协议虽然解决了语言解析器和 Web Server 之间通讯的问题,但是它的效率很低,因为 Web Server每收到一个请求都会创建一个CGI进程,PHP解析器都会解析php.ini文件,初始化环境,请求结束的时候再关闭进程,对于每一个创建的CGI进程都会执行这些操作,所以效率很低,而FastCGI是用来提高CGI性能的,FastCGI每次处理完请求之后不会关闭掉进程,而是保留这个进程,使这个进程可以处理多个请求。这样的话每个请求都不用再重新创建一个进程了,大大提升了处理效率

什么是PHP-FPM?PHP-FPM(FastCGI Process Manager:

FastCGI进程管理器)是一个实现了Fastcgi的程序,并且提供进程管理的功能。

进程包括master进程和worker进程。master进程只有一个,负责监听端口,接受来自web server

的请求

worker进程一般会有多个,每个进程中会嵌入一个PHP解析器,进行PHP代码的处理。

 Nginx与php-fpm在同一服务器

[root@nginx php-8.3.9]# ./configure --prefix=/usr/local/php \
>--with-config-file-path=/usr/local/php/etc \
> --enable-fpm \
> --with-fpm-user=nginx \
> --with-fpm-group=nginx \
> --with-curl \
> --with-iconv \
> --with-mhash \
> --with-zlib \
> --with-openssl \
> --enable-mysqlnd \
> --with-mysqli \
> --with-pdo-mysql \
> --disable-debug \
> --enable-sockets \
> --enable-soap \
> --enable-xml \
> --enable-ftp \
> --enable-gd \
> --enable-exif \
> --enable-mbstring \
> --enable-bcmath \
> --with-fpm-systemd

[root@nginx php-8.3.9]#./configure --prefix=/usr/local/php  --with-config-file-path=/usr/local/php/etc --with-fpm-user=nginx --with-fpm-group=nginx --with-curl --with-iconv --with-mhash --with-zlib --with-openssl --enable-mysqlnd --with-mysqli --with-pdo-mysql --disable-debug --enable-sockets --enable-soap --enable-xml --enable-ftp --enable-gd --enable-exif --enable-mbstring --enable-bcmath --with-fpm-systemd

[root@nginx php-8.3.9]# make && make install
[root@Nginx ~]# cd /usr/local/php/etc
[root@Nginx etc]# cp php-fpm.conf.default php-fpm.conf
[root@Nginx etc]# vim php-fpm.conf
去掉注释
pid = run/php-fpm.pid #指定pid文件存放位置
[root@Nginx etc]# cd php-fpm.d/
[root@Nginx php-fpm.d]# cp www.conf.default www.conf
#生成主配置文件
[root@Nginx php-fpm.d]# cd /root/php-8.3.9/
[root@Nginx php-8.3.9]# cp php.ini-production /usr/local/php/etc/php.ini
[root@Nginx ~]# vim /usr/local/php/etc/php.ini
[Date]
; Defines the default timezone used by the date functions
; https://php.net/date.timezone
date.timezone = Asia/Shanghai #修改时区

#生成启动文件
[root@Nginx ~]# cd /root/php-8.3.9/
[root@Nginx php-8.3.9]# cp sapi/fpm/php-fpm.service /lib/systemd/system/
[root@nginx php-8.3.9]# cd sapi/
[root@nginx sapi]# cd fpm
[root@nginx fpm]# vim /lib/systemd/system/php-fpm.service
# Mounts the /usr, /boot, and /etc directories read-only for processes invoked by
this unit.
#ProtectSystem=full #注释该内容
[root@Nginx php-8.3.9]# systemctl start php-fpm.service
[root@Nginx php-8.3.9]# netstat -antlupe | grep php
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 0
820758 176202/php-fpm: mas

[root@nginx sapi]# cd /usr/local/php/etc/php-fpm.d/
[root@nginx sapi]# cd /usr/local/php/etc/php-fpm.d/
[root@nginx php-fpm.d]# vim www.conf
#省略
; Note: This value is mandatory.
listen = *:9000
#省略
[root@nginx php-fpm.d]#  systemctl restart php-fpm.service
[root@nginx php-fpm.d]# netstat -antlupe | grep php
tcp6       0      0 ::1:9000                :::*                    LISTEN      0          434546     374128/php-fpm: mas 


[root@Nginx ~]# mkdir /data/php -p
[root@centos8 ~]# cat /data/php/index.php #php测试页面
<?php
phpinfo();
?>

#Nginx配置转发
#Nginx安装完成之后默认生成了与fastcgi的相关配置文件,一般保存在nginx的安装路径的conf目录当
#中,比如/apps/nginx/conf/fastcgi.conf、/apps/nginx/conf/fastcgi_params。
[root@Nginx ~]# vim /usr/local/nginx/conf.d
server {
listen 80;
server_name php.timinglee.org;
root /data/php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
#重启Nginx并访问web测试
[root@Nginx ~]# nginx -s reload

 

 

php的动态扩展模块(php的缓存模块)
安装memcache模块

#安装memcache模块
[root@Nginx ~]# tar zxf memcache-8.2.tgz
[root@Nginx ~]# cd memcache-8.2/
[root@Nginx memcache-8.2]# yum install autoconf
[root@Nginx memcache-8.2]# phpize
[root@Nginx memcache-8.2]# ./configure && make && make install
[root@Nginx memcache-8.2]# ls /usr/local/php/lib/php/extensions/no-debug-non-zts-20230831/
memcache.so opcache.so
复制测试文件到nginx发布目录中

#复制测试文件到nginx发布目录中
[root@Nginx ~]# cd memcache-8.2/
[root@Nginx memcache-8.2]# ls
[root@Nginx memcache-8.2]# cp example.php memcache.php /data/web/php/
[root@Nginx ~]# vim /data/web/php/memcache.php
define('ADMIN_USERNAME','admin'); // Admin Username
define('ADMIN_PASSWORD','lee'); // Admin Password
define('GRAPH_SIZE',200);
define('MAX_ITEM_DUMP',50);
$MEMCACHE_SERVERS[] = '127.0.0.1:11211'; // add more as an array
#$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array
配置php加载memcache模块

# 配置php加载memcache模块
[root@Nginx ~]# vim /usr/local/php/etc/php.ini
;extension=zip
extension=memcache
;zend_extension=opcache
[root@Nginx ~]# systemctl reload php-fpm
# 因为没有指定路劲,所以要移动到默认路径
[root@Nginx ~]# cp /usr/local/php/etc/php.ini /usr/local/php/lib/
[root@Nginx no-debug-non-zts-20230831]# php -m | grep mem
memcache
部署memcached

# 部署memcached
[root@Nginx ~]# yum install memcached -y
[root@Nginx ~]# systemctl enable --now memcached.service
[root@Nginx ~]# netstat -antlupe | grep memcache
tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN
976 1037243 186762/memcached
[root@Nginx ~]# cat /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 127.0.0.1,::1
测试:

# 测试
访问 http://www.timinglee.org/example.php 不断刷新
访问 http://www.timinglee.org/memcache.php 查看命中效果

 php高速缓存

#php高速缓存


部署方法:

[root@Nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
upstream memcache {
    server 127.0.0.1:11211;
    keepalive 512;
}
server {
    listen 80;
    server_name www.timinglee.org;
    root /data/web/html;
    index index.html;
    location /memc {
        internal;
        memc_connect_timeout 100ms;
        memc_send_timeout 100ms;
        memc_read_timeout 100ms;
        set $memc_key $query_string; #使用内置变量$query_string来作为key
        set $memc_exptime 300; #缓存失效时间300秒
        memc_pass memcache;
    }
    location ~ \.php$ {
        root /data/web/php;
        set $key $uri$args; #设定key的值
        srcache_fetch GET /memc $key; #检测mem中是否有要访问的php
        srcache_store PUT /memc $key; #缓存为加载的php数据
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
}
[root@Nginx ~]# nginx -s reload
​
#测试
[root@nginx ~]# ab -n1000 -c10 http://www.timinglee.org/index.php
Failed requests:        0

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2065339.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Python编码系列—Python WebSocket 实时通信:构建高效互动的网络应用

&#x1f31f;&#x1f31f; 欢迎来到我的技术小筑&#xff0c;一个专为技术探索者打造的交流空间。在这里&#xff0c;我们不仅分享代码的智慧&#xff0c;还探讨技术的深度与广度。无论您是资深开发者还是技术新手&#xff0c;这里都有一片属于您的天空。让我们在知识的海洋中…

基于贝叶斯优化卷积神经网络(Bayes-CNN)的多因子数据分类识别算法matlab仿真

目录 1.算法运行效果图预览 2.算法运行软件版本 3.部分核心程序 4.算法理论概述 5.算法完整程序工程 1.算法运行效果图预览 (完整程序运行后无水印) 贝叶斯优化过程 贝叶斯优化后的CNN训练和识别结果 标准的CNN的识别结果 2.算法运行软件版本 matlab2022a 3.部分核心程…

JavaScript(29)——函数参数

动态参数 arguments是函数内部内置的伪数组变量&#xff0c;它包含了调用函数时传入的所有实参&#xff0c;结果是一个伪数组。 function sum() {console.log(arguments);}sum(2, 3, 4)sum(6, 7) 剩余参数 剩余参数允许我们将一个不定数量的参数表示为一个数组 function sum…

深入理解HTTP的基础知识:请求-响应过程解析

首先&#xff0c;我们从网络协议的最顶层开始讲解&#xff0c;即应用层。在网络通信中&#xff0c;应用层是最接近用户的一层&#xff0c;它负责为特定的网络应用提供服务和功能。应用层协议定义了数据交换的规则和格式&#xff0c;以便不同的应用程序能够相互通信和交换信息。…

Xenu 网站死链接检测工具下载以及使用指南

引言 Xenus Link Sleuth&#xff08;简称 Xenu&#xff09;是一款功能强大的网站死链接检测工具。由于其绿色蜗牛形状的图标&#xff0c;Xenu 被广大的 SEO 人员亲切地称为“绿蜗牛”。 随着网站的发展&#xff0c;可能会因为删除过期内容或改版而导致一些死链接的产生。死链…

扫描包得到所有Bean的Class对象

文章目录 1.任务介绍以及类加载器1.介绍2.类加载器1.介绍2.自己程序的类路径 2.新建一个模块sun-spring用来写自己的spring容器1.新建模块2.查看是否交给父模块管理3.引入日志 3.具体实现1.目录2.Component.java3.ComponentScan.java4.MonsterDao.java5.MonsterService.java6.S…

第三期书生大模型实战营 进阶岛第3关LMDeploy 量化部署进阶实践

环境准备 conda create -n lmdeploy python3.10 -y conda activate lmdeploy conda install pytorch2.1.2 torchvision0.16.2 torchaudio2.1.2 pytorch-cuda12.1 -c pytorch -c nvidia -y pip install timm1.0.8 openai1.40.3 lmdeploy[all]0.5.3为方便文件管理&#xff0c;我…

Vue(一) 插值与指令语法、数据代理、MVVM模式、事件处理

文章目录 1. 初始Vue2. 模板语法2.1 插值语法2.2 指令语法 3. el与data的两种写法3.1 el的两种写法3.2 data的两种写法 4. MVVM模式5. 数据代理5.1 Object.defineProperty()5.2 何为数据代理5.4 vue中的数据代理 6. 事件处理6.1 v-on6.2 事件修饰符6.3 键盘按键事件 1. 初始Vue…

【C语言】预处理的使用

预处理 一、预处理-宏定义1、程序编译过程(1) 编写源程序(2) 程序编译过程说明 2、预处理3、宏的概念4、无参宏5、带参宏6、带参宏的副作用7、宏定义中的符号粘贴 二、预处理.条件编译1、无值宏定义2、条件编译3、条件编译的使用场景 三、预处理.头文件1、头文件的作用2. 头文件…

RTSP/Onvif安防视频监控平台EasyNVR在欧拉系统中启动失败的原因排查

视频安防监控平台EasyNVR可支持设备通过RTSP/Onvif协议接入&#xff0c;并能对接入的视频流进行处理与多端分发&#xff0c;包括RTSP、RTMP、HTTP-FLV、WS-FLV、HLS、WebRTC等多种视频流格式。平台支持轻量化部署&#xff0c;可兼容各类操作系统&#xff0c;包括Windows、Linux…

Admin.NET源码学习(4:基于Furion的后台服务启动方式浅析)

Admin.NET为前后端分离架构&#xff0c;后台服务的入口项目为Admin.NET.Web.Entry&#xff0c;其与其它项目的依赖关系如下图所示。   由于项目采用Furion框架&#xff0c;后台服务启动方式、注册方式、配置方式等方面与常规的asp.net core项目差异明显&#xff0c;初步接触…

计算机的错误计算(七十)

摘要 讨论大数的正割函数 sec(x)的错误计算。 例1. 已知 在 Maple 中计算 在 Maple中输入&#xff1a; restart; sec(30^54.8); 则输出&#xff1a; -5.214386310 若输入&#xff1a; Digits : 16;evalf[16](sec(30^54.8)); 则输出&#xff1a; 1.324455078865824…

中年人开发语言学习之路,反其道而行之

大家都更愿意学习新技术、新架构&#xff0c;代表着新方向新趋势&#xff0c;当大家都这么想的时候&#xff0c;注定了竞争就会激烈。有一部分中年程序员&#xff0c;反其道而行之&#xff0c;学习一些老掉牙的开发语言&#xff0c;向哪些近乎被遗忘的老旧系统进军。 市面上依…

一文了解Ansible原理以及常见使用模块

ansible使用手册 1. 简述 Ansible 是一种开源的自动化工具&#xff0c;主要用于配置管理、应用程序部署和任务自动化。 它使用简单的 YAML 语言来定义自动化的任务【playbook】&#xff0c;使得配置和部署变得更加直观和易于管理。 基于SSH协议连接到远程主机来执行指令。 2…

图像数据处理21

五、边缘检测 5.2基于二阶导数的边缘检测 一阶导数&#xff08;如Sobel、Prewitt算子&#xff09;能够捕捉到灰度值的快速变化&#xff0c;但有时会因检测到过多的边缘点而导致边缘线过粗。为了更加精确地定位边缘位置&#xff0c;可以利用二阶导数的零交叉点。零交叉点是是函…

触想工业一体机辅助DR系统提升医学影像诊断效率

一、行业发展背景 早期X线摄影依赖胶片成像&#xff0c;不便于图像存储管理&#xff0c;且显影过程长&#xff0c;无法进行后期处理&#xff0c;诊断质量和效率受到局限。 随着数字化技术的发展&#xff0c;DR系统问世&#xff0c;利用平板探测器将X射线图像转化为数字信号&…

推荐一款功能全面的层次化笔记应用,支持自由拖拽、缩放、旋转,可视化非常牛逼(附源码)

背景 不知道各位大佬日常生活中笔记软件用的多不&#xff0c;小编在工作中常常用笔记来记录每天的收获和安排。笔记软件的好坏直接影响了工作的心情和效率。今天给大家介绍的这款笔记软件&#xff0c;以其强大的笔记功能为基础&#xff0c;创造性地融入了画布式的自由编辑特性…

关于武汉芯景科技有限公司的RS232通信接口芯片XJ3243EEUI开发指南(兼容MAX3243EEUI)

一、芯片引脚介绍 1.芯片引脚 2.引脚描述 二、典型应用电路 三、功能描述 1.Transmitter 通过T1&#xff0c;T2可以将TTL电平转换为RS232电平 2.Receiver 通过R1&#xff0c;R2可以将RS232电平转换为TTL电平 3.工作模式控制 4.INVALID引脚

EDKII之安全启动详细介绍

文章目录 安全启动简介安全启动流程介绍签名过程BIOS实现小结 安全启动简介 安全启动&#xff08;Secure Boot&#xff09;是一种计算机系统的安全功能&#xff0c;旨在确保系统启动过程中只能加载经过数字签名的受信任的操作系统和启动加载程序。通过使用安全启动&#xff0c…

数据结构之串与KMP算法详解

串 一. 定义&#xff08;了解&#xff09; 串&#xff0c;即字符串&#xff0c;是计算机系统和网络传输中最常用的数据类型&#xff0c;任何非数值型的处理都会以字符串的形式存储和使用。 串&#xff08;String&#xff09;是由零个或多个字符组成的有限序列&#xff0c;一…