Nginx三種不同類型的虛擬主機的配置(基于域名、IP 和端口)
Nginx 是一款高性能的 Web 服務(wù)器,支持多種虛擬主機配置方式,能夠根據(jù)域名、IP 或端口區(qū)分不同的站點。這種靈活性讓 Nginx 成為搭建多站點服務(wù)的首選工具。本文將帶你一步步實現(xiàn)三種常見的虛擬主機配置方法:基于域名、基于 IP 和基于端口的虛擬主機。無論你是初學者還是有經(jīng)驗的運維人員,這篇教程都能幫助你快速掌握虛擬主機的配置技巧。
以下案例演示 是基于源碼包安裝的nignx (如果你是rpm包 也差不多 只用把路徑改為你nginx的路徑即可 其他沒什么大的變化,如果你是小白請繞道!)
1. 基于域名的虛擬主機
步驟 1:準備網(wǎng)站根目錄
為每個域名創(chuàng)建獨立的子目錄,并添加測試頁面:
[root@localhost ~]# mkdir -p /usr/local/nginx/html/site1 [root@localhost ~]# mkdir -p /usr/local/nginx/html/site2 [root@localhost ~]# echo "Welcome to Site 1" > /usr/local/nginx/html/site1/index.html [root@localhost ~]# echo "Welcome to Site 2" > /usr/local/nginx/html/site2/index.html
步驟 2:修改 Nginx 配置文件
打開 Nginx 的配置文件:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
在 http 配置段中添加以下內(nèi)容:
注釋:如果需要兩個虛擬主機 只用將再額外添加一個server即可

# 全局配置
user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 基于域名的虛擬主機配置
server {
listen 80;
server_name www.site1.com;
# 網(wǎng)站根目錄
root html/site1;
index index.html index.htm;
# 日志配置
access_log logs/site1_access.log;
error_log logs/site1_error.log;
# 主路徑配置
location / {
try_files $uri $uri/ =404;
}
# 狀態(tài)監(jiān)控
location /status {
stub_status on;
access_log off;
allow 192.168.14.112;
deny all;
}
# 錯誤頁面配置
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /404.html {
root html/site1;
}
location = /50x.html {
root html;
}
# 禁止訪問 .ht 文件
location ~ /\.ht {
deny all;
}
}
server {
listen 80;
server_name www.site2.com;
# 網(wǎng)站根目錄
root html/site2;
index index.html index.htm;
# 日志配置
access_log logs/site2_access.log;
error_log logs/site2_error.log;
# 主路徑配置
location / {
try_files $uri $uri/ =404;
}
# 錯誤頁面配置
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /404.html {
root html/site2;
}
location = /50x.html {
root html;
}
# 禁止訪問 .ht 文件
location ~ /\.ht {
deny all;
}
}
}步驟 3:測試配置并重啟 Nginx
測試配置文件語法:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
重啟 Nginx 服務(wù):
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
步驟 4:訪問測試
在瀏覽器中訪問:
http://www.site1.com,應(yīng)顯示Welcome to Site 1。http://www.site2.com,應(yīng)顯示Welcome to Site 2。
客戶端測試
修改hosts文件(本地dns解析)
[root@localhost ~]# vim /etc/hosts


2. 基于 IP 的虛擬主機
步驟 1:準備網(wǎng)站根目錄
為每個 IP 創(chuàng)建獨立的子目錄,并添加測試頁面:
[root@localhost ~]# mkdir -p /usr/local/nginx/html/ip1 [root@localhost ~]# mkdir -p /usr/local/nginx/html/ip2 [root@localhost ~]# echo "Welcome to IP 192.168.14.111" > /usr/local/nginx/html/ip1/index.html [root@localhost ~]# echo "Welcome to IP 192.168.14.112" > /usr/local/nginx/html/ip2/index.html
步驟 2:修改 Nginx 配置文件
打開 Nginx 的配置文件:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
在 http 配置段中添加以下內(nèi)容:

