Nginx配置虛擬主機支持3種方式:基于IP的虛擬主機配置,基于端口的虛擬主機配置,基于域名的虛擬主機配置。
1、基于IP的虛擬主機配置
如果同一臺服務器有多個IP,可以使用基于IP的虛機主機配置,將不同的服務綁定在不同的IP上。
1.1 假設服務器有個IP地址為192.168.2.150,首先使用ifconfig在同一個網絡接口上綁定其他3個IP。
1.2 3個IP對應的域名如下,配置主機的host文件便于測試
[root@localhost ~]# vim /etc/hosts[root@localhost ~]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.2.151 www.test151.com192.168.2.152 www.test152.com192.168.2.153 www.test153.com
可以模擬實現DNS輪詢的情況。
附:設置完hosts文件后一定要記得執行以下命令使其生效
1、windows下cmd進入命令行
C:/Users/1234>ipconfig /flushdnsWindows IP 配置已成功刷新 DNS 解析緩存。
1.3 建立虛擬主機存放網頁的根目錄,并創建首頁文件index.html
[root@localhost /]# mkdir -p /data/www[root@localhost /]# cd /data/www[root@localhost www]# mkdir 151[root@localhost www]# mkdir 152[root@localhost www]# mkdir 153[root@localhost www]# echo "192.168.2.151" > 151/index.html[root@localhost www]# echo "192.168.2.152" > 152/index.html[root@localhost www]# echo "192.168.2.153" > 153/index.html[root@localhost www]# ls151 152 153
1.4 修改nginx.conf,將虛擬主機配置文件包含進主文件
[root@localhost /]# cd /usr/local/nginx/conf/[root@localhost conf]# lsfastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utffastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default[root@localhost conf]# vim nginx.conf
在nginx.conf文件末尾加入以下配置
# 在http段中找到以下內容并刪除每行前面的“#” log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';# 配置文件結尾的最后一個“}”之前加入以下語句,如下所示include vhost/*.conf;}
1.5 編輯每個IP的配置文件(每個虛擬主機的配置文件)
[root@localhost conf]# mkdir -p vhost[root@localhost conf]# cd vhost/[root@localhost vhost]# cat www.test151.conf server { listen 192.168.2.151:80; # 配置成實際的域名,每個虛擬主機的配置文件域名都相同 #server_name www.test.com; access_log /data/logs/www.test151.com.log main; error_log /data/logs/www.test151.com.error.log; location / { root /data/www/151; index index.html index.htm; } }[root@localhost vhost]# cat www.test152.conf server { listen 192.168.2.152:80; # 配置成實際的域名,每個虛擬主機的配置文件域名都相同 #server_name www.test.com; access_log /data/logs/www.test152.com.log main; error_log /data/logs/www.test152.com.error.log; location / { root /data/www/152; index index.html index.htm; } }[root@localhost vhost]# cat www.test153.conf server { listen 192.168.2.153:80; # 配置成實際的域名,每個虛擬主機的配置文件域名都相同 #server_name www.test.com; access_log /data/logs/www.test153.com.log main; error_log /data/logs/www.test153.com.error.log; location / { root /data/www/153; index index.html index.htm; } }
1.6 創建日志文件,否則無法啟動nginx
[root@localhost /]# mkdir -p /data/logs[root@localhost /]# touch /data/logs/www.test151.com.log[root@localhost /]# touch /data/logs/www.test151.com.error.log[root@localhost /]# touch /data/logs/www.test152.com.log[root@localhost /]# touch /data/logs/www.test152.com.error.log[root@localhost /]# touch /data/logs/www.test153.com.log[root@localhost /]# touch /data/logs/www.test153.com.error.log[root@localhost /]# ls /data/logs/www.test151.com.error.log www.test152.com.error.log www.test153.com.error.logwww.test151.com.log www.test152.com.log www.test153.com.log
1.7 先測試配置文件然后再啟動nginx
[root@localhost /]# cd /usr/local/nginx/sbin/[root@localhost sbin]# ./nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful# 啟動nginx[root@localhost sbin]# ./nginx
1.8 測試文件
[root@localhost sbin]# curl www.test151.com192.168.2.151[root@localhost sbin]# curl www.test152.com192.168.2.152[root@localhost sbin]# curl www.test153.com192.168.2.153
附:配置過程中出現的問題
1、測試配置文件時出現的問題
[root@localhost sbin]# ./nginx -tnginx: [emerg] unexpected "}" in /usr/local/nginx/conf/nginx.conf:122nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
解決方法:下面語句忘記加分號
include vhost/*.conf;
2、使用 curl www.test*.com 測試時總是訪問到相同的結果
解決方法:不要將IP地址寫在 server_name 后面, server_name 后面只能添加域名。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答