Nginx三種不同類型的虛擬主機(jī)的配置(基于域名、IP 和端口)
Nginx 是一款高性能的 Web 服務(wù)器,支持多種虛擬主機(jī)配置方式,能夠根據(jù)域名、IP 或端口區(qū)分不同的站點(diǎn)。這種靈活性讓 Nginx 成為搭建多站點(diǎn)服務(wù)的首選工具。本文將帶你一步步實(shí)現(xiàn)三種常見(jiàn)的虛擬主機(jī)配置方法:基于域名、基于 IP 和基于端口的虛擬主機(jī)。無(wú)論你是初學(xué)者還是有經(jīng)驗(yàn)的運(yùn)維人員,這篇教程都能幫助你快速掌握虛擬主機(jī)的配置技巧。
以下案例演示 是基于源碼包安裝的nignx (如果你是rpm包 也差不多 只用把路徑改為你nginx的路徑即可 其他沒(méi)什么大的變化,如果你是小白請(qǐng)繞道!)
1. 基于域名的虛擬主機(jī)
步驟 1:準(zhǔn)備網(wǎng)站根目錄
為每個(gè)域名創(chuàng)建獨(dú)立的子目錄,并添加測(cè)試頁(yè)面:
[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 配置文件
打開(kāi) Nginx 的配置文件:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
在 http
配置段中添加以下內(nèi)容:
注釋:如果需要兩個(gè)虛擬主機(jī) 只用將再額外添加一個(gè)server即可
# 全局配置 user nobody; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # 基于域名的虛擬主機(jī)配置 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; } # 錯(cuò)誤頁(yè)面配置 error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /404.html { root html/site1; } location = /50x.html { root html; } # 禁止訪問(wèn) .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; } # 錯(cuò)誤頁(yè)面配置 error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /404.html { root html/site2; } location = /50x.html { root html; } # 禁止訪問(wèn) .ht 文件 location ~ /\.ht { deny all; } } }
步驟 3:測(cè)試配置并重啟 Nginx
測(cè)試配置文件語(yǔ)法:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
重啟 Nginx 服務(wù):
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
步驟 4:訪問(wèn)測(cè)試
在瀏覽器中訪問(wèn):
http://www.site1.com
,應(yīng)顯示Welcome to Site 1
。http://www.site2.com
,應(yīng)顯示Welcome to Site 2
。
客戶端測(cè)試
修改hosts文件(本地dns解析)
[root@localhost ~]# vim /etc/hosts
2. 基于 IP 的虛擬主機(jī)
步驟 1:準(zhǔn)備網(wǎng)站根目錄
為每個(gè) IP 創(chuàng)建獨(dú)立的子目錄,并添加測(cè)試頁(yè)面:
[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 配置文件
打開(kāi) 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; # 使用默認(rèn)路徑的子目錄 index index.html; location / { try_files $uri $uri/ =404; } # 錯(cuò)誤頁(yè)面 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; # 默認(rèn)錯(cuò)誤頁(yè)面路徑 } } server { listen 192.168.14.112:80; server_name 192.168.14.112; root html/ip2; # 使用默認(rèn)路徑的子目錄 index index.html; location / { try_files $uri $uri/ =404; } # 錯(cuò)誤頁(yè)面 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; # 默認(rèn)錯(cuò)誤頁(yè)面路徑 } }
步驟 3:測(cè)試配置并重啟 Nginx
測(cè)試配置文件語(yǔ)法:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
重啟 Nginx 服務(wù):
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
步驟 4:訪問(wèn)測(cè)試
在瀏覽器中訪問(wèn):
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
。
客戶端測(cè)試
因?yàn)槲以谔摂M機(jī)測(cè)試 只有一個(gè)網(wǎng)卡 所以我在虛擬一個(gè)網(wǎng)卡 這個(gè)你可以忽視 看測(cè)試結(jié)果即可
ip addr add 192.168.14.110/24 dev ens33
3. 基于端口的虛擬主機(jī)
步驟 1:準(zhǔn)備網(wǎng)站根目錄
為每個(gè)端口創(chuàng)建獨(dú)立的子目錄,并添加測(cè)試頁(yè)面:
[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 配置文件
打開(kāi) Nginx 的配置文件:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
在 http
配置段中添加以下內(nèi)容:
server { listen 8080; server_name localhost; root html/port1; # 使用默認(rèn)路徑的子目錄 index index.html; location / { try_files $uri $uri/ =404; } # 錯(cuò)誤頁(yè)面 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; # 默認(rèn)錯(cuò)誤頁(yè)面路徑 } } server { listen 9090; server_name localhost; root html/port2; # 使用默認(rèn)路徑的子目錄 index index.html; location / { try_files $uri $uri/ =404; } # 錯(cuò)誤頁(yè)面 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; # 默認(rèn)錯(cuò)誤頁(yè)面路徑 } }
步驟 3:測(cè)試配置并重啟 Nginx
測(cè)試配置文件語(yǔ)法:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
重啟 Nginx 服務(wù):
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
步驟 4:訪問(wèn)測(cè)試
在瀏覽器中訪問(wèn):
http://192.168.14.111:8080
,應(yīng)顯示Welcome to Port 8080
。http://192.168.14.111:9090
,應(yīng)顯示Welcome to Port 9090
。
客戶端測(cè)試
4.總結(jié)
通過(guò)本文的詳細(xì)步驟,我們成功實(shí)現(xiàn)了基于域名、IP 和端口的虛擬主機(jī)配置。Nginx 的靈活性和高性能使其能夠輕松應(yīng)對(duì)多站點(diǎn)服務(wù)的需求。這些配置方法不僅適用于日常開(kāi)發(fā)和測(cè)試環(huán)境,也能在生產(chǎn)環(huán)境中提供穩(wěn)定可靠的服務(wù)。
到此這篇關(guān)于Nginx三種不同類型的虛擬主機(jī)的配置(基于域名、IP 和端口)的文章就介紹到這了,更多相關(guān)Nginx 虛擬主機(jī)配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx進(jìn)行端口轉(zhuǎn)發(fā)的實(shí)現(xiàn)
本文主要介紹了nginx進(jìn)行端口轉(zhuǎn)發(fā)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03nginx訪問(wèn)報(bào)403錯(cuò)誤的幾種情況詳解
最近訪問(wèn)新安裝的nginx,發(fā)現(xiàn)報(bào)403錯(cuò)誤,所以這篇文章主要給大家介紹了關(guān)于nginx訪問(wèn)報(bào)403錯(cuò)誤的幾種情況,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07nginx限制ip訪問(wèn)頻率的實(shí)現(xiàn)示例
Nginx通過(guò)limit_conn_zone 和 limit_req_zone對(duì)同一個(gè)IP地址進(jìn)行限速限流,本文主要介紹了nginx限制ip訪問(wèn)頻率的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Nginx 配置反向代理使用 Google fonts 字體并開(kāi)啟 HTTP2/SSL 支持
nginx作為web服務(wù)器一個(gè)重要的功能就是反向代理。當(dāng)然你也可以使用nginx配置正向代理,本是介紹如何配置nginx的反向代理。nginx反向代理的指令不需要新增額外的模塊,默認(rèn)自帶proxy_pass指令,只需要修改配置文件就可以實(shí)現(xiàn)反向代理。2017-04-04Nginx通過(guò)header中的標(biāo)識(shí)進(jìn)行分發(fā)
本文主要介紹了Nginx通過(guò)header中的標(biāo)識(shí)進(jìn)行分發(fā),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03詳解nginx使用ssl模塊配置支持HTTPS訪問(wèn)
這篇文章主要介紹了詳解nginx使用ssl模塊配置支持HTTPS訪問(wèn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08