您的位置:首页 > 运维架构 > Nginx

nginx配置虚拟主机

2018-06-19 21:05 471 查看

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_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.ldwt.org;
root html/www;
index index.html index.htm;
}

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

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

}

修改完成后检查语法:../sbin/nginx -t
如果nginx已启动就重启:../sbin/nginx -s reload
如果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时报错:
[root@localhost html]# ../sbin/nginx -s reload
nginx: [error] invalid PID number "" in "/application/nginx1.6.2/logs/nginx.pid"

解决:/application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
cd ../logs
ll

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx 虚拟站点