1. 程式人生 > >centOS7安裝nginx做負載均衡

centOS7安裝nginx做負載均衡

我的機器資訊:

系統版本:

[[email protected] ~]#  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

核心版本:

[[email protected] ~]# 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
[[email protected] ~]# 
[[email protected] ~]# 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

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

一. 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 http://dl.Fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm  rpm -ivh epel-release-latest-7.noarch.rpm yum install nginx -y

檢視確認 是否安裝 [[email protected] ~]# 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 所生成的檔案  [[email protected] ~]# 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,進行訪問測試