• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

nginx 配置虚拟主机,实现在一个服务器可以访问多个网站的方法 ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

在一台服务器上,访问不同的网站

通常有两种区分方式:

1.通过监听的端口号

2.通过域名

1.通过端口访问不同的主机:

Nginx的配置文件:

/usr/local/nginx/conf/nginx.conf

Centos文件默认编码格式 latin1

查看编码格式的命令: :set fileencoding

#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节点
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 节点,即 你需要访问网站的配置
 #一个server节点,就是一个虚拟主机
  server {
    listen    80;  #监听的端口号,访问网站 默认是80端口
    server_name localhost;  #即访问的域名
    #charset koi8-r;
    #access_log logs/host.access.log main;
    location / {  #定位
      root  html;  #定位的是nginx根目录下的 html文件夹
      index index.html index.htm;  #设置网站首页
    }
  }
}

此时 可以配置多个server,也就是配置了不同的主机

添加虚拟主机:(通过端口号 区别)

server {
    listen    81;
    server_name localhost;
    #charset koi8-r;
    #access_log logs/host.access.log main;
    location / {
      root  html-81;
  #nginx根目录下 新建的html81 文件夹
      index index.html index.htm;
    ``
  }

编辑好文件之后,我们重新加载配置文件

通过命令: ./nginx -s reload

效果:

我们知道,当一个服务器上配置多个网站时,我们不可能通过端口号来区分它们,所以接下来 我需要通过域名来区分

2.通过域名区分不同的虚拟主机

什么是域名??

域名就是网址

例如:www.baidu.com

通常我们在访问域名的时候,我们需要通过dns服务器解析域名

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

一个域名对应一个ip地址,一个ip地址可以被多个域名绑定。

本地测试可以修改hosts文件。

修改window的hosts文件:(C:\Windows\System32\drivers\etc)

可以配置域名和ip的映射关系,如果hosts文件中配置了域名和ip的对应关系,不需要走dns服务器!!!!

在刚刚的nginx.conf文件下 继续配置:

server {
    listen    80;
    server_name www.taobao.com;
    #charset koi8-r;
    #access_log logs/host.access.log main;
    location / {
      root  html-taobao;
      index index.html index.htm;
    }
  }
  server {
    listen    80;
    server_name www.baidu.com;
    #charset koi8-r;
    #access_log logs/host.access.log main;
    location / {
      root  html-baidu;
      index index.html index.htm;
    }
  }
}

域名的配置:

192.168.25.148 www.test.com
192.168.25.148 www.yiyou.com

重启nginx服务

观察下效果:

以上这篇nginx 配置虚拟主机,实现在一个服务器可以访问多个网站的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持极客世界。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Nginx服务器配置HTTPS nginx.config 配置文件(教程)发布时间:2022-02-12
下一篇:
nginx https反向代理tomcat的2种实现方法发布时间:2022-02-12
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap