centos安装nginx,配置负载均衡
生活随笔
收集整理的這篇文章主要介紹了
centos安装nginx,配置负载均衡
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
location?/?{???? ????????????#設置主機頭和客戶端真實地址,以便服務器獲取客戶端真實IP???? ????????????proxy_set_header?Host?$host;???? ????????????#?nginx非80端口處理????? ????????????#proxy_set_header?Host?$host:$server_port;????? ????????????#?獲取真實IP????? ????????????proxy_set_header?X-Real-IP?$remote_addr;???? ????????????#獲取代理者的真實ip???? ????????????proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for;???? ???? ????????????#禁用緩存???? ????????????proxy_buffering?off;???? ????????????#設置反向代理的地址???? ????????????proxy_pass?http://192.168.1.1;(根據實際情況修改)???? ??????}????
#user?nobody;?? worker_processes?2;?? ??? #error_log?logs/error.log;?? #error_log?logs/error.log?notice;?? #error_log?logs/error.log?info;?? ??? #pid????logs/nginx.pid;?? ??? ??? events?{?? ??accept_mutex?on;?#設置網路連接序列化,防止驚群現象發生,默認為on?? ??multi_accept?on;?#設置一個進程是否同時接受多個網絡連接,默認為off?? ??worker_connections?1024;#最大連接數?? }?? ??? ??? http?{?? ??include????mime.types;#文件擴展名與文件類型映射表,此映射表主要用于部署在本nginx上的靜態資源?? ??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;?? ??? ??#反向代理?? ??? ??#【配置1】此配置是[配置4]和[配置5]的結合?? ??#此配置將請求轉發到兩個WEB服務器,根據客戶端IP分配目標主機,同時按權重分配流量?? ??upstream?app1?{?? ????ip_hash;?? ????server?192.168.14.132:8080?weight=5;?? ????server?192.168.14.133:80?weight=3;?? ??}?? ??? ??#【配置2】?? ??#默認負載平衡配置,nginx應用HTTP負載平衡來分發請求。?? ??#upstream?app1?{?? ??#??server?192.168.14.132:8080;?? ??#??server?192.168.14.133:80;?? ??#}?? ??? ??#【配置3】?? ??#最小連接負載平衡配置,nginx將盡量不使用繁忙的服務器,而是將新請求分發給不太忙的服務器。?? ??#upstream?app1?{?? ??#??least_conn;?? ??#??server?192.168.14.132:8080;?? ??#??server?192.168.14.133:80;?? ??#}?? ??? ??#【配置4】?? ??#會話持久性配置,使用ip-hash,客戶端的IP地址用作散列密鑰,?? ??#以確定應為客戶端請求選擇服務器組中的哪個服務器。?? ??#此方法確保來自同一客戶端的請求將始終定向到同一服務器,除非此服務器不可用。?? ??#upstream?app1?{?? ??#??ip_hash;?? ??#??server?192.168.14.132:8080;?? ??#??server?192.168.14.133:80;?? ??#}?? ??? ??#【配置5】?? ??#加權負載平衡配置,通過使用服務器權重進一步影響nginx負載平衡算法。?? ??#未配置權重的服務器,意味著所有指定的服務器被視為對特定負載平衡方法同等資格。?? ??#upstream?app1?{?? ??#??ip_hash;?? ??#??server?192.168.14.132:8080?weight=3;?? ??#??server?192.168.14.133:80?weight=2;?? ??#??server?192.168.14.134:80;?? ??#??server?192.168.14.135:80;?? ??#}?? ??? ??? ??server?{#可配置多個server以監聽不同IP和不同端口?? ????listen????80;#監聽的端口?? ????server_name?localhost;#監聽的服務器?? ??? ????#charset?koi8-r;?? ??? ????#access_log?logs/host.access.log?main;?? ??? ????#反斜桿代表所有連接,此配置目的是將所有連接交給名為app1的upstream代理,實現負載平衡?? ????location?/?{?????? ????#設置主機頭和客戶端真實地址,以便服務器獲取客戶端真實IP?? ????????proxy_set_header?Host?$host;?? ?? ????????proxy_set_header?X-Real-IP?$remote_addr;?? ????????proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for;?? ?? ????????#禁用緩存?? ????????proxy_buffering?off;?? ?? ???????#反向代理的配置(地址按實際情況配置)?? ???????proxy_pass?http://app1;?? ????}?? ??? ????#圖片文件路徑,一般來說,靜態文件會部署在本機以加快響應速度?? ????#可配置多個這樣的location,滿足各種需求?? ????location?~\.(gif|jpg|png)$?{?? ??????root?/home/root/images;?? ????}?? ??? ????location?~\.(iso|zip|txt|doc|docx)$?{?? ??????root?/home/root/files;?? ????}?? ??? ??? ????#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;?? ????}?? ??? ??? ????#?FastCGI是CGI全稱是“公共網關接口”(Common?Gateway?Interface)?? ????#對于我來說,使用Tomcat代替即可,請忽略此配置。?? ????#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;?? ????#}?? ??? ????#?添加黑名單,禁止某某訪問特定文件?? ????#?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?-s?reload??
5、Nginx內置變量含義
[html]view plaincopy名稱????版本????說明(變量列表來源于文件?ngx_http_variables?)??? $args????1.0.8????請求中的參數;??? $binary_remote_addr????1.0.8????遠程地址的二進制表示??? $body_bytes_sent????1.0.8????已發送的消息體字節數??? $content_length????1.0.8????HTTP請求信息里的"Content-Length";??? $content_type????1.0.8????請求信息里的"Content-Type";??? $document_root????1.0.8????針對當前請求的根路徑設置值;??? $document_uri????1.0.8????與$uri相同;??? $host????1.0.8????請求信息中的"Host",如果請求中沒有Host行,則等于設置的服務器名;??? $hostname????1.0.8??????? $http_cookie????1.0.8????cookie?信息??? $http_post????1.0.8??????? $http_referer????1.0.8????引用地址??? $http_user_agent????1.0.8????客戶端代理信息??? $http_via????1.0.8?????最后一個訪問服務器的Ip地址。??? $http_x_forwarded_for????1.0.8?????相當于網絡訪問路徑。??? $is_args????1.0.8??????? $limit_rate????1.0.8????對連接速率的限制;??? $nginx_version????1.0.8??????? $pid????1.0.8??????? $query_string????1.0.8????與$args相同;??? $realpath_root????1.0.8??????? $remote_addr????1.0.8????客戶端地址;??? $remote_port????1.0.8????客戶端端口號;??? $remote_user????1.0.8????客戶端用戶名,認證用;??? $request????1.0.8????用戶請求??? $request_body????1.0.8??????? $request_body_file????1.0.8????發往后端的本地文件名稱??? $request_completion????1.0.8??????? $request_filename????1.0.8????當前請求的文件路徑名??? $request_method????1.0.8????請求的方法,比如"GET"、"POST"等;??? $request_uri????1.0.8????請求的URI,帶參數;??? ...??
1、安裝nginx
? ?安裝教程,參照:http://mp.weixin.qq.com/s/RVaRlRpHqZRjCaXGmOlfKw
2、反向代理的配置
?修改部署目錄下conf子目錄的nginx.conf文件的內容
3、負載均衡的配置
?nginx 的 upstream默認是以輪詢的方式實現負載均衡,這種方式中,每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器down掉,能自動剔除。另外一種方式是ip_hash:每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題。?
[html]view plaincopy4、配置完后,記得執行以下命令生效配置
[html]view plaincopy總結
以上是生活随笔為你收集整理的centos安装nginx,配置负载均衡的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个简单的游戏服务器框架 .
- 下一篇: 我对STL的一些看法(二)认识vecto