歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> ubuntu安裝nginx及負載均衡配置

ubuntu安裝nginx及負載均衡配置

日期:2017/3/3 14:13:20   编辑:Linux技術
所有的安裝包可以去以下地址下載,或者自行去官網下載,下面都有介紹.
所有安裝包地址:http://download.csdn.net/detail/carboncomputer/9238037
原文地址:http://www.cnblogs.com/zhongshengzhen/p/nginx.html
nginx 學習地址:http://tengine.taobao.org/book/appendix_c.html#nginxlinux
具體步驟如下:
1、下載PCRE, 是一個用C語言編寫的正則表達式函數庫
[root@localhost pcre-8.36]# cd /tmp/download/
[root@localhost download]# wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz
[root@localhost download]# tar zxvf pcre-8.36.tar.gz
2、下載zlib庫
[root@localhost pcre-8.36]# cd /tmp/download/
[root@localhost download]# wget http://ncu.dl.sourceforge.net/project/libpng/zlib/1.2.8/zlib-1.2.8.tar.gz
[root@localhost download]# tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8
4、下載SSL
[root@localhost zlib-1.2.8]# cd ..
[root@localhost download]# wget http://www.openssl.org/source/openssl-1.0.1p.tar.gz
[root@localhost download]# cd openssl-1.0.1c
[root@localhost openssl-1.0.1c]# tar -zxvf openssl-1.0.1c.tar.gz
5、下載nginx
[root@localhost download]# wget http://nginx.org/download/nginx-1.2.8.tar.gz
[root@localhost download]# tar -zxvf nginx-1.2.8.tar.gz
6、安裝
[root@localhost download]mv pcre-8.36 /usr/local/
[root@localhost download]mv zlib-1.2.8 /usr/local/
[root@localhost download]mv openssl-1.0.1c /usr/local/
[root@localhost download]mv nginx-1.2.8 /usr/local/
[root@localhost download]cd /usr/local/
[root@localhost local]# cd pcre-8.36
[root@localhost pcre-8.36]# ./configure&&make&&make install
[root@localhost pcre-8.36] cd ../zlib-1.2.8
[root@localhost zlib-1.2.8]# ./configure && make && make install
[root@localhost zlib-1.2.8]# cd ../openssl-1.0.1c
[root@localhost openssl-1.0.1c]# ./config && make && make install
[root@localhost openssl-1.0.1c]# cd ../nginx-1.2.8
[root@localhost nginx-1.2.8]# ./configure --prefix=/usr/local/nginx && make && make install
7、啟動nginx
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
8、測試
在確保可以連接到服務器的電腦上,浏覽器輸入裝了nginx的機器的ip地址,會看到Welcome to nginx!的提示說明安裝配配置成功了。

負載均衡配置:
現有兩部服務器:
192.168.1.100 (按照以上操作安裝有nginx,作為轉發機,虛擬機,同時運行有測試用的web工程)
192.168.1.101 (無安裝nginx,運行有測試用的web工程,虛擬機)
測試用的web工程為:speechWeb
[root@a conf]# vi /usr/local/nginx/conf/nginx.conf
配置如下展示:
#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;
    #gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
"conf/nginx.conf" [readonly] 137L, 3107C                                                                          1,0-1         Top
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

   # my server
   upstream solrserver {
        # solr1
        server 192.168.1.100:9080 weight=2;
        # solr2
        server 192.168.1.101:8080 weight=2;
        # solr3
    }

    server {
        listen 8030;

        location / {
            proxy_passhttp://solrserver;             #auth_basic "Restricted";
            #auth_basic_user_file pwd;
        }
    }

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

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

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

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

}

其中關鍵代碼是:
upstream solrserver {
        # solr1
        server 192.168.1.100:9080 weight=2;
        # solr2
        server 192.168.1.101:8080 weight=2;
        # solr3
    }

    server {
        listen 8030;

        location / {
            proxy_passhttp://solrserver;             #auth_basic "Restricted";
            #auth_basic_user_file pwd;
        }
    }
啟動每台機器的speechWeb應用,然後通過以下格式在浏覽器中訪問轉發機:
http://192.168.1.100:8030/speechWeb/
轉發機會根據配置的權重參數(weight)進行負載均衡式訪問服務器(http://192.168.1.100:9080/speechWeb/ 或http://192.168.1.100:8030/speechWeb/)
每次更改配置文件後都需要重新啟動nginx才會生效。
1、當第一次啟動gnix時,用以下方式:
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
2、當nginx已經在運行時,則需要平滑重啟,步驟如下:
先查看nginx master的pid: ps aux|grep nginx|grep master
假設nginx的pid為3979,則平滑重啟命令為:kill -HUP 3979
常見問題及解決辦法:
1、安裝PCRE時提示configure: error: You need a C++ compiler for C++ support.
原因是沒有安裝c++編譯器,采用下面的命令安裝:
[root@localhost pcre-8.37]# apt-get install build-essential
[root@localhost pcre-8.37]# apt-get install gcc
2、啟動nginx失敗
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
原因是64bit的系統,但是默認取了/usr/local/lib裡面的包
檢查方法:
[root@localhost nginx]# cd /usr/local/
[root@localhost local]# ls /lib64/ |grep pcre
libpcre.so.0
libpcre.so.0.0.1
[root@localhost local]# ls /lib/ |grep pcre
說明缺失的包在lib64
設置軟連接來解決:
[root@localhost local]# ln -s /lib64/libpcre.so.0.0.1 /lib64/libpcre.so.1
[root@localhost local]# cd nginx
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
3、啟動報錯:
報錯:nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
解決辦法:sudo fuser -k 80/tcp
端口被占用,關閉占用端口
Copyright © Linux教程網 All Rights Reserved