Linux CentOS --- 安装Nginx

19 篇文章 0 订阅
1 篇文章 0 订阅

一、什么是Nginx

 

Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。

其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。

Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

 

下载地址:http://nginx.org/

 

 

二、正向代理和反向代理

 

参考资料:

http://blog.csdn.net/ka_ka314/article/details/78927051

 

 

三、为什么使用Nginx

 

互联网飞速发展的今天,大用户量高并发已经成为互联网的主体.怎样能让一个网站能够承载几万个或几十万个用户的持续访问呢?这是一些中小网站急需解决的问题。用单机tomcat搭建的网站,在比较理想的状态下能够承受的并发访问量在150到200左右。按照并发访问量占总用户数量的5%到10%这样计算,单点tomcat网站的用户人数在1500到4000左右。对于一个为全国范围提供服务的网站显然是不够用的,为了解决这个问题引入了负载均衡方法。负载均衡就是一个web服务器解决不了的问题可以通过多个web服务器来平均分担压力来解决,并发过来的请求被平均分配到多个后台web服务器来处理,这样压力就被分解开来。


负载均衡服务器分为两种一种是通过硬件实现的负载均衡服务器,简称硬负载例如:f5。另一种是通过软件来实现的负载均衡,简称软负载:例如apache和nginx。硬负载和软负载相比前者作用的网络层次比较多可以作用到socket接口的数据链路层对发出的请求进行分组转发但是价格成本比较贵,而软负载作用的层次在http协议层之上可以对http请求进行分组转发并且因为是开源的所以几乎是0成本,并且阿里巴巴,京东等电商网站使用的都是Nginx服务器。

 

 

四、Nginx环境依赖

 

 

nginx是C语言开发,建议在linux上运行,本教程使用Centos7作为安装环境。

·gcc

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

yum install gcc-c++

 

·PCRE

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

yuminstall -y pcre pcre-devel

 

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

 

·zlib

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

yuminstall -y zlib zlib-devel

 

·openssl

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

 

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

yuminstall -y openssl openssl-devel

 

 

五、安装Nginx

(1)上传安装包到linux

 

(2)解压安装包 --- 解压后自动在/usr/local/下创建nginx-1.8.0文件夹

tar -zvf nginx-1.8.0.tar.gz -C /usr/local/

 

(3)进入nginx-1.8.0文件夹,使用configure命令创建makefile

./configure

 

(4)./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

 

 

 

注意:执行完成后默认在/usr/local/目录下创建nginx文件夹

 

 

(5)执行make

 

(6)执行make install

 

 

六、安装Tomcat

安装多个tomcat,准备配置搭建tomcat集群环境

 

tomcat安装参考资料:

http://blog.csdn.net/ka_ka314/article/details/78926222

 

 

七、Nginx的负载均衡配置

 

修改配置文件: 配置文件路径: /usr/local/nginx/conf下的nginx.conf

(1)增加upstream配置,配置tomcat访问地址,如下:

(2)server_name,配置域名,如下

(3)增加proxy_pass配置,如下:

 

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
	
    upstream server_lb{
	server 192.168.153.128:8081;
	server 192.168.153.128:8082;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
	    proxy_pass http://server_lb;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

 

 

八、Nginx的启动与关闭

(1)启动Nginx

 

在nginx目录下有一个sbin目录,sbin目录下有一个nginx可执行程序。

./nginx  --- 启动Nginx

 

(2)关闭Nginx

 

 

关闭命令:相当于找到nginx进程kill。

./nginx -s stop

 

退出命令:

./nginx -s quit

等程序执行完毕后关闭,建议使用此命令。

 

(3)动态加载Nginx配置文件

 

 

./nginx -s reload

可以不关闭nginx的情况下更新配置文件。

 

 

 

 

其他参考资料: --- nginx.conf配置详解

http://www.runoob.com/w3cnote/nginx-install-and-config.html

https://www.cnblogs.com/hunttown/p/5759959.html

http://blog.csdn.net/tjcyjd/article/details/50695922

http://blog.csdn.net/hzsunshine/article/details/63687054

 

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值