nginx基于域名的虚拟主机实战配置

实验环境:

操作系统:CentOS release 6.8 (Final)

Web服务器:nginx-1.10.1

工具:VMware Workstation 10.0.1 build-1379776

实战任务:配置nginx.conf文件

本节内容在生产场景中是最常用到的,因此,系统工程师、运维工程师、Linux运维等专业技术人员要优先并且熟练掌握。

# mkdir /data0/www/{www,bbs,blog} –p   #在www目录下分别建立三个文件夹

[[email protected] www]# for n in www blog bbs;do echo "$n">/data0/www/$n/index.html;done #将www,blog,bbs分别写入三个目录中的index.html文件中;

[[email protected] /]# tree /data0/www   #显示树状目录结构

[[email protected] /]# chown -R nginx.nginx /data0/www  #授权

[[email protected] conf]# echo www >/data0/www/www/index.html

#将www写入index.html文件中。

[[email protected] conf]# /application/nginx/sbin/nginx -t  #检查语法

nginx: the configuration file /application/nginx-1.10.1/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.10.1/conf/nginx.conf test is successful

[[email protected] conf]# /application/nginx/sbin/nginx -s reload  #平滑重启

[[email protected] conf]# lsof -i :80 #检查80端号,开启了8个进程

COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

nginx   1644  root    6u  IPv4  11420      0t0  TCP *:http (LISTEN)

nginx   3304 nginx    6u  IPv4  11420      0t0  TCP *:http (LISTEN)

nginx   3305 nginx    6u  IPv4  11420      0t0  TCP *:http (LISTEN)

nginx   3306 nginx    6u  IPv4  11420      0t0  TCP *:http (LISTEN)

nginx   3307 nginx    6u  IPv4  11420      0t0  TCP *:http (LISTEN)

nginx   3308 nginx    6u  IPv4  11420      0t0  TCP *:http (LISTEN)

nginx   3309 nginx    6u  IPv4  11420      0t0  TCP *:http (LISTEN)

nginx   3310 nginx    6u  IPv4  11420      0t0  TCP *:http (LISTEN)

nginx   3311 nginx    6u  IPv4  11420      0t0  TCP *:http (LISTEN)

配置hosts文件测试

如果域名没有做正式DNS解析,我们可以在我位的笔记本电脑上编辑hosts文件,添加如下内容在本地进行host解析。

Host文件的通用路径为:

%systemroot%\system32\drivers\etc\hosts #开始——》运行

#host文件一般比喻为本地的DNS文件,其功能是把指定域名解析成对应的IP,多个域名可以对应一个IP,默认情况下hosts文件中的配置解析优先于DNS服务器。公司里做开发测试等环节会普遍应用这个host文件,简单而方便。

如果经常使用该文件,可以在桌面或任务栏建立个hosts快捷方式,省得每次找起来费劲,同时也有小软件或插件,可以实现帮你快速修改。

需要加入的域名和你配置的机器的对应解析:

192.168.222.135  www.51cto.com

192.168.222.135  bbs.51cto.com  blog.51cto.com

在nginx.conf文件配置:

#将下面的#去掉,注意main类型,与错误日志文件类型一致

user nginx nginx;

worker_processes  8;    #设置了同时8个进程任务

log_format   main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘

‘$status $body_bytes_sent "$http_referer" ‘

‘"$http_user_agent" "$http_x_forwarded_for"‘;

server{

listen 80;

server_name  www.51cto.com  51cto.com   #域名

location / {

root  /data0/www/www;     #站点目录

index index.html  index.htm;

access_log  /app/logs/www_access.log main;

}

}

###

server{

listen 80;

server_name  bbs.51cto.com

location / {

root  /data0/www/bbs;

index index.html  index.htm;

access_log  /app/logs/bbs_access.log main;

}

}

###

server{

listen 80;

server_name  blog.51cto.comm

location / {

root  /data0/www/blog;

index index.html  index.htm;

access_log  /app/logs/blog_access.log main;

}

}

[[email protected] ~]# echo www.51cto.com > /data0/www/www/index.html

[[email protected] ~]# echo bbs.51cto.com > /data0/www/bbs/index.html

[[email protected] ~]# echo blog.51cto.com > /data0/www/blog/index.html

[[email protected] conf]# ../sbin/nginx –t   #检查语法

nginx: [warn] server name "http://sky9896.blog.51cto.com/" has suspicious symbols in /application/nginx-1.10.1/conf/nginx.conf:83

nginx: [warn] server name "http://sky9896.blog.51cto.com/" has suspicious symbols in /application/nginx-1.10.1/conf/nginx.conf:97

#在nginx.conf配置域名中:server_name  http://sky9896.blog.51cto.com/;

不需要添加http,只要改成:server_name  sky9896.blog.51cto.com;即可

[[email protected] sbin]# ./nginx -t

nginx: the configuration file /application/nginx-1.10.1/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.10.1/conf/nginx.conf test is successful

[[email protected] sbin]# ./nginx -s reload  #平滑重启

测试页面:

[[email protected] log]# pwd

/app/log

[[email protected] log]# ll   #下面是对应的三个站点目录日志文件

总用量 12

-rw-r--r-- 1 root root 2708 6月  26 14:31 bbs_access.log

