Nginx的安裝與負載均衡配置

一、安裝

1、下載(此處不再贅述,忽略)

2、解壓到指定目錄

[root@localhost home]# tar zxvf nginx-1.11.2.tar.gz -C /myapp/

[root@localhost home]# cd /myapp/nginx-1.11.2/

[root@localhost nginx-1.11.2]# ls

auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src


3、使用yum安裝依賴庫 【gcc 、pcre、openssl、zilib】

[root@localhost nginx-1.11.2]# yum -y install gcc

[root@localhost nginx-1.11.2]# yum -y install pcre

[root@localhost nginx-1.11.2]# yum -y install openssl

[root@localhost nginx-1.11.2]# yum -y install zlib


4、編譯並安裝

[root@localhost nginx-1.11.2]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module  

[root@localhost nginx-1.11.2]# make

[root@localhost nginx-1.11.2]# make install


5、說明:

參數 --with-http_stub_status_modul是爲了啓用nginx的 NginxStatus 功能,用來監控 Nginx 的當前狀態

--prefix=/usr/local/nginx 指定安裝目錄更詳細的參數參考./configure --help

安裝成功後 /usr/local/nginx 目錄下有四個子目錄分別是:conf、html、logs、sbin 。


二、配置Nginx

1、配置

#user  nobody;

user root root;               #工作進程的屬主

worker_processes  1;    #工作進程數,一般與 CPU核數等同

 

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

#pid        logs/nginx.pid;

events {

############################################################################

# 使用的網絡I/O模型,Linux系統推薦採用epoll模型,FreeeBSD系統推薦採用kqueue模型use epoll;

############################################################################

    worker_connections  1024;  #每個工作進程允許最大的同時連接數

}

 

 

http {

    include       mime.types;

    default_type  application/octet-stream;

 

############################################################################

    server_names_hash_bucket_size 128;

    client_header_buffer_size 32k;

    large_client_header_buffers 4  32k;

 

    # 設置客戶端能夠上傳的文件大小,注意要與應用程序中的文件大小限制兼容。

    client_max_body_size 10m;

    # sendfile on;

    tcp_nopush on;

    # keepalive_timeout 60;

    tcp_nodelay on;

    client_body_buffer_size 512k;

    proxy_connect_timeout 5;

    proxy_read_timeout 60;

    proxy_send_timeout 5;

    proxy_buffer_size 16k;

    proxy_buffers 4 64k;

    proxy_busy_buffers_size 128k;

    proxy_temp_file_write_size 128k;


    # 以下放開註釋    自定義日誌記錄格式設置,main爲名字,在access_log命令中引用

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

 

#指定日誌存放路徑,如果想使用默認的combined格式記錄日誌,可以使用access_log logs/access.log combined; 以下是使用log_format自定義的格式記錄日誌的。

    access_log  logs/access.log  main;

    sendfile        on;

    #tcp_nopush     on;

    #keepalive_timeout  0;

    keepalive_timeout  65;

############################################################################

# 開啓gzip壓縮設置(只能在http模塊中設置)

    gzip  on;     #放開註釋

    gzip_min_length 1k;

    gzip_buffers  4 16k;

    gzip_http_version 1.1;

    gzip_comp_level 2;

    gzip_types application/x-javascript text/css application/xml;

    gzip_vary on;

 

#upstream設置,設置代理服務器(負載均衡池),默認的負載均衡方式是輪詢,另外一種是ip_hash

    upstream tomcat_server{

          ip_hash; #每個請求按訪問iphash結果分配,這樣每個訪客固定訪問一個後端服務器,可以解決session的問題。

          server 10.10.10.145:8081 weight=10;

          server 10.10.10.146:8082 weight=10;

    }

 

    #處理上傳和下載的圖片文件服務器,設置代理服務器(負載均衡池),默認的負載均衡方式是輪訓,另外一種是ip_hash

    upstream image_server{

        server 10.10.10.148:8083 weight=10;

    }

############################################################################

 

    server {

        listen       8090       #nginx偵聽端口 根據自己需要修改 我的8080端口已經被佔用,所以修改爲8090

        server_name localhost;

 

############################################################################

        charset utf-8;

        index  index.html index.htm index.jsp index.do;

############################################################################

 

        #charset koi8-r;

 

############################################################################

        #rewrite settings

        if (-d $request_filename)

        {

            rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;

        }

############################################################################

        #access_log  logs/host.access.log  main;

 

        location / {

            #root   html;

            index  index.html index.htm index.jsp index.do;

        }

 

############################################################################

        # iamge 服務器location

        location ~*/admin/images/{

            # alias /web/www/html/img/;

            proxy_pass http://image_server;

            proxy_set_header Host $host:8083;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        }

 

        location ~*/admin/

        {

            # 如果後端服務器返回502504,執行超時等錯誤,自動將請求轉發到upstream負載均衡池中的另一臺服務器,實現failover

            proxy_next_upstream http_502 http_504 error timeout invalid_header;

 

            proxy_pass http://tomcat_server;

            proxy_redirect off;

            # 變量$host等於客戶端請求頭中的Host

            #proxy_set_header Host $host:8081;

            proxy_set_header Host $host:$server_port;

 

            proxy_set_header X-Real-IP $remote_addr;

            proxy_set_header REMOTE-HOST $remote_addr;

 

            #後端的web服務器可以通過X-Forwarded-For獲取真實的IP地址,$remote_addr客戶端的ip地址

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            client_max_body_size 10m;

            client_body_buffer_size 128k;

            proxy_connect_timeout 90;

            proxy_send_timeout 90;

            proxy_read_timeout 90;

            proxy_buffer_size 4k;

            proxy_buffers 4 32k;

            proxy_busy_buffers_size 64k;

            proxy_temp_file_write_size 64k;

        }

 

        #image expires settings

        # expires 屬於http Header模塊,主要用來Nginx返回給用戶網頁添加附件的header信息,可以在http,server,location中使用

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

        {

            expires 30d;

        }

 

        #css&js expires settings

        # expires 屬於http Header模塊,主要用來Nginx返回給用戶網頁添加附件的header信息,可以在http,server,location中使用

        location ~ .*\.(js|css|html)?$

        {

            expires 2h;

        }

 

        # 放開註釋

        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;

#    }

#}

}

2、測試

測試配置文件是否正確

[root@localhost conf]# /usr/local/nginx/sbin/nginx -t

如果顯示以下信息,說明配置正確可以啓動Nginx服務

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful


3、啓動、重啓、停止、重新加載配置文件命令

啓   動nginx:   /usr/local/nginx/sbin/nginx

重   啓Nginx:  /usr/local/nginx/sbin/nginx -s reload

停   止Nginx:  /usr/local/webserver/nginx/sbin/nginx -s stop

重新加載配置文件:/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf


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