虚拟主机的概念
虚拟主机,就是把一台物理服务器划分成多个“虚拟”的服务器,每一个虚拟主机都可以有独立的域名和独立的目录
nginx虚拟主机的配置

nginx的虚拟主机就是通过nginx.conf中server节点指定的,想要设置多个虚拟主机,配置多个server节点即可.

nginx安装环境

是C语言开发,建议在linux上运行,本次使用Centos6.5作为安装环境。

n gcc

安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:yum install gcc-c++

n PCRE

是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

yum install -y pcre pcre-devel

注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

n zlib

库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。

yum install -y zlib zlib-devel

n openssl

是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。

不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

yum install -y openssl openssl-devel

 

 编译安装

参数设置如下:

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

 

注意:上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

安装

make

make  install

安装成功查看安装目录 :

linux nginx 配置环境变量profile linux系统nginx配置_nginx

启动nginx

cd /usr/local/nginx/sbin/

./nginx

linux nginx 配置环境变量profile linux系统nginx配置_虚拟主机_02

看到这两个进程代表启动成功

停止nginx:

./nginx -s quit

重启nginx:

./nginx -s reload

完成nginx配置

在usr/local/nginx/nginx.conf下添加server

server {
        listen       80;
        server_name  www.test1.com;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html82;
            index  index.html index.htm;
        }
    }server {
        listen       80;
        server_name  www.test2.com;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html83;
            index  index.html index.htm;
        }
    }

在hosts中配置:

192.168.136.128 www.test1.com

192.168.136.128 www.test2.com

在防火墙配置文件中添加端口:

linux nginx 配置环境变量profile linux系统nginx配置_虚拟主机_03

重启防火墙和nginx:

/etc/init.d/iptables restart

./nginx -s reload

测试

linux nginx 配置环境变量profile linux系统nginx配置_html_04

linux nginx 配置环境变量profile linux系统nginx配置_nginx_05