server {
listen 192.168.14.111:80;
server_name 192.168.14.111;
root html/ip1; # 使用默認路徑的子目錄
index index.html;
location / {
try_files $uri $uri/ =404;
}
# 錯誤頁面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html; # 默認錯誤頁面路徑
}
}
server {
listen 192.168.14.112:80;
server_name 192.168.14.112;
root html/ip2; # 使用默認路徑的子目錄
index index.html;
location / {
try_files $uri $uri/ =404;
}
# 錯誤頁面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html; # 默認錯誤頁面路徑
}
}步驟 3:測試配置并重啟 Nginx
測試配置文件語法:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
重啟 Nginx 服務(wù):
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
步驟 4:訪問測試
在瀏覽器中訪問:
http://192.168.14.111,應(yīng)顯示Welcome to IP 192.168.14.111。http://192.168.14.112,應(yīng)顯示Welcome to IP 192.168.14.112。
客戶端測試
因為我在虛擬機測試 只有一個網(wǎng)卡 所以我在虛擬一個網(wǎng)卡 這個你可以忽視 看測試結(jié)果即可
ip addr add 192.168.14.110/24 dev ens33

3. 基于端口的虛擬主機
步驟 1:準備網(wǎng)站根目錄
為每個端口創(chuàng)建獨立的子目錄,并添加測試頁面:
[root@localhost ~]# mkdir -p /usr/local/nginx/html/port1 [root@localhost ~]# mkdir -p /usr/local/nginx/html/port2 [root@localhost ~]# echo "Welcome to Port 8080" > /usr/local/nginx/html/port1/index.html [root@localhost ~]# echo "Welcome to Port 9090" > /usr/local/nginx/html/port2/index.html
步驟 2:修改 Nginx 配置文件
打開 Nginx 的配置文件:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
在 http 配置段中添加以下內(nèi)容:

server {
listen 8080;
server_name localhost;
root html/port1; # 使用默認路徑的子目錄
index index.html;
location / {
try_files $uri $uri/ =404;
}
# 錯誤頁面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html; # 默認錯誤頁面路徑
}
}
server {
listen 9090;
server_name localhost;
root html/port2; # 使用默認路徑的子目錄
index index.html;
location / {
try_files $uri $uri/ =404;
}
# 錯誤頁面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html; # 默認錯誤頁面路徑
}
}步驟 3:測試配置并重啟 Nginx
測試配置文件語法:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
重啟 Nginx 服務(wù):
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
步驟 4:訪問測試
在瀏覽器中訪問:
http://192.168.14.111:8080,應(yīng)顯示Welcome to Port 8080。http://192.168.14.111:9090,應(yīng)顯示Welcome to Port 9090。
客戶端測試

4.總結(jié)
通過本文的詳細步驟,我們成功實現(xiàn)了基于域名、IP 和端口的虛擬主機配置。Nginx 的靈活性和高性能使其能夠輕松應(yīng)對多站點服務(wù)的需求。這些配置方法不僅適用于日常開發(fā)和測試環(huán)境,也能在生產(chǎn)環(huán)境中提供穩(wěn)定可靠的服務(wù)。
到此這篇關(guān)于Nginx三種不同類型的虛擬主機的配置(基于域名、IP 和端口)的文章就介紹到這了,更多相關(guān)Nginx 虛擬主機配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx進行端口轉(zhuǎn)發(fā)的實現(xiàn)
本文主要介紹了nginx進行端口轉(zhuǎn)發(fā)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03
Nginx 配置反向代理使用 Google fonts 字體并開啟 HTTP2/SSL 支持
nginx作為web服務(wù)器一個重要的功能就是反向代理。當然你也可以使用nginx配置正向代理,本是介紹如何配置nginx的反向代理。nginx反向代理的指令不需要新增額外的模塊,默認自帶proxy_pass指令,只需要修改配置文件就可以實現(xiàn)反向代理。2017-04-04

