文章目錄
- 一、基于域名的 Nginx 虛擬主機
- 1.為虛擬主機提供域名決議
- 2.為虛擬主機準備網頁檔案
- 3.修改Nginx的組態檔
- 4.重啟服務,訪問測驗
- 二、基于IP的虛擬主機
- 三、基于埠的虛擬主機
- 總結
一、基于域名的 Nginx 虛擬主機
1.為虛擬主機提供域名決議
echo "192.168.126.13 www.xjj.com www.accp.com" >> /etc/hosts

2.為虛擬主機準備網頁檔案
mkdir -p /var/www/html/xjj
mkdir -p /var/www/html/accp
echo "<h1>www.xjj.com</h1>" > /var/www/html/xjj/index.html
echo "<h1>www.accp.com</h1>" > /var/www/html/accp/index.html
3.修改Nginx的組態檔
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 80;
server_name www.xjj.com; #設定域名www.xjj.com
charset utf-8;
access_log logs/www.xjj.access.log;
location / {
root /var/www/html/xjj; #設定www.xjj.com 的作業目錄
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 80;
server_name www.accp.com; #設定域名www.accp.com
charset utf-8;
access_log logs/www.accp.access.log;
location / {
root /var/www/html/accp;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}

4.重啟服務,訪問測驗
systemctl restart nginx
瀏覽器訪問:
http://www.xjj.com
http://www.accp.com
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-ewlkj4T7-1633612858832)(C:\Users\姜姜愛吃香菜\AppData\Roaming\Typora\typora-user-images\image-20210930200704497.png)]
二、基于IP的虛擬主機
ifconfig ens33:0 192.168.126.31 netmask 255.255.255.0
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 192.168.126.14:80; #設定監聽地址
server_name www.xjj.com;
charset utf-8;
access_log logs/www.xjj.access.log;
location / {
root /var/www/html/xjj;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 192.168.126.18:80; #設定監聽地址
server_name www.accp.com;
charset utf-8;
access_log logs/www.accp.access.log;
location / {
root /var/www/html/accp;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
nginx -t
systemctl restart nginx.service

瀏覽器訪問
http://192.168.126.14
http://192.168.126.18


三、基于埠的虛擬主機
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 192.168.126.14:8080;
#設定監聽8080 埠
server_name www.xjj.com;
charset utf-8;
access_log logs/www.xjj.access.log;
location / {
root /var/www/html/xjj;
index index.html index.html;
}
error_page 500 502. 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 192.168.126.14:8888;
#設定監聽8888埠
server_name www.xcf.com;
charset utf-8;
access_log logs/www.xcf.access.log;
location / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
nginx -t
systemctl restart nginx.service
瀏覽器訪問
http://192.168.126.14:8080
http://192.128.126.14:8888
systemctl restart nginx



瀏覽器訪問
http://192.168.126.14:8080
http://192.168.126.14:8888


總結
- 虛擬主機:節省資源, 充分利用資源
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/306401.html
標籤:其他
