Nginx 虛擬主機
虛擬主機:把一臺物理服務器劃分成多個“虛擬”的服務器,每一個虛擬主機都可以有獨立的域名和獨立的目錄
Nginx 虛擬主機配置
通過 nginx.conf 中的 server 節點指定,要想設置多個虛擬主機,配置多個 server節點即可
可通過三種方式配置虛擬主機
- 基於域名
- 基於端口
- 基於 IP
基於域名
我們設置下面這三個域名
修改 hosts 文件
Windows host 文件位於 C:\Windows\System32\drivers\etc
Ip 地址是 Nginx 服務器所在地址
192.168.1.180 pic.example.com
192.168.1.180 dami01.example.com
192.168.1.180 dami02.example.com
修改 Nginx 配置文件
增加 server 字段
server {
listen 80;
server_name pic.example.com;
root /var/www/html/vhost/pic.example.com;
}
server {
listen 80;
server_name dami01.example.com;
root /var/www/html/vhost/dami01.example.com;
}
server {
listen 80;
server_name dami02.example.com;
root /var/www/html/vhost/dami02.example.com;
}
Nginx 服務器新建對應根目錄
$ mkdir -p /var/www/html/vhost/pic.example.com
$ mkdir -pv /var/www/html/vhost/dami0{1..2}.example.com
拷貝 index.html 到對應目錄並修改
[root@localhost dami01.example.com]# cat index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to dami01!</h1> ##修改成對應主機,便於標識
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
剩餘兩臺主機參考上面dami01,這裏不再贅述
重啓 Nginx
$ ./sbin/nginx -s reload
打開Web 瀏覽器
驗證