Nginx和centos配置虚拟主机教程

前言

  • 一、什么是配置虚拟主机
  • 二、通过端口区分虚拟主机
  • 三、通过域名区分虚拟主机

阅读本文需要安装Nginx:https://www.cnblogs.com/huangyi-427/p/9229645.html

一、什么是配置虚拟主机

就是在一台服务器启动多个网站

二、通过端口区分虚拟主机

复制一份静态页面

cd /usr/local/nginx

cp -r html html81

修改部分内容以示区分

vim /usr/local/nginx/html81/index.html

Nginx和centos配置虚拟主机教程

查看配置文件

more /usr/local/nginx/conf/nginx.conf
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

Nginx和centos配置虚拟主机教程

可以通过配置多个server来配置多个虚拟主机

添加虚拟主机 将下面配置拷贝进去(与原有的server节点同级)

vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       81;
        server_name  localhost;

        location / {
            root   html81;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

进入sbin目录

cd /usr/local/nginx/sbin

开机状态刷新配置文件

./nginx -s reload

Nginx和centos配置虚拟主机教程

Nginx和centos配置虚拟主机教程

三、通过域名区分虚拟主机

域名: 域名就是网站

DNS服务器: 把域名解析为IP地址 保存的就是域名和IP的映射关系

注意: 一个域名对应一个IP地址 一个IP地址可以被多个域名绑定

这里我准备了2个域名

www.hb218.cn  www.hdcpa.cn

在阿里云上购买的 只有3-5天的使用期(可以续费) 总共花了2个大洋

买好域名之后需要在阿里云控制台 -> 云解析DNS -> 配置2个域名指向同一台nginx服务器(IP)

这里赞一下 马爸爸的阿里云平台啥都有 挺方便的

复制一份显示www.hb218.cn的静态页面

cd /usr/local/nginx

cp -r html html-hb218

修改部分内容以示区分

vim /usr/local/nginx/html-hb218/index.html

Nginx和centos配置虚拟主机教程

复制一份显示www.hdcpa.cn的静态页面

cd /usr/local/nginx

cp -r html html-hdcpa

修改部分内容以示区分

vim /usr/local/nginx/html-hdcpa/index.html

Nginx和centos配置虚拟主机教程

查看配置文件

more /usr/local/nginx/conf/nginx.conf
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

Nginx和centos配置虚拟主机教程

可以通过配置多个server来配置多个虚拟主机

添加虚拟主机 将下面配置拷贝进去(与原有的server节点同级)

vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  www.hb218.cn;

        location / {
            root   html-hb218;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen       80;
        server_name  www.hdcpa.cn;

        location / {
            root   html-hdcpa;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

进入sbin目录

cd /usr/local/nginx/sbin

开机状态刷新配置文件

./nginx -s reload

大功告成 浏览器分别访问www.hb218.cn  www.hdcpa.cn

请求

欢迎大家来学派吧投稿 欢迎加入我们学派吧QQ群 右上角

主题测试文章,只做测试使用。发布者:云大使,转转请注明出处:https://www.xp8.net/server/1006.html

(0)
打赏 微信扫一扫 微信扫一扫
云大使的头像云大使
上一篇 2018年10月16日 下午9:19
下一篇 2018年10月16日 下午10:57

相关推荐

  • Ubuntu系统ECS使用脚本之后如何处理出现数据库连接不上-linux运维

    本篇文章给大家带来的内容是关于Ubuntu系统ECS使用脚本之后如何处理出现数据库连接不上,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。 Ubuntu系统ECS使用“如何通过防火墙策略限制对外扫描行为”脚本之后出现无法远程、数据库连接不上 Ubuntu系统ECS使用“如何通过防火墙策略限制对外扫描行为”中的脚本进行安全防护后出现系统无法远程…

    服务器运维 2018年11月21日
    1.8K00
  • 将宝塔面板linux版装在/www以外的目录的方法

    很多云VPS系统盘,也就是根目录(系统盘)都是10个G,宝塔安装完整环境基本5个G没有了,用了不到几个月,随着日志文件等乱七八糟的东西增加,面板就提示根目录低于1个G,随后mysql就莫名其妙挂掉,这里就为大家分享一下将面板安装到别的目录的方法本人在饱受重装系统折磨之后,终于忍无可忍将宝塔面板安装到home下(home分区一般都很大) 宝塔面板官方的安装脚本…

    2018年7月9日
    2.5K00
  • apache如何做到日志分割-Linux运维-学派吧

    现在许多网站都是用 apache 做 web 代理的,随时间时间的推长,apache 的访问日志 access.log 会越来越大,所以如果能实现每天分割成一个新得日志,这是比较方便管理的。 当然,apache 本身就具备这样的功能,我需要在 apache 的配置文件中,每个网站的日志部分需要调整成如下的格式 httpd.conf:ErrorLog “| /…

    2018年9月30日
    2.0K00
  • 从阿里云域名解析到DNSPod,如何实现 DNS 平滑迁移?

    腾讯云解析 DNSPod 向全网域名提供快速、安全的智能解析服务,拥有精准调度、安全防护、容灾切换等能力,为您全面提升业务可用性、稳定性。 解析记录迁移 导出解析记录 进入后单击导入/导出,并选择导出记录 导出文件类型选择zone,并单击立即导出。 导出完成后重命名文件,添加.zone后缀,如下两图所示: 导入解析记录 添加需要迁移的域名,以dnstest.…

    2026年1月12日
    6500
  • 使用Linux/centos终端获取公共IP的5个命令教程

    公共IP用于计算机之间通过Internet进行通信。使用公共IP运行的计算机可以使用Internet访问世界各地。所以我们可以说它是互联网上计算机的身份。 现在的问题是,我们如何知道我们的公共知识产权?对于具有GUI的计算机,可以使用类似这样的Web工具轻松获得IP,但如何获得仅具有终端访问权限的计算机的公共IP。 解决方案:可以使用以下命令之一用Linux…

    2019年3月23日
    2.6K00

发表回复

登录后才能评论
联系我们

联系我们

18838889666

在线咨询: QQ交谈

邮件:xinyun@88.com

工作时间:周一至周五,9:30-18:30,节假日休息

添加微信
添加微信
分享本页
返回顶部
---------官方优惠叠加渠道折扣:通过我们购买腾讯云/阿里云,价格更低,服务更优。更有专业配置指导与服务。微信同步:18838889666----