Nginx的安装

来源:互联网 发布:dt数据时代 编辑:程序博客网 时间:2024/06/07 07:04

1.   Nginx的安装

1.1.  下载nginx

官网:http://nginx.org/

1.2.  上传并解压nginx

tar -zxvf nginx-1.8.1.tar.gz -C/usr/local/src

1.3.  编译nginx

#进入到nginx源码目录

cd /usr/local/src/nginx-1.8.1

 

#检查安装环境,并指定将来要安装的路径

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

 

#缺包报错 ./configure: error: C compiler cc is not found

 

#使用YUM安装缺少的包

yum-y install gcc pcre-devel openssl openssl-devel

 

#编译安装

make&& make install

 

安装完后测试是否正常:

/usr/loca/nginx/sbin/nginx

查看端口是否有ngnix进程监听

netstat-ntlp | grep 80

 

2.   配置nginx

2.1.  配置反向代理

1.修改nginx配置文件

server {

    listen       80;

    server_name  nginx-01.itcast.cn;    #nginx所在服务器的主机名

#反向代理的配置

location / {             #拦截所有请求

    root html;

proxy_pass http://234241:8080;  #这里是代理走向的目标服务器:tomcat

    }

}

2.启动tomcat-01上的tomcat

 

3.启动nginx-01上的nginx

./nginx

 

重启:

kill -HUP `cat /usr/local/nginx/logs/nginx.pid`

参考网址:http://www.cnblogs.com/jianxie/p/3990377.html

1.1.  动静分离

#动态资源 index.jsp

location ~ .*\.(jsp|do|action)$ {

proxy_pass http://tomcat-01.itcast.cn:8080;

}

 

#静态资源

location ~ .*\.(html|js|css|gif|jpg|jpeg|png)$ {

    expires 3d;

}

 

1.2.  负载均衡

在http这个节下面配置一个叫upstream的,后面的名字可以随意取,但是要和location下的proxy_pass http://后的保持一致。

http {

    是在http里面的, 已有http, 不是在server里,在server外面

upstream tomcats {

server shizhan02:8080 weight=1;#weight表示多少个

server shizhan03:8080 weight=1;

        server shizhan04:8080 weight=1;

}

#卸载server里

location ~ .*\.(jsp|do|action) {

    proxy_pass http://tomcats;        #tomcats是后面的tomcat服务器组的逻辑组号

}

}