Nginx配置http和tcp,Nginx配置httptcp


  最近在弄ejabberd+riak。其实这俩东西配置挺坑的,然后具体这俩货怎么配置,我以后会抽空写出配置的详细过程。对于负载均衡,我知道的现在有Nginx/LVS/HAProxy这三个大仙儿,各自有各自的优缺点,有关优缺点大家可以度娘一下。先来看看什么是负载均衡。

  负载均衡:是由多台服务器以对称的方式组成一个服务器集合,每台服务器都具有等价的地位,都可以单独对外提供服务而无须其他服务器的辅助。通过某种负载分担技术,将外部发送来的请求均匀分配到对称结构中的某一台服务器上,而接收到请求的服务器独立地回应客户的请求。均衡负载能够平均分配客户请求到服务器列阵,籍此提供快速获取重要数据,解决大量并发访问服务问题。这种群集技术可以用最少的投资获得接近于大型主机的性能。--摘自度娘

  今天先来看看Nginx。

  一、本机环境:

    ubbuntu 14

    pcre-8.38  传送门-->pcre

    nginx-1.9.12  传送门-->nginx

  二、安装

    1.分别解压两个压缩包

1 #解压pcre并重命名文件夹 2 tar zxvf ./pcre-8.38.tar.gz 3 mv pcre-8.38 pcre 4 #解压nginx并重命名文件夹 5 tar zxvf ./nginx-1.9.12.tar.gz 6 mv nginx-1.9.12 nginx View Code

    2.安装依赖

1 sudo apt-get install -y gcc g++ make View Code

    3.安装pcre

1 cd ./pcre 2 ./configure prefix=/usr/local/pcre 3 make && make install View Code

    4.安装nginx

1 #进入nginx文件夹 2 cd ../nginx 3 #加入参数编译 4 ./configure --prefix=/usr/local/nginx --pid-path=/var/run/nginx.pid --with-http_stub_status_module --with-http_ssl_module --with-pcre=stream --with-pcre=/programs/pcre View Code

    在这步,需要注意注意几点:

    第一点,--with-pcre=stream这个参数,自从nginx1.9.0版本开始,开始支持tcp的负载均衡,但是默认没有这个功能,加入这个参数才能对tcp金星负载均衡。

    第二点,--with-pcre这个参数,这个其实是要你定位你pcre的源代码的,不管你是否刚才装了pcre。

    第三点,就是有关openssl。当你遇到如下错误,要安装Openssl

1 #错误1,需要安装openssl 2 ./configure: error: SSL modules require the OpenSSL library. 3 You can either do not enable the modules, or install the OpenSSL library 4 into the system, or build the OpenSSL library statically from the source 5 with nginx by using --with-openssl=<path> option. 6 #安装openssl 7 sudo apt-get install -y openssl View Code

    第四点,有关zlib。当遇到如下错误,要安装libssl-dev

1 #错误,需要安装libssl-dev 2 ./configure: error: the HTTP gzip module requires the zlib library. 3 You can either disable the module by using --without-http_gzip_module 4 option, or install the zlib library into the system, or build the zlib library 5 statically from the source with nginx by using --with-zlib=<path> option. 6 #安装libssl-dev 7 sudo apt-get install -y libssl-dev 8 #./configure通过以后,开始编译 9 sudo make && make install View Code

  现在你可以到转到/usr/local/nginx/sbin/目录去启动Nginx。不出意外,你打开浏览器输入网址,应该可以看到Nginx的欢迎界面

   

  那现在先恭喜下,你的Nginx安装成功了。下面开始做配置。

  三、将Nginx添加到PATH

1 #编辑/etc/bash.bashrc 2 sudo vim /etc/bash.bashrc View Code

  在最下面添加如下代码

1 #添加如下代码 2 if [ -d "/usr/local/nginx/sbin" ]; then 3 PATH="$PATH:/usr/local/nginx/sbin" 4 fi 5 #保存退出 6 #刷新配置 7 source /etc/bash.bashrc View Code

  现在你在任何地方都可以用nginx来启动了

  四、配置

    1.Http

    你的conf文件在/usr/local/nginx/conf中,叫做nginx.conf,你需要对其进行配置,下面我的一个简单的例子,来展示如何配置

1 http { 2 upstream backend { 3 server 10.0.1.11:1234; 4 server 10.0.1.12:1234; 5 } 6 server { 7 listen 80; 8 server_name example.com; 9 location / { 10 limit_except GET { 11 deny all; 12 } 13 proxy_pass http://backend; 14 } 15 } 16 } View Code

    其中upstream部分设置你要负载的地址,可以添加weight来设置比重

    proxy_pass后面要跟http://upstreamname这样才能配置成功,然后你可以重启你的nginx来验证你的配置

    2.tcp

    还是更改conf文件,如下例子

1 stream { 2 upstream backend { 3 server 10.17.0.1:1234; 4 server 10.17.0.2:1234; 5 } 6 server { 7 listen 8080; 8 proxy_connect_timeout 1s; 9 proxy_timeout 3s; 10 proxy_pass backend; 11 } 12 } View Code

    这里要注意proxy_pass填的是upstreamname,没有http://

    这样你就能访问tcp接口了。

  其实有关Nginx还有很多细项配置,我以后可能会在讲解实际软件配置时再讲解。这篇先写到这了,希望对大家有帮助。

  转载请注明
      作者:李小二的春天
      地址:http://www.cnblogs.com/LittleTwoLee/p/5258279.html

相关内容