Nginx生產配置大全-反向代理與負載均衡

1、Nginx安裝

下載地址 http://nginx.org/en/download.html
依賴安裝開發工具包:
yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel

解壓安裝包進入nginx源碼目錄,配置環境變量
./configure --prefix=/usr/local/nginx --with-pcre
–with-http_stub_status_module --with-http_ssl_module
–with-http_gzip_static_module --with-http_realip_module \

編譯並安裝nginx源碼
make && make install

2、編譯說明

–prefix=PATH : 指定nginx的安裝目錄。默認 /usr/local/nginx

–conf-path=PATH : 設置nginx.conf配置文件的路徑。nginx允許使用不同的配置文件啓動,通過命令行中的-c選項。默認爲prefix/conf/nginx.conf

–user=name: 設置nginx工作進程的用戶。安裝完成後,可以隨時在nginx.conf配置文件更改user指令。

–with-pcre : 設置PCRE庫的源碼路徑,使用–with-pcre自動找到庫文件。perl正則表達式使用在location指令ngx_http_rewrite_module模塊中。

–with-zlib=PATH : 指定zlib的源碼解壓目錄。啓用的網絡傳輸壓縮模塊ngx_http_gzip_module時需要使用zlib 。

–with-http_ssl_module : 使用https協議模塊,前提是openssl與openssl-devel已安裝

–with-http_stub_status_module : 用來監控 Nginx 的當前狀態

–with-http_realip_module : 改變客戶端請求頭中客戶端IP地址值(例如X-Real-IP 或 X-Forwarded-For),能夠使得後臺服務器記錄原始客戶端的IP地址。

–add-module=PATH : 添加第三方外部模塊,如nginx-sticky-module-ng或緩存模塊。

2、Nginx配置

user root root;	#用戶和用戶組
worker_processes 8; #nginx進程數,設置爲等於CPU總核心數。
worker_rlimit_nofile 65535; #指定進程可以打開的最大文件數

#全局錯誤日誌
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;	#進程pid文件


events {
    
    worker_connections  2048;	#單個進程最大連接數
    accept_mutex on;	#設置網路連接序列化,防止驚羣現象發生,默認爲on
    multi_accept on;  	#設置一個進程是否同時接受多個網絡連接,默認爲off 
    
}


