centOS7安裝nginx做負載均衡

我的機器信息:

系統版本:

[root@localhost ~]#  lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:langu
ages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.2.1511 (Core) 
Release:        7.2.1511
Codename:       Core

內核版本:

[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# 
[root@localhost ~]# cat /proc/version
Linux version 3.10.0-327.el7.x86_64 ([email protected]) (gcc version 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC) ) #1 SMP Thu Nov 
19 22:10:57 UTC 2015

 

 

CentOS 7下安裝部署:

如果是新環境的話,先安裝一些必要的命令工具

一. gcc 安裝
安裝 nginx 需要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝:

yum install gcc-c++

二. PCRE pcre-devel 安裝
PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,所以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也需要此庫。命令:

yum install -y pcre pcre-devel

三. zlib 安裝
zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫。

yum install -y zlib zlib-devel

四. OpenSSL 安裝
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。
nginx 不僅支持 http 協議,還支持 https(即在ssl協議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。

yum install -y openssl openssl-devel

開始安裝nginx

配置epel yum 源

確保系統已經安裝了wget,如果沒有安裝,執行 yum install wget 安裝。


wget http://dl.Fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 
rpm -ivh epel-release-latest-7.noarch.rpm
yum install nginx -y


查看確認 是否安裝
[root@localhost ~]# rpm -qa | grep nginx
nginx-1.10.2-1.el7.x86_64
nginx-mod-stream-1.10.2-1.el7.x86_64
nginx-mod-http-geoip-1.10.2-1.el7.x86_64
nginx-all-modules-1.10.2-1.el7.noarch
nginx-mod-http-perl-1.10.2-1.el7.x86_64
nginx-mod-http-image-filter-1.10.2-1.el7.x86_64
nginx-mod-mail-1.10.2-1.el7.x86_64
nginx-filesystem-1.10.2-1.el7.noarch
nginx-mod-http-xslt-filter-1.10.2-1.el7.x86_64


查看 安裝nginx 所生成的文件 
[root@localhost ~]# rpm -ql nginx 
/etc/logrotate.d/nginx

/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi.conf.default
/etc/nginx/fastcgi_params
/etc/nginx/fastcgi_params.default
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/mime.types.default
/etc/nginx/nginx.conf
/etc/nginx/nginx.conf.default
/etc/nginx/scgi_params
/etc/nginx/scgi_params.default
/etc/nginx/uwsgi_params
/etc/nginx/uwsgi_params.default
/etc/nginx/win-utf
/usr/bin/nginx-upgrade
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx/modules
/usr/sbin/nginx
/usr/share/doc/nginx-1.10.2
/usr/share/doc/nginx-1.10.2/CHANGES
/usr/share/doc/nginx-1.10.2/README
/usr/share/doc/nginx-1.10.2/README.dynamic
/usr/share/doc/nginx-1.10.2/UPGRADE-NOTES-1.6-to-1.10
/usr/share/licenses/nginx-1.10.2
/usr/share/licenses/nginx-1.10.2/LICENSE
/usr/share/man/man3/nginx.3pm.gz
/usr/share/man/man8/nginx-upgrade.8.gz
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx/html/404.html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/usr/share/nginx/html/nginx-logo.png
/usr/share/nginx/html/poweredby.png
/usr/share/vim/vimfiles/ftdetect/nginx.vim
/usr/share/vim/vimfiles/indent/nginx.vim
/usr/share/vim/vimfiles/syntax/nginx.vim
/var/lib/nginx
/var/lib/nginx/tmp
/var/log/nginx


三、測試nginx 

#啓動nginx服務
systemctl start nginx.service
#停止nginx服務
systemctl stop nginx.service
#重啓nginx服務
systemctl restart nginx.service

#查看運行狀態

systemctl status nginx.service

 


設置開機啓動 
systemctl enable nginx.service

 


查看nginx 啓動狀態
systemctl status nginx


查看是否監聽


 ss -tnl | grep 80 
LISTEN    0      128          *:80                      *:*                  
LISTEN    0      128        :::80                      :::* 


測試 nginx


在瀏覽器中輸入 nginx 服務器的ip 地址 
備註:如果不能正常訪問,關閉防火牆
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機啓動
firewall-cmd --state #查看默認防火牆狀態(關閉後顯示notrunning,開啓後顯示running)

或者打開Linux 80端口

firewall-cmd --zone=public --add-port=80/tcp --permanent

如果還不能訪問,關閉SElinux

setsebool -P httpd_can_network_connect 1

測試頁面

錯誤日誌路徑(可以自定義)
vi /var/log/nginx/error.log

安裝成功

四、nginx 的配置文件說明

配置文件路徑  vi /etc/nginx/nginx.conf

我測試時使用的下面的配置

user  root;
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 tomcat_onlineAPI  {
#我這裏使用的是IPhash負載策略,可以配置你想用的
	ip_hash;
#Tomcat服務器的IP和端口號
       server 192.168.199.149:9999;
	server 192.168.199.57:8080;
	}

    server {
#監聽端口80
        listen       80;
#可以使用serverName訪問
        server_name  www.tyr.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root html;
            index  index.html index.htm;
#pass參數http://後跟的是上面upstream後的名稱
			proxy_pass  http://tomcat_onlineAPI;
        }

        #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,進行訪問測試。

至此搭建完成。

nginx負載策略使用iphash的一些問題

session共享的話,ip_hash是容易理解的,但是因爲僅僅能用ip這個因子來分配後端,因此ip_hash是有缺陷的,不能在一些情況下使用:

1.nginx不是最前端的服務器。

ip_hash要求nginx一定是最前端的服務器,否則nginx得不到正確ip,就不能根據ip作hash。譬如使用的是squid爲最前端,那麼nginx取ip時只能得到squid的服務器ip地址,用這個地址來作分流是肯定錯亂的。
2.nginx的後端還有其它方式的負載均衡。

假如nginx後端又有其它負載均衡,將請求又通過另外的方式分流了,那麼某個客戶端的請求肯定不能定位到同一臺session應用服務器上。這麼算起來,nginx後端只能直接指向應用服務器。

5、upstream_hash
爲了解決ip_hash的一些問題,可以使用upstream_hash這個第三方模塊

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章