zoukankan      html  css  js  c++  java
  • 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 
  • 相关阅读:
    寒宣资料汇编
    Windows邮件客户端
    Dear Menuhin
    2017-11-11 Sa Oct Spider
    2017-11-11 Sa Oct How to open a browser in Python
    skynet游戏服务器框架分享
    钉钉 机器人接入 自定义webhook
    golang语法笔记
    [学习笔记]尝试go-micro开发微服务<第一波>
    [学习笔记]Golang--基础数据类型
  • 原文地址:https://www.cnblogs.com/reycg-blog/p/7860327.html
Copyright © 2011-2022 走看看