http {
    include       mime.types;	#文件擴展名與文件類型映射表
    default_type  application/octet-stream;
    server_tokens   off; # 關閉nginx版本號
    #輸出日誌格式
    log_format main  '$remote_addr - $remote_user [$time_local] "$request" '
               '$status $body_bytes_sent "$http_referer" '
               '"$http_user_agent" $http_x_forwarded_for';
  
    charset UTF-8;

    #access_log  logs/access.log  main;

    sendfile        on;	#sendfile指令指定 nginx 是否調用sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,必須設爲on。
    #tcp_nopush     on;

    keepalive_timeout 180;	#長連接超時時間,單位是秒
    
    #FastCGI優化服務性能,提高訪問速度。
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    
    #gzip模塊設置
    gzip on; #開啓gzip壓縮輸出
    gzip_min_length 5k;    #最小壓縮文件大小
    gzip_buffers 4 16k;    #壓縮緩衝區
    gzip_http_version 1.0; #壓縮版本(默認1.1,前端如果是squid2.5請使用1.0)
    gzip_comp_level 2;     #壓縮等級
    gzip_types text/plain application/x-javascript text/css application/xml;#壓縮類型,默認就已經包含textml,所以下面就不用再寫了,寫上去也不會有問題,但是會有一個warn。
    gzip_vary on;

    #負載均衡配置的4中方式
    upstream myserver {
     
        #upstream的負載均衡,weight是權重,可以根據機器配置定義權重。weigth參數表示權值,權值越高被分配到的機率越大。
        server 192.168.172.51:80 weight=1;
        server 192.168.172.52:80 weight=3;

        #nginx的upstream目前支持4種方式的分配
        #1、輪詢(默認)
        #每個請求按時間順序逐一分配到不同的後端服務器,如果後端服務器down掉,能自動剔除。
        #2、權重
        #指定輪詢機率,weight和訪問比率成正比,用於後端服務器性能不均的情況。
        #例如:
        #upstream myserver {
        #    server 192.168.0.54 weight=2;
        #    server 192.168.0.55 weight=3;
        #}
        #2、ip_hash
        #每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端服務器,可以解決session的問題。
        #例如:
        #upstream myserver {
        #    ip_hash;
        #    server 192.168.0.54:80;
        #    server 192.168.0.55:80;
        #}
        #3、fair(第三方)
        #按後端服務器的響應時間來分配請求,響應時間短的優先分配。
        #upstream myserver {
        #    server server1;
        #    server server2;
        #    fair;
        #}
        #4、url_hash(第三方)
        #按訪問url的hash結果來分配請求,使每個url定向到同一個後端服務器,後端服務器爲緩存時比較有效。
        #例:在upstream中加入hash語句,server語句中不能寫入weight等其他的參數,hash_method是使用的hash算法
        #upstream myserver {
        #    server squid1:9527;
        #    server squid2:9527;
        #    hash $request_uri;
        #    hash_method crc32;
        #}

        #tips:
        #upstream bakend{#定義負載均衡設備的Ip及設備狀態}{
        #    ip_hash;
        #    server 192.168.0.1:9090 down;
        #    server 192.168.0.1:9080 weight=2;
        #    server 192.168.0.1:9060;
        #    server 192.168.0.1:9070 backup;
        #}
        #在需要使用負載均衡的server中增加 proxy_pass http://myserver/;

        #每個設備的狀態設置爲:
        #1.down表示單前的server暫時不參與負載
        #2.weight爲weight越大,負載的權重就越大。
        #3.max_fails:允許請求失敗的次數默認爲1.當超過最大次數時,返回proxy_next_upstream模塊定義的錯誤
        #4.fail_timeout:max_fails次失敗後,暫停的時間。
        #5.backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這臺機器壓力會最輕。

        #nginx支持同時設置多組的負載均衡,用來給不用的server來使用。
        #client_body_in_file_only設置爲On
        #client_body_temp_path設置記錄文件的目錄 可以設置最多3層目錄
        #location對URL進行匹配.可以進行重定向或者進行新的代理 負載均衡
    }
     
     
    server {
        listen       80;
        server_name  xxx.com;

        charset UTF-8;
        access_log  logs/80_access.log;
        error_log  logs/80_error.log;
	 
        location / {
	    #root path;  					#根目錄
          #index index.html;  			#設置默認頁面
	    proxy_pass http://myserver;		#反向代理到upstream上游服務器
          proxy_http_version 1.1; 			#長連接配置支持HTTP1.1協議
	    proxy_set_header Upgrade $http_upgrade;
	    proxy_set_header Connection "upgrade";
	    proxy_set_header Host $host;
	    proxy_set_header X-Real-IP $remote_addr;
	    proxy_set_header X-Forwarded-Proto $scheme;
	    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	    client_max_body_size       1024k;	#客戶端請求的最大單文件字節數
	    client_body_buffer_size    1024k;	#客戶端緩衝區代理緩衝用戶端請求的最大字節數
	    proxy_connect_timeout      180;		#nginx跟後端服務器連接超時時間
	    proxy_send_timeout         180;		#後端服務器數據回傳時間_就是在規定時間之內後端服務器必須傳完所有的數據
	    proxy_read_timeout         180;		#連接成功後,後端服務器響應時間
	    proxy_buffer_size          512k;	#代理服務器讀取的第一部分應答的緩衝區大小
	    proxy_buffers              4 512k; 	#緩衝區大小
	    proxy_busy_buffers_size    512k;	#高負荷下緩衝大小(proxy_buffers*2)
	    proxy_temp_file_write_size 1024k;	#設定緩存文件大小
        }

	#靜態文件,nginx自己處理,不去請求後端tomcat
	#正則語法說明:* ~ 爲區分大小寫匹配,* ~* 爲不區分大小寫匹配,* !~!~*分別爲區分大小寫不匹配及不區分大小寫不匹配
	#location ~* /download/ {
	#	root /app;
	#}
	
	#location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
	#{
	#	root /app;  #靜態資源在nginx服務器上的目錄
	#	expires 30d; #靜態資源緩存時間30天
	#}
	
	#location /status {
	#	stub_status on;
	#	access_log off;
	#	allow 192.168.80.53/54; #允許設置的IP訪問,設置白名單
	#	deny all;		# 拒絕白名單以外的IP訪問
	#}
	  
	#location ~ ^/(WEB-INF)/ {
	#	deny all;	# 拒絕白名單以外的IP訪問
	#}

        #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  xxx.com;

    #last 相當於Apache裏的[L]標記,表示完成rewrite;* break 終止匹配, 不再匹配後面的規則;* redirect 返回302臨時重定向 地址欄會顯示跳轉後的地址;* permanent 返回301永久重定向 地址欄會顯示跳轉後的地址
    
    #反向代理重定向
    #	if ( $request_uri = "/" ) {
    #         rewrite "/" https://$server_name$request_uri; break;
    #        }

	#if ($scheme = http) {
	#return 301 https://$server_name$request_uri;
	#}

	#if ($server_port = 80) {
	#return 301 https://$server_name$request_uri;
	#}

	
    #error_page 497 https://$server_name$request_uri;
    #    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;
    #    }
    #}

}

3、Nginx啓停

#檢查配置文件是否正確
/usr/local/nginx/sbin/nginx -t

#查看Nginx信息
/usr/local/nginx/sbin/nginx -V

#關閉
/usr/local/nginx/sbin/nginx -s stop 或 pkill nginx

#啓動
/usr/local/nginx/sbin/nginx

#重啓
/usr/local/nginx/sbin/nginx -s reload

作者簡介
思維的持續,一個真的有思想,不穿格子襯衫的程序員。
在這裏插入圖片描述

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