nginx 虚拟主机配置

1. 什么是虚拟主机,分成几种类型

虚拟主机,就是一个独立的网站站点,它可以独立的提供服务从而供用户访问,nginx 上使用 server{} 来标识一个虚拟主机,一个web 服务里也可以有多个虚拟主机。

其类型包括

  • 基于 IP 的虚拟主机
  • 基于端口的虚拟主机
  • 基于域名的虚拟主机

 

2. 基于域名的虚拟主机配置

2.1  Linux 中配置主机域名

[root@vultr conf]# echo "198.xxx.xxx.xxx www.reycgNginxTest.com" >> /etc/hosts

2.2 编辑 nginx.conf

[root@vultr ~]# cd /opt/nginx/conf/
[root@vultr conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf
[root@vultr conf]# vim nginx.conf

编辑后的结果

[root@vultr conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.reycgNginxTest.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

2.3 创建站点目录以及文件

从上面配置 nginx.conf 文件内容中可看出,配置的站点目录是 html/www

而这个目录默认是没有创建的,我们需要创建该目录,以及里面的 index.html 文件

[root@vultr conf]# cd /opt/nginx/html/
[root@vultr html]# ls
50x.html  index.html
[root@vultr html]# mkdir www
[root@vultr html]# cd www/
[root@vultr www]# echo "Hello I'm ReyCG Nginx Test" > index.html
[root@vultr www]# cat index.html 
Hello I'm ReyCG Nginx Test

2.4 重新加载 nginx

[root@vultr www]# cd /opt/nginx/sbin/
[root@vultr sbin]# ./nginx -t
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
[root@vultr sbin]# ./nginx -s reload

使用 linux 命令插件 nginx 重载后的结果

[root@vultr sbin]# ps -ef|grep nginx 
root     14701     1  0 08:01 ?        00:00:00 nginx: master process ./nginx
nobody   14985 14701  0 08:46 ?        00:00:00 nginx: worker process
root     14999 14719  0 08:48 pts/1    00:00:00 grep --color=auto nginx

[root@vultr sbin]# netstat -lntp |grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      14701/nginx: master 

2.5 Windows 客户端配置域名

使用 switchHosts 配置

198.xxx.xxx.xxx www.reycgNginxTest.com

2.6 访问域名

 3. 配置多个基于域名的虚拟主机

3.1 主机域名配置(包括 windows客户端,linux服务器)

198.xxx.xxx.xxx  www.reycgNginxTest.com bbs.reycgNginxTest.com

3.2 添加新域名对应的配置信息

nginx 配置文件当前的配置为

[root@vultr nginx]# vim conf/nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.reycgNginxTest.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
} server { listen
80; server_name bbs.reycgNginxTest.com; location / { root html/bbs; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }

3.3 后续步骤

[root@vultr nginx]# mkdir -p html/bbs
[root@vultr nginx]# echo "Hello, I'm bbs\!" > html/bbs/index.html
[root@vultr sbin]# ./nginx -t
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
[root@vultr sbin]# ./nginx -s reload
[root@vultr sbin]# ps -ef|grep nginx
root     14701     1  0 08:01 ?        00:00:00 nginx: master process ./nginx
nobody   15159 14701  0 09:17 ?        00:00:00 nginx: worker process
root     15164 14719  0 09:18 pts/1    00:00:00 grep --color=auto nginx
[root@vultr sbin]# netstat -lntp|grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      14701/nginx: master 

 

posted @ 2017-11-19 16:59  ReyCG  阅读(379)  评论(0编辑  收藏  举报