Nginx實現(xiàn)三種常見的虛擬主機配置方法
引言
Nginx 是一款高性能的 Web 服務器,支持多種虛擬主機配置方式,能夠根據(jù)域名、IP 或端口區(qū)分不同的站點。這種靈活性讓 Nginx 成為搭建多站點服務的首選工具。本文將帶你一步步實現(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 服務:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
步驟 4:訪問測試
在瀏覽器中訪問:
http://www.site1.com
,應顯示Welcome to Site 1
。http://www.site2.com
,應顯示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 服務:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
步驟 4:訪問測試
在瀏覽器中訪問:
http://192.168.14.111
,應顯示Welcome to IP 192.168.14.111
。http://192.168.14.112
,應顯示Welcome to IP 192.168.14.112
。
客戶端測試
因為我在虛擬機測試 只有一個網(wǎng)卡 所以我在虛擬一個網(wǎng)卡 這個你可以忽視 看測試結果即可
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 服務:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
步驟 4:訪問測試
在瀏覽器中訪問:
http://192.168.14.111:8080
,應顯示Welcome to Port 8080
。http://192.168.14.111:9090
,應顯示Welcome to Port 9090
。
客戶端測試
4.總結
通過本文的詳細步驟,我們成功實現(xiàn)了基于域名、IP 和端口的虛擬主機配置。Nginx 的靈活性和高性能使其能夠輕松應對多站點服務的需求。這些配置方法不僅適用于日常開發(fā)和測試環(huán)境,也能在生產(chǎn)環(huán)境中提供穩(wěn)定可靠的服務。如果你對 Nginx 的配置還有疑問或其他需求,歡迎留言交流,讓我們共同學習、共同進步!
以上就是Nginx實現(xiàn)三種常見的虛擬主機配置方法的詳細內(nèi)容,更多關于Nginx虛擬主機配置的資料請關注腳本之家其它相關文章!
相關文章
詳解Nginx防盜鏈和Nginx訪問控制與Nginx解析php的配置
這篇文章主要介紹了詳解Nginx防盜鏈和Nginx訪問控制與Nginx解析php的配置的相關資料,這里提供實例幫助大家,學習理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08在Linux和Windows系統(tǒng)上安裝Nginx服務器的教程
這篇文章主要介紹了在Linux和Windows系統(tǒng)上安裝Nginx服務器的教程,Linux系統(tǒng)這里以CentOS為代表,需要的朋友可以參考下2015-08-08