時間:2023-07-12 14:30:01 | 來源:網(wǎng)站運(yùn)營
時間:2023-07-12 14:30:01 來源:網(wǎng)站運(yùn)營
day47-Nginx-虛擬主機(jī)—多個server模塊(轉(zhuǎn)載):認(rèn)識1.Nginx的server標(biāo)簽
1個虛擬主機(jī) 相當(dāng)于是1個網(wǎng)站
Nginx多個server標(biāo)簽
不同虛擬主機(jī)
虛擬主機(jī)(必備)
不同的域名不同的網(wǎng)站
[8:55 root@web01 ~]# mkdir -p /usr/share/nginx/html/{www,blog} //創(chuàng)建www與blog站點目錄[09:00 root@web01 ~]# for n in www blog ;do echo $n.oldboy.com >/usr/share/nginx/html/$n/index.html ;done //給倆個站點目錄index.html文件 添加內(nèi)容[09:00 root@web01 ~]# cat /usr/share/nginx/html/{www,blog}/index.htmlwww.oldboy.comblog.oldboy.com[09:01 root@web01 ~]# vim /etc/hosts //添加域名解析127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6172.16.1.5 lb01172.16.1.6 lb02172.16.1.7 web01 www.oldboy.com blog.oldboy.com //添加域名解析172.16.1.8 web02172.16.1.31 nfs01172.16.1.41 backup172.16.1.51 db01 db01.etiantian.org172.16.1.61 m01[09:03 root@web01 ~]# systemctl restart nginx //重啟nginx服務(wù)[09:03 root@web01 ~]# curl www.oldboy.com //curl一下www的域名www.oldboy.com[09:04 root@web01 ~]# curl blog.oldboy.com //curl一下blog的域名blog.oldboy.com[09:04 root@web01 ~]#
※【不同的虛擬主機(jī)】基于域名的虛擬主機(jī)(必備)nginx處理用戶請求過程
不同的域名訪問不同虛擬主機(jī)(網(wǎng)站)
基于端口的虛擬主機(jī)
不同的端口訪問不同的虛擬主機(jī)
正常端口 80 443
網(wǎng)站后臺人員 使用特殊端口
基于ip的虛擬主機(jī)
server { listen 81; server_name www.oldboy.com; location / { root /usr/share/nginx/html/www; index index.html index.htm; } } server { listen 82; server_name blog.oldboy.com; location / { root /usr/share/nginx/html/blog; index index.html index.htm; } }}[10:14 root@web01 ~]# curl http://10.0.0.7curl: (7) Failed connect to 10.0.0.7:80; Connection refused[10:14 root@web01 ~]# curl http://10.0.0.7:81www.oldboy.com[10:14 root@web01 ~]# curl http://10.0.0.7:82blog.oldboy.com
4.基于ip的虛擬主機(jī)server { listen 10.0.0.9:80; server_name blog.oldboy.com; location / { root /usr/share/nginx/html/blog; index index.html index.htm; } }
修改之后直接重啟或者檢查語法會報錯[10:30 root@web01 ~]# systemctl restart nginxJob for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.[10:30 root@web01 ~]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: [emerg] bind() to 10.0.0.66:81 failed (99: Cannot assign requested address)nginx: configuration file /etc/nginx/nginx.conf test failed
添加ip地址[10:30 root@web01 ~]# ip addr add 10.0.0.9/24 dev eth0 label eth0:1[10:31 root@web01 ~]# ip a.........2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:68:78:4f brd ff:ff:ff:ff:ff:ff inet 10.0.0.7/24 brd 10.0.0.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet 10.0.0.9/24 scope global secondary eth0:1 valid_lft forever preferred_lft forever
[10:31 root@web01 ~]# systemctl restart nginx[10:35 root@web01 ~]# curl 10.0.0.9:80blog.oldboy.com[10:38 root@web01 ~]# curl 10.0.0.7www.oldboy.com
5.nginx配置默認(rèn)訪問第一個> <pre style="padding: 0px; margin: 0px;">server { listen 80 **default_server** ; server_name example.net www.example.net; ... }</pre>
※6.nginx的日志'$remote_addr 客戶端ip地址日志格式:
$remote_user 遠(yuǎn)程用戶(空)
[$time_local] 時間
"$request" 請求報文的起始行 $request_uri 只取出uri
'$status 狀態(tài)碼
$body_bytes_sent 身體 字節(jié) 發(fā)送 服務(wù)端發(fā)給客戶端大?。總€文件的大?。?br>"$http_referer" 記錄著用戶從哪里跳轉(zhuǎn)過來的
'"$http_user_agent" 用戶瀏覽器
"$http_x_forwarded_for"'; 負(fù)載均衡: web服務(wù)器用來記錄用戶真實ip地址
[11:48 root@web01 ~]# vim /etc/nginx/nginx.conf .... server { listen 80; server_name www.oldboy.com; access_log /var/log/nginx/access_www.log main; //日志 location / { root /usr/share/nginx/html/www; index index.html index.htm; } } server { listen 80; server_name blog.oldboy.com; access_log /var/log/nginx/access_blog.log main; //日志 location / { root /usr/share/nginx/html/blog; index index.html index.htm; } }}
去/etc/hosts添加域名解析[root@m01 /usr/share/nginx/html]# vim /etc/hosts10.0.0.7 www.oldboy.com blog.oldboy.com status.oldboy.com
重啟nginx后檢查語法 查看日志路徑下的內(nèi)容:[11:52 root@web01 ~]# systemctl reload nginx.service [11:52 root@web01 ~]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[11:52 root@web01 ~]# ll /var/log/nginx/access*-rw-r--r-- 1 root root 0 Jun 5 11:48 /var/log/nginx/access_blog.log-rw-r----- 1 nginx adm 12983 Jun 5 11:36 /var/log/nginx/access.log-rw-r--r-- 1 root root 0 Jun 5 11:48 /var/log/nginx/access_www.log
nginx狀態(tài)模塊及權(quán)限控制7.將配置中的模塊單獨(dú)寫出來放到conf.d下
狀態(tài)模塊
權(quán)限控制
最好將站點目錄的默認(rèn)壓縮gzip 解壓 gzip -d
zcat zless zmore zgrep zegrep
[12:13 root@web01 /etc/nginx]# ll conf.d/total 12-rw-r--r-- 1 root root 233 Jun 5 12:04 01-www.conf-rw-r--r-- 1 root root 254 Jun 5 12:04 02-blog.conf-rw-r--r-- 1 root root 488 Apr 23 22:34 default.conf.gz
8.添加http://status.oldboy.com域名配置status.conf添加到conf.d下
/etc/hosts下配置域名解析
重啟nginx檢查語法 curl一下status.conf中添加的域名
[12:22 root@web01 /etc/nginx]# cat conf.d/status.conf server { listen 80; server_name status.oldboy.com; stub_status on; access_log off;}[12:22 root@web01 /etc/nginx]# ll conf.d/total 16-rw-r--r-- 1 root root 233 Jun 5 12:04 01-www.conf-rw-r--r-- 1 root root 254 Jun 5 12:04 02-blog.conf-rw-r--r-- 1 root root 488 Apr 23 22:34 default.conf.gz-rw-r--r-- 1 root root 90 Jun 5 12:21 status.conf[12:26 root@web01 /etc/nginx]# systemctl restart nginx[12:26 root@web01 /etc/nginx]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[12:26 root@web01 /etc/nginx]# curl status.oldboy.comActive connections: 1 server accepts handled requests 1 1 1 Reading: 0 Writing: 1 Waiting: 0
9.添加allow 允許網(wǎng)段ngx_http_access_module 模塊
不要寫錯格式
檢查語法后重啟 curl一下http://status.oldboy.com
[12:36 root@web01 /etc/nginx]# vim conf.d/status.conf server { listen 80; server_name status.oldboy.com; stub_status on; access_log off; allow 172.16.1.0/24; //添加允許網(wǎng)段 deny all;}[12:37 root@web01 /etc/nginx]# nginx -tnginx: [emerg] unexpected "}" in /etc/nginx/conf.d/status.conf:8nginx: configuration file /etc/nginx/nginx.conf test failed[12:37 root@web01 /etc/nginx]# systemctl reload nginx[12:37 root@web01 /etc/nginx]# curl status.oldboy.comActive connections: 1 server accepts handled requests 7 7 7 Reading: 0 Writing: 1 Waiting: 0
瀏覽器看一下今天配置的這幾個域名可不可以訪問[12:45 root@web01 /etc/nginx]# curl -H Host:status.oldboy.com 10.0.0.7<html><head><title>403 Forbidden</title></head><body><center><h1>403 Forbidden</h1></center><hr><center>nginx/1.16.0</center></body></html>
轉(zhuǎn)自:關(guān)鍵詞:轉(zhuǎn)載,虛擬,主機(jī)
客戶&案例
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。