漂亮蓝影 发表于 2018-11-16 07:21:26

nginx配置虚拟主机

Nginx配置文件特点:

1. Nginx里的配置文件都是以分号;结尾的。

2. 一个server标签就是一个虚拟主机

  NGINX配置虚拟主机流程:
  1.复制一个完整的server标签段,放在http的结束大括号前。
  ---------------------------相当于将server标签段放入http标签里
  2.更改server_name及对应网页的root根目录
  3.检查配置文件语法,平滑重启服务
  4.创建server_name对应网页的根目录,并建立测试文件。
  ----------------------------如果没有index首页会出现403错误
  5.在客户端对server_name的主机名做hosts解析DNS配置。
  -------------------------------------使用ping检查、浏览器访问
  6.在linux服务端做hosts解析,用wget或curl访问

  一、编辑nginx主配置文件
  进入nginx主配置文件目录下:cd /application/nginx/conf
  过滤掉以#开头和空行:egrep -vn "#|^$" nginx.conf >a.log
  将源文件备份:mv nginx.conf nginx.conf.ori
  修改过滤出来的文件:mv a.log nginx.conf
  编辑文件:vim nginx.conf
  worker_processes1;
  events {
  worker_connections1024;
  }
  http {
  include       mime.types;
  default_typeapplication/octet-stream;
  sendfile      on;
  keepalive_timeout65;
  server {
  listen       80;
  server_namewww.ldwt.org;
  root   html/www;
  indexindex.html index.htm;
  }
  

server {  listen       80;
  server_namebbs.ldwt.org;
  root   html/bbs;
  indexindex.html index.htm;
  }
  

  
server {
  listen       80;
  server_nameblog.ldwt.org;
  root   html/blog;
  indexindex.html index.htm;
  }
  

  }
  修改完成后检查语法:../sbin/nginx -t

  如果nginx已启动就重启:../sbin/nginx -s>  如果nginx未启动则启动:../sbin/nginx start
  二、创建站点目录:mkdir ../html/{www,bbs,blog}
  for n in www bbs blog;do echo "$n.ldwt.com" >../html/$n/index.html;done
  for n in www bbs blog;do cat ../html/$n/index.html;done
  三、配置客户端hosts文件



  ———————————————————————————————————
  ——————————————问题解决:————————————————
  重启或启动nginx时报错:

  # ../sbin/nginx -s>  nginx: invalid PID number "" in "/application/nginx1.6.2/logs/nginx.pid"
  解决:/application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
  cd ../logs
  ll


页: [1]
查看完整版本: nginx配置虚拟主机