-rw-r--r-- 1 root root 3358 6月  26 14:32 blog_access.log

-rw-r--r-- 1 root root  848 6月  26 14:31 www_access.log

时间: 2024-08-01 11:47:12

nginx基于域名的虚拟主机实战配置的相关文章

nginx基于域名的虚拟主机配置

与apache服务器类似,nginx也有基于域名,IP及端口的虚拟主机配置,在实际工作场景中,基于域名的虚拟主机配置较常见.nginx服务的主要配置文件nginx.conf[[email protected] conf]# ls -l nginx.conf-rw-r--r-- 1 root root 2788 Jan 14 17:41 nginx.conf[[email protected] conf]# pwd/application/nginx/conf 去掉注释及空行后的配置文件[[ema

Nginx基于域名的虚拟主机

1.1 问题 沿用练习二,配置基于域名的虚拟主机,实现以下目标: 实现两个基于域名的虚拟主机,域名分别为www.aa.com和www.bb.com 对域名为www.aa.com的站点进行用户认证,用户名称为tom,密码为123456 1.2 方案 修改Nginx配置文件,添加server容器实现虚拟主机功能:对于需要进行用户认证的虚拟主机添加auth认证语句. 3.3 步骤 实现此案例需要按照如下步骤进行. 步骤一:修改配置文件 1)修改Nginx服务配置,添加相关虚拟主机配置如下 [[emai

nginx基于域名的虚拟主机配置实战

背景: 在www虚拟主机站点基础上新增一个bbs虚拟主机站点. 1    备份配置文件 [[email protected] conf]# pwd /application/nginx/conf [[email protected] conf]# cp nginx.conf{,.oldboy.2017.0819} 2    编辑nginx.conf配置文件,新增bbs虚拟主机站点 [[email protected] conf]# vim nginx.conf.oldboy.20170819 

nginx服务做用户认证和基于域名的虚拟主机

实验一.用nginx怎么实现用户访问时的认证 一.目标        通过调整Nginx服务端配置,实现以下目标: 访问Web页面需要进行用户认证 用户名为:tom,密码为:123456 二.方案         通过Nginx实现Web页面的认证,需要修改Nginx配置文件,在配置文件中添加auth语句实现用户认证.    最后使用htpasswd命令创建用户及密码即可,服务端:192.168.4.102,客户端:192.168.4.101 三.实施步骤(nginx服务安装见我的"搭建ngin

Nginx配置多个基于域名的虚拟主机+实验环境搭建+测试

标签:Linux 域名 Nginx 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://xpleaf.blog.51cto.com/9315560/1901284 0.说明 使用Nginx可以配置基于域名的虚拟主机.基于端口的虚拟主机和基于端口的虚拟主机,比较常用的是基于域名的虚拟主机,这里要做的配置是基于域名的虚拟主机,并且是配置多个基于域名的虚拟主机. 关于Nginx配置文件的说明可以参考官方文档,同时也可以参考老男孩老师的书

13_搭建Nginx服务器、配置网页认证、基于域名的虚拟主机、ssl虚拟主机

官方yum源:[nginx]name=nginx repobaseurl=http://nginx.org/packages/centos/$releasever/$basearch/gpgcheck=0enabled=1 pc71. 安装nginx]# yum -y install nginx]# nginx]# nginx -Vnginx version: nginx/1.16.1]# netstat -anptu | grep nginx]# curl http://10.10.11.10

CentOS7.4—nginx应用之基于域名的虚拟主机

Nginx功能应用-虚拟主机目录:第一部分:准备工作第二部分:搭建nginx第三部分:搭建基于域名的虚拟主机 第一部分 准备工作一:服务器:Linux系统-CentOS 7.4:IP地址:192.168.80.10 客户端:以WIN7为例,测试验证结果,与服务器在同一网段:IP地址:192.168.80.2 二:准备压缩包 三:将防火墙与selinux关闭 第二部分 安装Nginx服务一:安装编译工具与插件[[email protected] ~]# yum -y install \ gcc \

nginx的简介和搭建基于域名的虚拟主机

今天就来和大家讲一讲nginx和基于域名搭建虚拟主机 简介 Nginx (engine x) 是一个高性能的Web服务器和反向代理服务器,也是一个IMAP/POP3/SMTP服务器 俄罗斯程序员Igor Sysoev于2002年开始Nginx是增长最快的Web服务器,市场份额已达33.3%全球使用量排名第二2011年成立商业公司 Nginx源码结构: 代码量大约11万行C代码源代码目录结构core (主干和基础设置)event (事件驱动模型和不同的IO复用模块)http (HTTP服务器和模块

CentOS 7运维管理笔记(7)----Apache基于域名的虚拟主机配置

使用基于域名的虚拟主机配置是比较流行的方式,可以在同一个IP上配置多个域名并且都通过80端口访问. (1) 在网卡 eth0的第五个接口上配置 192.168.1.215 这个地址: (2) 配置/etc/hosts文件,192.168.1.215 对应的域名如下: 做ping测试,保证ip是导通的: (3) 建立虚拟主机存放网页的根目录,并创建首页文件的 index.html 文件 (4)修改 /usr/local/apache2/conf/httpd.conf 文件,使得服务器开始Liste