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

相关推荐

  • 学派吧-Linux/centos中SSH客户端断开后保持进程继续运行配置方法的具体教程

    在云服务器 ECS Linux 系统中,通常我们在执行一些运行时间比较长的任务时,必须等待执行完毕才能断开 SSH 连接或关闭客户端软件,否则可能会导致执行中断。本文介绍几种保障程序在用户退出登录后持续运行的方法。 使用管理终端执行 通过 管理终端 会登录服务器的本地会话(console)口,在该终端执行的程序不会受到 SSH 登录用户退出的影响。具体操作方…

    服务器运维 2018年12月12日
    3.8K00
  • Linux解压gz文件的命令使用教程及详解

    今天小编给带来一篇linux解压方面的资料 Linux解压gz文件的命令有:gunzip命令、gzip命令、tar命令。下面本篇文章就来带大家了解一下 gz文件是一种压缩文件,以.gz或者.tar.gz(.tgz)为扩展名,在Linux、UNIX和OSX下常见,Linux和OSX都可以直接解压使用这种压缩文件。在Windows下常用压缩软件WinRAR打开g…

    2019年3月13日
    87.4K00
  • 分享Linux/centos下如何查看环境变量-学派吧

    本篇文章主要给大家介绍linux下查看环境变量的命令方法,希望对需要的朋友有所帮助! 环境变量的查看 1.使用echo命令查看单个环境变量。例如: echo $PATH 2.使用env查看所有环境变量。例如: env 3.使用set查看所有本地定义的环境变量。 常用的环境变量: PATH 决定了shell将到哪些目录中寻找命令或程序 HOME 当前用户主目录…

    服务器运维 2019年5月7日
    5.1K00
  • 使用 htop 命令报错:-bash: htop: command not found

    今天通过htop命令查看下服务器状态、居然提示-bash: htop: command not found 后来看看没有这个工具。那我们进行安装下1: yum -y install htop 由于htop是一个扩展工具,是一个强大的进程管理前端工具,有的系统优化去掉了 。2、第二个解决办法步骤一:yum install -y epel-release步骤二:…

    2018年8月26日
    8.3K00
  • 关于win2008 IP安全策略关闭端口、禁止ping、修改3389端口、开放指定端口的方法-windows教程-学派吧

    这篇文章主要介绍了windows server 2008 IP安全策略关闭端口,禁止ping,修改远程连接3389端口,开放指定端口,需要的朋友可以参考下 windows server 2008 IP安全策略关闭端口: Tomcat服务访问不了情况解决。 Windows默认情况下有很多端口是开放的,在你上网的时候,网络病毒和黑客可以通过这些端口连上你的电脑。…

    服务器运维 2018年12月2日
    3.2K00

发表回复

登录后才能评论
联系我们

联系我们

18838889666

在线咨询: QQ交谈

邮件:xinyun@88.com

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

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