设为首页 收藏本站
查看: 745|回复: 0

[经验分享] nginx基于域名的虚拟主机实战配置

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-6-27 10:09:56 | 显示全部楼层 |阅读模式
实验环境:

操作系统: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目录下分别建立三个文件夹

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

[iyunv@localhost /]# tree /data0/www   #显示树状目录结构

[iyunv@localhost /]# chown -R nginx.nginx /data0/www  #授权

[iyunv@localhost conf]# echo www >/data0/www/www/index.html

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

[iyunv@localhost 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

[iyunv@localhost conf]# /application/nginx/sbin/nginx -s reload  #平滑重启

[iyunv@localhost 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.iyunv.com

192.168.222.135  bbs.iyunv.com  blog.iyunv.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.iyunv.com  iyunv.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.iyunv.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.iyunv.comm

       location / {

           root  /data0/www/blog;

           index index.html  index.htm;

           access_log  /app/logs/blog_access.log main;

       }

    }

[iyunv@localhost ~]# echo www.iyunv.com > /data0/www/www/index.html

[iyunv@localhost ~]# echo bbs.iyunv.com > /data0/www/bbs/index.html

[iyunv@localhost ~]# echo blog.iyunv.com > /data0/www/blog/index.html

[iyunv@localhost conf]# ../sbin/nginx –t   #检查语法

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

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

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

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

[iyunv@localhost 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

[iyunv@localhost sbin]# ./nginx -s reload  #平滑重启



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-235861-1-1.html 上篇帖子: Nginx应用实践入门 下篇帖子: Nginx报错403 forbidden (13: Permission denied)的解决办法 虚拟主机
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表