nginx 配置虛擬主機,實現在一個服務器可以訪問多個網站的方法
在一臺服務器上,訪問不同的網站
通常有兩種區(qū)分方式:
1.通過監(jiān)聽的端口號
2.通過域名
1.通過端口訪問不同的主機:
Nginx的配置文件:
/usr/local/nginx/conf/nginx.conf
Centos文件默認編碼格式 latin1
查看編碼格式的命令: :set fileencoding
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } ##一個http節(jié)點 http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; #server 節(jié)點,即 你需要訪問網站的配置 #一個server節(jié)點,就是一個虛擬主機 server { listen 80; #監(jiān)聽的端口號,訪問網站 默認是80端口 server_name localhost; #即訪問的域名 #charset koi8-r; #access_log logs/host.access.log main; location / { #定位 root html; #定位的是nginx根目錄下的 html文件夾 index index.html index.htm; #設置網站首頁 } } }
此時 可以配置多個server,也就是配置了不同的主機
添加虛擬主機:(通過端口號 區(qū)別)
server { listen 81; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html-81; #nginx根目錄下 新建的html81 文件夾 index index.html index.htm; `` }
編輯好文件之后,我們重新加載配置文件
通過命令: ./nginx -s reload
效果:
我們知道,當一個服務器上配置多個網站時,我們不可能通過端口號來區(qū)分它們,所以接下來 我需要通過域名來區(qū)分
2.通過域名區(qū)分不同的虛擬主機
什么是域名??
域名就是網址
例如:www.baidu.com
通常我們在訪問域名的時候,我們需要通過dns服務器解析域名
Dns服務器:把域名解析為ip地址。保存的就是域名和ip的映射關系。
一個域名對應一個ip地址,一個ip地址可以被多個域名綁定。
本地測試可以修改hosts文件。
修改window的hosts文件:(C:\Windows\System32\drivers\etc)
可以配置域名和ip的映射關系,如果hosts文件中配置了域名和ip的對應關系,不需要走dns服務器!?。?!
在剛剛的nginx.conf文件下 繼續(xù)配置:
server { listen 80; server_name www.taobao.com; #charset koi8-r; #access_log logs/host.access.log main; location / { root html-taobao; index index.html index.htm; } } server { listen 80; server_name www.baidu.com; #charset koi8-r; #access_log logs/host.access.log main; location / { root html-baidu; index index.html index.htm; } } }
域名的配置:
192.168.25.148 www.test.com
192.168.25.148 www.yiyou.com
重啟nginx服務
觀察下效果:
以上這篇nginx 配置虛擬主機,實現在一個服務器可以訪問多個網站的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
nginx配置gzip壓縮優(yōu)化傳輸效率加快頁面訪問速度的問題
本文介紹了如何在nginx服務器中配置gzip壓縮,通過壓縮HTTP響應內容,減少數據傳輸大小和響應時間,從而提升網站性能和訪問速度,感興趣的朋友跟隨小編一起看看吧2024-09-09