当前位置: 首页 > news >正文

nginx配置虚拟主机

准备站点目录:

我们站点统一放到/data/site下,每个站点根目录名称都和域名相同,具体如下。

新建a.deng.com站点根目录:

# mkdir -pv /data/site/a.deng.com

新建站点a.deng.com主页

# echo "this is a.deng.com" >> /data/site/a.deng.com/index.html

新建b.deng.com站点根目录:

# mkdir -pv /data/site/b.deng.com

新建站点b.deng.com主页

# echo "this is b.deng.com" >> /data/site/b.deng.com/index.html

新建统一日志目录

我们统一讲日志存放到/data/logs下,这边是存放nginx日志,所以nginx日志保持在当前的nginx目录下.日志统一存放相对来说比较规范

# mkdir -p /data/logs/nginx

创建虚拟主机配置文件:

# mkdir -p /usr/local/nginx/conf/vhosts

在nginx.conf的http{}中增加:

include /usr/local/nginx/conf/vhosts/*.conf;

配置虚拟主机:

增加nginx.conf的配置---配置日志格式,去掉#注释符

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '

# '$status $body_bytes_sent "$http_referer" '

# '"$http_user_agent" "$http_x_forwarded_for"';

配置nginx的虚拟主机文件:

# vim /usr/local/nginx/conf/vhosts/a.deng.conf

server {

listen 80;

server_name a.deng.com;

root /data/site/a.deng.com;

index index.php index.htm index.html;

location ~ \.php$ {

include fastcgi_params;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

}

access_log /data/logs/nginx/a.deng.com-access.log main;

}

# vim /usr/local/nginx/conf/vhosts/b.deng.conf

server {

listen 80;

server_name b.deng.com;

root /data/site/b.deng.com;

index index.php index.htm index.html;

location ~ \.php$ {

include fastcgi_params;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

}

access_log /data/logs/nginx/b.deng.com-access.log main;

}

  • 配置讲解

server{}:配置虚拟主机必须有这个段。

server_name:虚拟主机的域名,可以写多个域名,类似于别名,比如说你可以配置成

server_name b.deng.com c.deng.com d.deng.com,这样的话,访问任何一个域名,内容都是一样的

listen 80,监听ip和端口,这边仅仅只有端口,表示当前服务器所有ip的80端口,如果只想监听127.0.0.1的80,写法如下:

listen 127.0.0.1:80

root /data/site/b.deng.com:站点根目录,你网站文件存放的地方。注:站点目录和域名尽量一样,养成一个好习惯

access_log /data/logs/nginx/b.deng.com-access.log main:访问日志

重启并打开站点

nginx -t 检查nginx配置是否ok,命令如下:

# /usr/lcoal/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

如果看到以上两行ok和successful就表示配置问题,那接下来我们启动nginx

启动nginx

# /usr/local/nginx/sbin/nginx

使用curl进行访问网站,先在linux下绑定hosts,进行DNS解析:

vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.0.139  a.deng.com b.deng.com

[root@localhost vhosts]# curl a.deng.com

this is a.deng.com

[root@localhost vhosts]# curl b.deng.com

this is b.deng.com



本文转自 IT_外卖小哥  51CTO博客,原文链接:http://blog.51cto.com/jinlong/1953539

相关文章:

  • mysql日志文件在哪
  • 21天让你成为Horizon View高手—Day7:配置View Connection Server
  • c用libcurl库实现https下get/post网络通信
  • bootstrap标题效果
  • nmap的用法
  • java 二叉树 深度优先递归遍历
  • 基于nginx部署app下载服务器
  • itunes Connect 未能创建 App 图标
  • Exchange(2007/2010/2013)共存环境中IMAP和POP的工作方式
  • 第二章 索引
  • 金山词霸PDF文档取词
  • 如何使用OpenSSL自签证书(Self-Sign Certificate)
  • 为什么,博主我要写下这一系列windows实用网络?
  • CentOS系统中出现错误--SSH:connect to host centos-py port 22: Connection refused
  • 笔记本外接显示器切换失败原因
  • 【css3】浏览器内核及其兼容性
  • 【vuex入门系列02】mutation接收单个参数和多个参数
  • 【前端学习】-粗谈选择器
  • 【译】理解JavaScript:new 关键字
  • 002-读书笔记-JavaScript高级程序设计 在HTML中使用JavaScript
  • CEF与代理
  • CSS魔法堂:Absolute Positioning就这个样
  • JAVA 学习IO流
  • mongo索引构建
  • MySQL数据库运维之数据恢复
  • PHP的Ev教程三(Periodic watcher)
  • Python实现BT种子转化为磁力链接【实战】
  • TypeScript迭代器
  • vue--为什么data属性必须是一个函数
  • 阿里云应用高可用服务公测发布
  • 程序员该如何有效的找工作?
  • 第三十一到第三十三天:我是精明的小卖家(一)
  • - 概述 - 《设计模式(极简c++版)》
  • 买一台 iPhone X,还是创建一家未来的独角兽?
  • 实习面试笔记
  • 微信端页面使用-webkit-box和绝对定位时,元素上移的问题
  • Nginx惊现漏洞 百万网站面临“拖库”风险
  • 积累各种好的链接
  • ​香农与信息论三大定律
  • # 手柄编程_北通阿修罗3动手评:一款兼具功能、操控性的电竞手柄
  • ###51单片机学习(1)-----单片机烧录软件的使用,以及如何建立一个工程项目
  • #QT(TCP网络编程-服务端)
  • $().each和$.each的区别
  • (1)虚拟机的安装与使用,linux系统安装
  • (2009.11版)《网络管理员考试 考前冲刺预测卷及考点解析》复习重点
  • (51单片机)第五章-A/D和D/A工作原理-A/D
  • (Bean工厂的后处理器入门)学习Spring的第七天
  • (BFS)hdoj2377-Bus Pass
  • (iPhone/iPad开发)在UIWebView中自定义菜单栏
  • (java版)排序算法----【冒泡,选择,插入,希尔,快速排序,归并排序,基数排序】超详细~~
  • (八)Spring源码解析:Spring MVC
  • (二)JAVA使用POI操作excel
  • (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画...
  • (附源码)springboot 智能停车场系统 毕业设计065415
  • (转)大道至简,职场上做人做事做管理