千家信息网

LNMP架构中Nginx如何配置虚拟主机

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,这篇文章给大家分享的是有关LNMP架构中Nginx如何配置虚拟主机的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。1、nginx虚拟主机简单介绍同apache服务一样,它也有
千家信息网最后更新 2025年02月03日LNMP架构中Nginx如何配置虚拟主机

这篇文章给大家分享的是有关LNMP架构中Nginx如何配置虚拟主机的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

1、nginx虚拟主机简单介绍

同apache服务一样,它也有三种不同的虚拟主机,基于域名的虚拟主机、基于IP的虚拟主机与基于端口的虚拟主机,至于其中的区别,请参考前面的 apache服务器的虚拟主机章节的相关介绍



2、nginx虚拟主机配置环境

系统环境

[root@centos6 scripts]# cat /etc/redhat-release

CentOS release 6.5 (Final)

[root@centos6 scripts]# uname -r

2.6.32-431.el6.x86_64

nginx服务的版本

[root@centos6 scripts]# /application/nginx/sbin/nginx -v

nginx version: nginx/1.10.1



3、nginx虚拟主机配置准备

生产环境的规范很重要,这是运维的重点,因此,配置前创建站点目录

[root@centos6 ~]# mkdir /www/{www,bbs,blog} -p

[root@centos6 ~]# tree /www

/www

+-- bbs

+-- blog

+-- www

3 directories, 0 files

用于存放站点文件的目录

---------------

授权目录

--------------

[root@centos6 ~]# chown -R nginx.nginx /www

[root@centos6 ~]# ll /www/

total 12

drwxr-xr-x. 2 nginx nginx 4096 Dec 4 18:38 bbs

drwxr-xr-x. 2 nginx nginx 4096 Dec 4 18:38 blog

drwxr-xr-x. 2 nginx nginx 4096 Dec 4 18:38 www

------------------------------------------------------

接下来写点东东进去吧,用于后面的测试

-----------------------------------------------------

[root@centos6 ~]# echo "welcont to mingongge's web stie" >/www/www/index.html

[root@centos6 ~]# echo "welcont to mingongge's bbs stie" >/www/bbs/index.html

[root@centos6 ~]# echo "welcont to mingongge's blog stie" >/www/blog/index.html

[root@centos6 ~]# cat /www/www/index.html

welcont to mingongge's web stie

[root@centos6 ~]# cat /www/bbs/index.html

welcont to mingongge's bbs stie

[root@centos6 ~]# cat /www/blog/index.html

welcont to mingongge's blog stie

生产环境检查也同样很重要,检查、检查 、检查,重要的事情说三遍!!!!



4、nginx虚拟主机配置

配置nginx 虚拟主机有两种方式,一种可以像前面apache服务这种,单独配置一个虚拟主机的配置文件,另一种也可以在主配置文件 nginx.conf中添加server标签,接下来介绍的是第一种方式,通过在配置文件里添加包含关系,单独配置虚拟主机的配置文件目录与配置文件,这样实际生产环境中比较常见,服务多了,也容易维护。

--------------------

配置主配置文件

-------------------

只需要在主配置文件nginx.conf最后一行加下如下配置

include extra/vhosts/*.conf;

-----------------------------

创建虚拟主机配置目录

----------------------------

[root@centos6 conf]# pwd

/application/nginx/conf

[root@centos6 conf]# mkdir -p extra/vhosts

-----------------------------

创建虚拟主机配置文件

----------------------------

[root@centos6 conf]# cd extra/vhosts/

[root@centos6 vhosts]# mkdir bbs www blog

[root@centos6 vhosts]# cp ../../nginx.conf www/www.conf

[root@centos6 vhosts]# cp ../../nginx.conf bbs/bbs.conf

[root@centos6 vhosts]# cp ../../nginx.conf blog/blog.conf


通过主配置文件来修改,其实只需要里面的server标签的内容,同样也可以做一个模板文件出来,通过cp命令改名后,再修改其内容,效果都一样


-----------------------------

配置虚拟主机配置文件

----------------------------


WWW站点虚拟主机配置文件(比较简单)

server {

listen 80;

server_name www.mingongge.com;

location / {

root /www/www;

index index.html index.htm;

}

}

基它的相同,只需要改下站点目录路径与域名信息


BBS站点虚拟主机配置文件

server {

listen 80;

server_name bbs.mingongge.com;

location / {

root /www/bbs;

index index.html index.htm;

}

}


BLOG站点虚拟主机配置文件

server {

listen 80;

server_name blog.mingongge.com;

location / {

root /www/blog;

index index.html index.htm;

}

}


5、重启服务与测试访问

[root@centos6 vhosts]# /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

[root@centos6 vhosts]# /application/nginx/sbin/nginx -s reload

[root@centos6 vhosts]# lsof -i :80

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME

nginx 2469 root 6u IPv4 15462 0t0 TCP *:http (LISTEN)

nginx 2519 nginx 6u IPv4 15462 0t0 TCP *:http (LISTEN)

[root@centos6 vhosts]# ps -ef|grep nginx

root 2469 1 0 18:47 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx

nginx 2519 2469 0 19:14 ?00:00:00 nginx: worker process


打开浏览器测试访问吧

本地DNS解析不要忘记了,否则无法通过域名来访问的


感谢各位的阅读!关于"LNMP架构中Nginx如何配置虚拟主机"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

配置 主机 虚拟主机 文件 目录 站点 服务 环境 内容 检查 重要 域名 测试 生产 架构 接下来 方式 更多 标签 篇文章 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 好搭档网络技术 自研数据库投资 不灵姐和小月服务器生存5集 智慧供热网络安全 小李是单位的网络技术员 网络安全生态社会和谐论文 一个教学管理数据库要求如下 传输网络安全隐患 新兴县淘正网络技术有限公司 石家庄广尚网络技术怎么样 未来之役服务器怎么选欧服 浏览器被设置为使用代理服务器 平安产险互联网科技事业部 计算机科学计算的网络技术 龙旭网络技术公司长春分公司 暗网网站会被服务器 哈霍兰服务器开服时间 uml软件开发重要吗 计算机网络安全研究方法 云会务服务器管理系统 网页下载软件开发 为什么服务器越来越不稳 网络安全部队专业 论文里数据库的表应该是什么样 网络技术服务税点 株洲管理软件开发价格 计算机科学计算的网络技术 数据库加字符串有什么用 建党一百周年 网络安全 浙江橙功营网络技术有限公司
0