nginx 配置虛擬主機(jī),實(shí)現(xiàn)在一個服務(wù)器可以訪問多個網(wǎng)站的方法
在一臺服務(wù)器上,訪問不同的網(wǎng)站
通常有兩種區(qū)分方式:
1.通過監(jiān)聽的端口號
2.通過域名
1.通過端口訪問不同的主機(jī):
Nginx的配置文件:
/usr/local/nginx/conf/nginx.conf
Centos文件默認(rèn)編碼格式 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é)點(diǎn)
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é)點(diǎn),即 你需要訪問網(wǎng)站的配置
#一個server節(jié)點(diǎn),就是一個虛擬主機(jī)
server {
listen 80; #監(jiān)聽的端口號,訪問網(wǎng)站 默認(rè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; #設(shè)置網(wǎng)站首頁
}
}
}
此時 可以配置多個server,也就是配置了不同的主機(jī)
添加虛擬主機(jī):(通過端口號 區(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
效果:

我們知道,當(dāng)一個服務(wù)器上配置多個網(wǎng)站時,我們不可能通過端口號來區(qū)分它們,所以接下來 我需要通過域名來區(qū)分
2.通過域名區(qū)分不同的虛擬主機(jī)
什么是域名??
域名就是網(wǎng)址
例如:www.baidu.com
通常我們在訪問域名的時候,我們需要通過dns服務(wù)器解析域名
Dns服務(wù)器:把域名解析為ip地址。保存的就是域名和ip的映射關(guān)系。
一個域名對應(yīng)一個ip地址,一個ip地址可以被多個域名綁定。
本地測試可以修改hosts文件。
修改window的hosts文件:(C:\Windows\System32\drivers\etc)
可以配置域名和ip的映射關(guān)系,如果hosts文件中配置了域名和ip的對應(yīng)關(guān)系,不需要走dns服務(wù)器?。。?!
在剛剛的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服務(wù)
觀察下效果:

以上這篇nginx 配置虛擬主機(jī),實(shí)現(xiàn)在一個服務(wù)器可以訪問多個網(wǎng)站的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解讀Nginx和Apache的特點(diǎn)與區(qū)別
這篇文章主要介紹了解讀Nginx和Apache的特點(diǎn)與區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
Nginx實(shí)現(xiàn)不同域名輸出不同的服務(wù)器頭信息方法
這篇文章主要介紹了Nginx實(shí)現(xiàn)不同域名輸出不同的服務(wù)器頭信息方法,本文使用了一個ngx_headers_more模塊實(shí)現(xiàn)這個特殊需求,需要的朋友可以參考下2015-02-02
Nginx+Windows搭建域名訪問環(huán)境的操作方法
這篇文章主要介紹了Nginx搭建域名訪問環(huán)境,包括nginx配置文件的相關(guān)介紹及對nginx配置文件的分析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03

