nginx在一个服务器上配置两个项目,并经过两个不一样的域名访问

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,由俄罗斯的程序设计师Igor Sysoev所开发,其特色是占有内存少,并发能力强。php

 

话很少说,先从最基本的nginx搭建开始html

 

Linux系统:Centos 6.5 x64
Nginx版本:1.7.8nginx

 

一、安装prce(重定向支持)和openssl(https支持,若是不须要https能够不安装。)web

yum -y install pcre*浏览器

 

yum -y install openssl*服务器

 

CentOS 6.5 我安装的时候是选择的“基本服务器”,默认这两个包都没安装全,因此这两个都运行安装便可。并发

二、下载nginx 1.7.8app

 

先进入目录/usr/localwebapp

cd /usr/localtcp

 

而后下载压缩包

 

wget http://nginx.org/download/nginx-1.7.8.tar.gz

 

而后进入目录编译安装

cd nginx-1.7.8

 

./configure --prefix=/usr/local/nginx-1.7.8 \

--with-http_ssl_module --with-http_spdy_module \

--with-http_stub_status_module --with-pcre

 

若是没有error信息,就能够执行下边的安装了:

make

make install

 

四、开启nginx进程

/usr/local/nginx-1.7.8/sbin/nginx

 

重启或关闭进程:

/usr/local/nginx-1.7.8/sbin/nginx -s reload

/usr/local/nginx-1.7.8/sbin/nginx -s stop

 

五、关闭防火墙,或者添加防火墙规则就能够测试了。

service iptables stop

 

或者编辑配置文件:

vi /etc/sysconfig/iptables

 

添加这样一条开放80端口的规则后保存:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

 

重启服务便可:

service iptables restart

 

ok,,能够浏览器访问了。

 

 

在浏览器中输入IP地址之后,若是你看到上图所示字样,那么恭喜你nginx安装成功

 

接下来进行第二步

打开/usr/local/nginx-1.7.8/conf/nginx.conf文件,对service配置以下:

server {

        listen       80;

        server_name test.horace.space;

        index index.html index.htm index.php default.html default.htm default.php;

        root  /home/am/webapps/am;

 

        location / {

            proxy_pass http://test.horace.space:8080/am/;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

 

server {

        listen       80;

        server_name m.horace.space;

        index index.html index.htm index.php default.html default.htm default.php;

        root  /home/tq/webapps/tq;

 

        location / {

            proxy_pass http://m.horace.space:8089/tq/;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

 

 

在这里作一下解释:

一、listen       80;  监听80端口

二、server_name m.horace.space;  项目所对应的域名

三、index index.html index.htm index.php default.html default.htm default.php;  项目首页名称

四、 root  /home/tq/webapps/tq;  项目存放路径

五、proxy_pass http://m.horace.space:8089/tq/;  域名对应URL,这个URL对应的就是http://m.horace.space,可经过域名直接访问

六、若是你想配置多个项目只须要将service{}复制多份便可。

相关文章
相关标签/搜索