6.6. nginx虚拟主机配置
- nginx虚拟主机类型:
基于域名的虚拟主机
基于端口的虚拟主机
基于IP的虚拟主机
小技巧
nginx的虚拟主机功能不如apache强。
nginx配置虚拟机,一般通过子配置文件,然后包含进主配置文件的方式配置。这样的优点:1、配置文件结构清晰简单;2、便于维护;3、复用性高。
6.6.1. nginx虚拟主机配置实例
6.6.1.1. nginx默认配置的server区域分离
创建子配置文件的目录:
1[root@zzjlogin ~]# cd /usr/local/nginx/conf/
2[root@zzjlogin conf]# mkdir conf.d
把主配置文件的server区域分离出
修改后的主配置文件如下:
1[root@zzjlogin conf]# vi nginx.conf
2
3worker_processes 1;
4events {
5 worker_connections 10240;
6}
7http {
8 include mime.types;
9 include conf.d/*;
10 default_type application/octet-stream;
11 sendfile on;
12 keepalive_timeout 65;
13 error_page 500 502 503 504 /50x.html;
14}
在conf.d目录创建文件testserver.conf,然后输入server区域内容,具体如下:
1[root@zzjlogin conf]# cat >>conf.d/testserver.conf<<EOF
2> server {
3> listen 80;
4> #server_name localhost;
5> root /var/www/html;
6> location / {
7> # root html;
8> index index.html index.htm;
9> }
10> }
11> EOF
12[root@zzjlogin conf]# cat conf.d/testserver.conf
13server {
14 listen 80;
15 #server_name localhost;
16 root /var/www/html;
17 location / {
18 # root html;
19 index index.html index.htm;
20 }
21}
以上配置的webserver,就是监听在服务器本地的80端口的虚拟主机。可以通过多个conf.d目录下的配置文件来配置多个虚拟主机。
每个虚拟主机又可以是基于IP的或者基于域名的或者端口的。
注意
多个虚拟主机不能同时监听同一个本地端口。
6.6.1.2. 添加基于端口的虚拟主机
添加基于端口的虚拟主机:
1[root@zzjlogin conf]# cat >>conf.d/porthost1.conf<<EOF
2> server {
3> listen 8080;
4> #server_name localhost;
5> root /var/www/html/port/;
6> location / {
7> # root html;
8> index index.html index.htm;
9> }
10> }
11> EOF
12[root@zzjlogin conf]# cat conf.d/porthost1.conf
13server {
14 listen 8080;
15 #server_name localhost;
16 root /var/www/html/port/;
17 location / {
18 # root html;
19 index index.html index.htm;
20 }
21}
创建端口虚拟主机的站点测试文件及目录:
1[root@zzjlogin conf]# mkdir /var/www/html/port -p
2[root@zzjlogin conf]# echo "port host test">/var/www/html/port/index.html
检查配置:
1[root@zzjlogin conf]# ../sbin/nginx -t
2nginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is ok
3nginx: configuration file /usr/local/nginx-1.12.2/conf/nginx.conf test is successful
重载nginx配置文件:
1[root@zzjlogin conf]# ../sbin/nginx -s reload
检查监听服务:
1[root@zzjlogin conf]# ss -lntup|grep 'nginx'
2tcp LISTEN 0 128 *:8080 *:* users:(("nginx",4109,10),("nginx",13222,10))
3tcp LISTEN 0 128 *:80 *:* users:(("nginx",4109,6),("nginx",13222,6))
测试:
1[root@zzjlogin conf]# curl http://192.168.161.132
2test
3[root@zzjlogin conf]# curl -I http://192.168.161.132
4HTTP/1.1 200 OK
5Server: nginx/1.12.2
6Date: Tue, 16 Oct 2018 16:12:50 GMT
7Content-Type: text/html
8Content-Length: 5
9Last-Modified: Tue, 16 Oct 2018 10:22:58 GMT
10Connection: keep-alive
11ETag: "5bc5bc02-5"
12Accept-Ranges: bytes
13
14[root@zzjlogin conf]# curl http://192.168.161.132:8080
15port host test
16[root@zzjlogin conf]# curl -I http://192.168.161.132:8080
17HTTP/1.1 200 OK
18Server: nginx/1.12.2
19Date: Tue, 16 Oct 2018 16:13:07 GMT
20Content-Type: text/html
21Content-Length: 15
22Last-Modified: Tue, 16 Oct 2018 16:09:34 GMT
23Connection: keep-alive
24ETag: "5bc60d3e-f"
25Accept-Ranges: bytes
6.6.1.3. 添加基于域名的虚拟主机
注意
基于域名的虚拟主机的配置中的 server_name 指定的域名需要在DNS有对应的A记录,或者通过本地主机的hosts文件指定这个域名和IP的映射关系。
本实例就是通过hosts文件测试。linux是/etc/hosts文件。windows系统是C:WindowsSystem32driversetchosts,windows10需要新建一个hosts文件。
hosts文件添加一条内容:192.168.161.132 www.mysite.com
windows如果清空本地DNS缓存需要运行CMD,然后输入命令: ipconfig /flushdns
增加域名的虚拟主机配置文件:
小技巧
现在是一个nginx程序监听多个端口。 这个80是默认配置的站点主机配置端口。 8080是端口虚拟主机的端口。
域名虚拟主机还可以继续复用这些端口。但是不同的域名虚拟主机之间不能使用相同的端口。
1[root@zzjlogin conf]# cat >>conf.d/mysite.conf<<EOF
2> server {
3> listen 80;
4> server_name www.mysite.com;
5> root /var/www/html/port/;
6> location / {
7> # root html;
8> index index.html index.htm;
9> }
10> }
11> EOF
12[root@zzjlogin conf]# cat conf.d/porthost1.conf
13server {
14 listen 80;
15 server_name www.mysite.com;
16 root /var/www/html/mysite/;
17 location / {
18 # root html;
19 index index.html index.htm;
20 }
21}
创建域名虚拟主机的测试站点文件:
1[root@zzjlogin conf]# mkdir /var/www/html/mysite -p
2[root@zzjlogin conf]# echo "www.mysite.com">/var/www/html/mysite/index.html
检查配置并重新加载配置:
1[root@zzjlogin conf]# ../sbin/nginx -t
2nginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is ok
3nginx: configuration file /usr/local/nginx-1.12.2/conf/nginx.conf test is successful
4[root@zzjlogin conf]# ../sbin/nginx -s reload
此时本地电脑因为配置了hosts文件中的域名和IP的映射。所以通过浏览器浏览www.mysite.com这个网站就看到前面的测试indext.html页面内容。