站長資訊網
        最全最豐富的資訊網站

        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; #每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決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/

        {

        # 如果后端服務器返回502,504,執行超時等錯誤,自動將請求轉發到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

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 人妻少妇乱子伦精品| 国产一精品一av一免费爽爽| 51视频国产精品一区二区| 蜜臀AV无码国产精品色午夜麻豆| 精品国产亚洲一区二区三区| 欧美日韩人妻精品一区二区在线| 久久亚洲国产精品123区| 成人国产精品日本在线观看| 91视频精品全国免费观看| 亚洲码国产精品高潮在线| 久久亚洲国产精品123区| 亚洲国产精品一区二区久久| 国产精品视频永久免费播放| 无码国产乱人伦偷精品视频| 午夜在线视频91精品 | 精品三级AV无码一区| 中日韩产精品1卡二卡三卡| 欧美精品黑人粗大视频| 久久精品女人天堂AV麻| 国产精品亚洲玖玖玖在线观看| 777欧美午夜精品影院| 四虎国产精品永久一区| 久久精品国产免费一区| 精品久久久久久综合日本| 99久久婷婷免费国产综合精品| 国产精品美女久久久m| 国产精品人成在线播放新网站| 国精品无码一区二区三区在线| 精品亚洲麻豆1区2区3区| 久久综合精品国产二区无码| 精品深夜AV无码一区二区| 国产精品久久久久国产A级| 国产精品无码久久综合| 91精品国产91久久久久福利| 国产精品爱啪在线线免费观看| 成人精品在线视频| 国产精品视频一区国模私拍| 久久91精品国产91久久麻豆 | 2021国产成人精品国产| 国产69精品久久久久777| 精品视频在线v|