首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >nginx.conf详解和Nginx的虚拟主机

nginx.conf详解和Nginx的虚拟主机

作者头像
张琳兮
发布2019-03-14 11:59:07
5460
发布2019-03-14 11:59:07
举报
文章被收录于专栏:首富手记首富手记
cat /etc/nginx/nginx.conf
worker_processes  1;
#定义有多少个工作的子进程,可自行修改,太大无益,因为要争夺CPU,一般设置为核心总数(lscpu中CPU(S)可看)
events {
    worker_connections  1024;
    #一个worker允许同时最大产生多少个链接
}
http {          #Web功能的标签
    include       conf/mime.types;
    #设定mime类型,类型由conf/mime.types决定
    default_type  application/octet-stream;
    sendfile        on;
    #sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设
    为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。
    keepalive_timeout  65;
    #http长连接的断开时间,(长连接:一次建立TCP链接多次请求)从最后一次请求开始计算时间
    server {        #虚拟主机的标签
        listen       80;
        #侦听的端口
        server_name  localhost;
        #服务器的名称,别人可以通过这个来访问你的网站
        location / {
            root   html;
            #网站的根目录,如果是绝对路径,就是对于nginx服务器的家目录来说的
            index  index.html index.htm;
            #网站默认的查找首页,从左向右依次查找
        }
        error_page   500 502 503 504  /50x.html;
        #如果碰到上述状态码,则转交给后面的50x.html页面
        location = /50x.html {
            root   html;
        }
    }
}

基于IP地址的虚拟主机

在http标签里面添加上一对server标签
    server {
        listen 80;
        server_name 10.0.0.7;
        access_log logs/ip_access.log main;
        location / {
            root html/server_ip;
            index index.html
        }
    }
保存退出,检查语法
[root@WNginx01_7 nginx]# nginx  -c /usr/local/nginx/conf/nginx.conf -t 
2018/08/24 11:15:36 [info] 9888#0: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
2018/08/24 11:15:36 [info] 9888#0: the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
平滑重启(重读配置文件)
[root@WNginx01_7 nginx]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
创建虚拟主机的网站根目录,刚才在配置文件里面定义的
[root@WNginx01_7 nginx]# mkdir /usr/local/nginx/html/server_ip
进入到这个里面编辑网站的主页
[root@WNginx01_7 nginx]# cat /usr/local/nginx/html/server_ip/index.html
<html>
<center><h1>Welcome to 10.0.0.7 server test</h1></center>
</html>
#然后在浏览器上输入IP地址,发现内容正确显示,基于IP的虚拟主机配置完成
nginx.conf详解和Nginx的虚拟主机
nginx.conf详解和Nginx的虚拟主机
基于端口的虚拟主机(生产环境中可以通过这种方法给网站单独开个后台,增加安全性)
在http标签里面添加上一对server标签
    server {
        listen 8888;
        server_namelocalhost;
        access_log logs/8888_port_access.log main;
        location / {
            root html/port;
            index index.html
        }
    }
保存退出,检查语法
[root@WNginx01_7 nginx]# nginx  -c /usr/local/nginx/conf/nginx.conf -t 
2018/08/24 11:15:36 [info] 9888#0: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
2018/08/24 11:15:36 [info] 9888#0: the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
平滑重启(重读配置文件)
[root@WNginx01_7 nginx]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
创建虚拟主机的网站根目录,刚才在配置文件里面定义的
[root@WNginx01_7 nginx]# mkdir /usr/local/nginx/html/port
进入到这个里面编辑网站的主页
[root@WNginx01_7 nginx]# cat /usr/local/nginx/html/port/index.html
<html>
<center><h1>Welcome to 8888 server test</h1></center>
</html>
然后在浏览器上输入IP地址加端口号,发现内容正确显示,基于port的虚拟主机配置完成
![](https://s1.51cto.com/images/blog/201809/29/40383a0d5dd2e15c944f4c73b40206f8.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
查看服务器侦听的端口
![](https://s1.51cto.com/images/blog/201809/29/93977800f00dc1a94d45e3598e9e2c70.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
基于域名的虚拟主机
在http标签里面添加上一对server标签
    server {
        listen 80;
        server_name www.sentinel.com;
        access_log logs/sentinel_access.log main;
        location / {
            root html/sentinel;
            index index.html
        }
    }
保存退出,检查语法
[root@WNginx01_7 nginx]# nginx  -c /usr/local/nginx/conf/nginx.conf -t 
2018/08/24 11:15:36 [info] 9888#0: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
2018/08/24 11:15:36 [info] 9888#0: the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
平滑重启(重读配置文件)
[root@WNginx01_7 nginx]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
创建虚拟主机的网站根目录,刚才在配置文件里面定义的
[root@WNginx01_7 nginx]# mkdir /usr/local/nginx/html/sentinel
进入到这个里面编辑网站的主页
[root@WNginx01_7 nginx]# cat /usr/local/nginx/html/sentinel/index.html
<html>
<center><h1>Welcome to www.sentinel.com server test</h1></center>
</html>
然后在本机的hosts文件里面写上www.sentinel.com和他的ip地址的对应关系,因为我们没有DNS服务器,虽然他是基于域名的,但是实际上还是通过ip地址来进行通信的。
[root@sentinel ~]# echo "10.0.0.7 www.sentinel.com" >>/etc/hosts
访问成功
![](https://s1.51cto.com/images/blog/201809/29/ca86a0dd52fa527a25c7cc26ba7c2020.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-09-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 基于IP地址的虚拟主机
    • 基于端口的虚拟主机(生产环境中可以通过这种方法给网站单独开个后台,增加安全性)
      • 基于域名的虚拟主机
      相关产品与服务
      轻量应用服务器
      轻量应用服务器(TencentCloud Lighthouse)是新一代开箱即用、面向轻量应用场景的云服务器产品,助力中小企业和开发者便捷高效的在云端构建网站、Web应用、小程序/小游戏、游戏服、电商应用、云盘/图床和开发测试环境,相比普通云服务器更加简单易用且更贴近应用,以套餐形式整体售卖云资源并提供高带宽流量包,将热门开源软件打包实现一键构建应用,提供极简上云体验。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档