nginx服务一配置虚拟主机

一、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

猜你喜欢

转载自blog.csdn.net/liang_operations/article/details/81431252