|NO.Z.00004|——————————|^^ 构建 ^^|——|Nginx&Nginx.V1.16&部署&降级&升级.V3|

一、Nginx WEB虚拟主机企业实战:
### --- Nginx WEB虚拟主机企业实战:
### --- 在企业生产环境中,

~~~     一台nginx服务器通常不仅仅只发布一个网站,而是基于单台nginx服务器,
~~~     发布+部署多套网站,可以最大化的利用Nginx WEB服务器操作系统资源,避免资源的浪费。
### --- 将单台Nginx WEB服务器,

~~~     发布多套网站的方式,称为虚拟主机,nginx发布虚拟主机主要有三种方式。
~~~     基于同一个80端口,不同的IP地址:(多IP)不推荐,
~~~     基于同一个IP地址,不同的PORT端口:(多端口)用户直接访问都是用80端口,不推荐
~~~     基于同一个80端口,不同的访问域名:(多域名)
### --- 基于同一个80端口,

~~~     不同的访问域名(v1.cvc.net、v2.cvc.net);来配置Nginx多网站虚拟主机,操作方案:
二、配置文件参数
### --- 切换到nginx安装目录下进入conf目录,找到主配置文件:

[root@cdeba90ec46e conf]#vim /ur/local/nginx/conf/nginx.conf
### --- shell三剑客awk、sed、grep去除配置文件中#号和空行;

[root@cdeba90ec46e conf]# awk '/#/' nginx.conf                      // 第一个斜杆开始查找,第二个斜杠查找结束,查找两个斜杠里面的内容
[root@cdeba90ec46e conf]# awk '!/#/' nginx.conf                     // 加上!号表示取反,表示我们不需要显示空行。
[root@cdeba90ec46e conf]# awk '!/#/' nginx.conf|awk '!/^$/'         // ^ 表示空行;/^/以空开头以空结尾;/^$/以空开头,不以空结尾。
[root@cdeba90ec46e conf]# sed  '/#/d' nginx.conf|sed '/^$/d'        // 删除#号的空行的内容
[root@cdeba90ec46e conf]# grep -v "#" nginx.conf|grep -v "^$"
三、nginx.conf标准文件代码:
### --- nginx.conf标准文件代码:

[root@cdeba90ec46e conf]# grep -v "#" nginx.conf|grep -v "^$"
worker_processes  1;                                                // worker_processes表示nginx的服务进程,负责接收和处理用户的请求。-9表强制kill
events {                                                            // events  时间模块
    worker_connections  1024;                                       // worker_connections表示一个工作进程处理的用户请求为1024,整个nginx可以处理最大的用户并发为1024*4=4096个人用户请求
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {                                                        // server里面的内容是我们的虚拟主机,也就是网站;默认nginx只有一套网站。若想发布多套网站,我们复制server再粘贴就可以了。
        listen       80;                                            // 监听端口
        server_name  localhost;                                     // server_name监听域名 ;localhost表示本机域名;
        location / {                                                // location表示我们的发布目录;
            root   html;                                            // root:表示发布目录  ,html:表示相对路径;绝对路径是:/usr/local/nginx/html  
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
### --- 可以看到一个master和一个worker进程:(配置文件里面只有一个进程)
### --- 更改配置文件4个进程:
~~~     master不负责接收用户请求,只负责管理进程。
~~~     worker:工作进程

[root@cdeba90ec46e conf]# ps -ef |grep nginx
root        3131       0  0 10:39 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx    
www         3132    3131  0 10:39 ?        00:00:00 nginx: worker process  
### --- 重启nginx

[root@cdeba90ec46e conf]# /usr/local/nginx/sbin/nginx -s reload 
四、基于同一个80端口,不同的访问域名(v1.cvc.net、v2.cvc.net)配置
### --- 基于同一个80端口,不同的访问域名(v1.cvc.net、v2.cvc.net)配置

[root@cdeba90ec46e conf]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  4;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
====================================================================================
    server {                                                    // 第一个虚拟主机
        listen       80;
        server_name  v1.cvc.net;                                // 域名
        location / {
            root   /usr/local/nginx/html/v1.cvc.net;            // 发布目录
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {                                                    // 第二个虚拟主机
        listen       80;
        server_name  v2.cvc.net;                                // 域名
        location / {
            root   /usr/local/nginx/html/v2.cvc.net;            // 发布目录
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
五、手工方式创建mkdir两个发布目录;
### --- 手工方式创建mkdir两个发布目录;
### --- 大括号表示范围

[root@cdeba90ec46e conf]# mkdir -p /usr/loca/nginx/html/v{1,2}.cvc.net
[root@cdeba90ec46e html]# cat >/usr/local/nginx/html/v1.cvc.net/index.html<<EOF
<html>
<h1>v1.cvc.net Test Pages 域名:www.cvc.com</h1>
<hr color=red>
</html>
EOF
[root@cdeba90ec46e html]# cat >/usr/local/nginx/html/v2.cvc.net/index.html<<EOF
<html>
<h1>v2.cvc.net Test Pages 域名:www.cvc.com</h1>
<hr color=red>
</html>
EOF
### --- 重启nginx

[root@cdeba90ec46e /]# /usr/local/nginx/sbin/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@cdeba90ec46e conf]# /usr/local/nginx/sbin/nginx -s reload
六、内网环境下我们需要配置本地hosts文件,域名解析;
### --- 内网环境下我们需要配置本地hosts文件,域名解析;

~~~     看浏览器缓存,没有
~~~     看电脑本地的hosts文件,DNS服务器会将这个域名的请求交给我们本地DNS服务器;
~~~     本地DNS有域名的配置,会直接发给浏览器;
~~~     若本地的DNS服务器也没有,会发往根,
### --- 本地DNS配置,hosts文件配置;
### --- 本地电脑C:\Windows\System32\drivers\etc
~~~     192.168.1.81 v1.cvc.net
~~~     192.168.1.81 v2.cvc.net

### --- 然后再通过浏览器去访问;
[root@cdeba90ec46e v2.cvc.net]# pkill nginx    停止nginx

附录一:
### --- 此课程有nginx虚拟主机配置脚本:52:49

~~~     https://ke.qq.com/webcourse/2025906/102127284#taid=8522396233296306&vid=5285890801428670469

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(9)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示