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

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

话不多说,先从最基本的nginx搭建开始

Linux系统:Centos 6.5 x64

Nginx版本:1.7.8

1、安装prce(重定向支持)和openssl(https支持,如果不需要https可以不安装。)

yum -y install pcre*

yum -y install openssl*

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

2、下载nginx 1.7.8

先进入目录/usr/local

cd /usr/local

然后下载压缩包

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

4、开启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

5、关闭防火墙,或者添加防火墙规则就可以测试了。

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;

        }

    }

在这里做一下解释:

1、listen 80; 监听80端口

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

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

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

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

6、如果你想配置多个项目只需要将service{}复制多份即可。