nginx服务一配置虚拟主机

本文介绍了Nginx的基础配置及如何配置三个虚拟主机的具体步骤。包括核心模块介绍、配置文件解析、虚拟主机配置示例等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、nginx基础配置

1.nginx模块

官方模块文档使用帮助:点击

1.1 nginx core modules(必须的)
包括
main
events
2.standard http modules (默认安装)
core
access
fastcgi
gzip 压缩模块
log  日志模块
proxy 
rewrite URL重写模块
upstream 负载均衡模块

2.配置文件解释

[root@nginx nginx]# egrep -v "^$|#" conf/nginx.conf|cat -n
     1  worker_processes  1;#nginx进程,一般设置为何CPU个数一样
     2  events {
     3      worker_connections  1024;#最大文件打开数
     4  }
     5  http {
     6      include       mime.types;#文件扩展名与类型映射表
     7      default_type  application/octet-stream;#默认文件类型
     8      sendfile        on;#允许sendfile方式传输文件,默认为off
     9      keepalive_timeout  65;#keepalive超时时间。
    10      server {
    11          listen       80;#监听端口
    12          server_name  localhost;#站点域名
    13          location / {
    14              root   html; 
    15              index  index.html index.htm;#站点首页文件
    16          }
    17          error_page   500 502 503 504  /50x.html;#出现错误跳转
    18          location = /50x.html {
    19              root   html;
    20          }
    21      }
    22  }

二、配置三个虚拟主机

1.编写配置文件

[root@nginx nginx]# egrep -v "^$|#" conf/nginx.conf.default > conf/nginx.conf
[root@nginx nginx]# cat -n  conf/nginx.conf
     1  worker_processes  1;
     2  events {
     3      worker_connections  1024;
     4  }
     5  http {
     6      include       mime.types;
     7      default_type  application/octet-stream;
     8      sendfile        on;
     9      keepalive_timeout  65;
    10      server {
    11          listen       80;
    12          server_name  www.liang.com;
    13          location / {
    14              root   html/www;
    15              index  index.html index.htm;
    16          }
    17      }
    18      server {
    19          listen       80;
    20          server_name  bbs.liang.com;
    21          location / {
    22              root   html/bbs;
    23              index  index.html index.htm;
    24          }
    25      }
    26      server {
    27          listen       80;
    28          server_name  blog.liang.com;
    29          location / {
    30              root   html/blog;
    31              index  index.html index.htm;
    32          }
    33      }
    34  }
[root@nginx nginx]# mkdir html/{www,bbs,blog}
[root@nginx nginx]# for name in www bbs blog;do echo "${name}.liang.com" > html/${name}/index.html;done

2.检查语法

[root@nginx nginx]# nginx -t
nginx: the configuration file /usr/local/nginx-1.6.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.6.2/conf/nginx.conf test is successful

3.重启

[root@nginx nginx]# nginx -s reload

4.打开另一台虚拟机测试

[root@lamp ~]# tail -1 /etc/hosts
10.0.0.129 www.liang.com bbs.liang.com blog.liang.com
[root@lamp ~]# curl www.liang.com
www.liang.com
[root@lamp ~]# curl bbs.liang.com
bbs.liang.com
[root@lamp ~]# curl blog.liang.com
blog.liang.com
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值