虛擬主機(jī)

認(rèn)識
1個虛擬主機(jī) 相當(dāng)于是1個網(wǎng)站
Nginx多個server標(biāo)簽
不同虛擬主機(jī)
虛擬主機(jī)(必備)
不同的域名不同的網(wǎng)站1.Nginx的server標(biāo)簽

[8:55 root@web01 ~]# mkdir" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網(wǎng)站運(yùn)營 > day47-Nginx-虛擬主機(jī)—多個server模塊(轉(zhuǎn)載)

day47-Nginx-虛擬主機(jī)—多個server模塊(轉(zhuǎn)載)

時間: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)載):

虛擬主機(jī)

認(rèn)識
1個虛擬主機(jī) 相當(dāng)于是1個網(wǎng)站
Nginx多個server標(biāo)簽
不同虛擬主機(jī)
虛擬主機(jī)(必備)
不同的域名不同的網(wǎng)站
1.Nginx的server標(biāo)簽

[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ī)(必備)
不同的域名訪問不同虛擬主機(jī)(網(wǎng)站)
基于端口的虛擬主機(jī)
不同的端口訪問不同的虛擬主機(jī)
正常端口 80 443
網(wǎng)站后臺人員 使用特殊端口
基于ip的虛擬主機(jī)
nginx處理用戶請求過程


3. 基于端口的虛擬主機(jī)

用81和82端口測試

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.com4.基于ip的虛擬主機(jī)

nginx.conf中添加一個新的ip

server { listen 10.0.0.9:80; server_name blog.oldboy.com; location / { root /usr/share/nginx/html/blog; index index.html index.htm; } }修改之后直接重啟或者檢查語法會報錯

因為沒有注冊ip

[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

systemctl重啟nginx

curl看一下

[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.com5.nginx配置默認(rèn)訪問第一個
在此配置中,nginx僅測試請求的標(biāo)頭字段“Host”,以確定請求應(yīng)路由到哪個服務(wù)器。如果其值與任何服務(wù)器名稱都不匹配,或者請求根本不包含此標(biāo)頭字段,則nginx會將請求路由到此端口的默認(rèn)服務(wù)器。在上面的配置中,默認(rèn)服務(wù)器是第一個 - 這是nginx的標(biāo)準(zhǔn)默認(rèn)行為。它也可以default_server使用listen指令中的參數(shù)明確設(shè)置哪個服務(wù)器應(yīng)該是默認(rèn)的:


> <pre style="padding: 0px; margin: 0px;">server { listen 80 **default_server** ; server_name example.net www.example.net; ... }</pre>※6.nginx的日志

/var/log/nginx/access.log

Nginx內(nèi)置變量

'$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地址
日志格式:
10.0.0.7 - - [05/Jun/2019:11:06:14 +0800] "GET /index.html HTTP/1.1" 200 15 "-" "curl/7.29.0" "-"

"$http_referer"記錄的用戶從哪里跳轉(zhuǎn)過來的

百度搜索本地ip

304 Not Modifed 用戶讀取瀏覽器緩存

ip訪問量:

awk '{print $1}' /var/log/nginx/access.log |sort |uniq -c

pv訪問量:

wc -l /var/log/nginx/access.log

8.Nginx日志格式

access_log

nginx配置文件中添加配置日志access_log

[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)限控制
狀態(tài)模塊
權(quán)限控制
7.將配置中的模塊單獨(dú)寫出來放到conf.d下

最好將站點目錄的默認(rèn)
zcat zless zmore zgrep zegrep
壓縮gzip 解壓 gzip -d

[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.gz8.添加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)段

官網(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瀏覽器看一下今天配置的這幾個域名可不可以訪問

10.curl -H的用法

curl -H Host:http://status.oldboy.com 10.0.0.7

[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ī)

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點擊下載Chrome瀏覽器
關(guān)閉