詳解Centos7.2安裝Nginx實現(xiàn)負載平衡
下載Nginx
到官網(wǎng)下載源碼文件,地址:http://nginx.org/en/download.html,選擇最新版本。本人下載的地址為:http://nginx.org/download/nginx-1.10.2.tar.gz,可用wget命令下載,也可以在windows系統(tǒng)上下載好再傳到linux上。
卸載httpd
如果系統(tǒng)默認安裝了httpd服務,卸載之。不卸載也沒關系,這里只是方便默認80端口的處理。
yum -y remove httpd
解壓
tar -xzvf nginx-xxxxxx.tar.gz
安裝編譯器和依賴庫
yum install gcc gcc-c++ zlib-devel pcre-devel openssl-devel openssl-libs openssl -y
如果已經(jīng)安裝,就不必了
安裝前配置
cd命令轉到解壓后的目錄下。
./configure --prefix=/usr/local/nginx
這樣安裝時系統(tǒng)就會把Nginx安裝到/usr/local/nginx目錄下。
編譯
make
安裝
make install
安裝完成,接下來配置環(huán)境變量以后就不用使用絕對路徑來操作Nginx了:
vim /etc/profile.d/http.sh
加入以下內(nèi)容:
export PATH=/usr/local/nginx/sbin:$PATH
生效配置:
source !$
啟動Nginx
nginx
nginx -s 后跟stop、reload來關閉和重載nginx,直接運行nginx則啟動服務。 如果啟動時提示端口被占用,則需要找出被占用的進程,或者更改/usr/local/nginx/conf/nginx.conf文件里的偵聽端口。
訪問Nginx
在瀏覽器上輸入 http://ip:port 如果出現(xiàn)“Welcome to nginx!”字樣,則證明安裝成功。如果訪問不了,先確認防火墻是否禁止相應端口了。
負載平衡配置示例
#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; #設置網(wǎng)路連接序列化,防止驚群現(xiàn)象發(fā)生,默認為on
multi_accept on; #設置一個進程是否同時接受多個網(wǎng)絡連接,默認為off
worker_connections 1024;#最大連接數(shù)
}
http {
include mime.types;#文件擴展名與文件類型映射表,此映射表主要用于部署在本nginx上的靜態(tài)資源
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]的結合
#此配置將請求轉發(fā)到兩個WEB服務器,根據(jù)客戶端IP分配目標主機,同時按權重分配流量
upstream app1 {
ip_hash;
server 192.168.14.132:8080 weight=5;
server 192.168.14.133:80 weight=3;
}
#【配置2】
#默認負載平衡配置,nginx應用HTTP負載平衡來分發(fā)請求。
#upstream app1 {
# server 192.168.14.132:8080;
# server 192.168.14.133:80;
#}
#【配置3】
#最小連接負載平衡配置,nginx將盡量不使用繁忙的服務器,而是將新請求分發(fā)給不太忙的服務器。
#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以監(jiān)聽不同IP和不同端口
listen 80;#監(jiān)聽的端口
server_name localhost;#監(jiān)聽的服務器
#charset koi8-r;
#access_log logs/host.access.log main;
#反斜桿代表所有連接,此配置目的是將所有連接交給名為app1的upstream代理,實現(xiàn)負載平衡
location / {
proxy_pass http://app1;
}
#圖片文件路徑,一般來說,靜態(tài)文件會部署在本機以加快響應速度
#可配置多個這樣的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全稱是“公共網(wǎng)關接口”(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;
# }
#}
}
配置完后,記得執(zhí)行以下命令生效配置
nginx -s reload
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Ubuntu 16.04安裝搜狗拼音輸入法錯誤問題的解決方法
目前來說搜狗輸入法是市場上最為好用和流行的輸入法,最為良心的是還有Linux版本,這讓眾多Linux人士歡呼雀躍。最近在新出的Ubuntu 16.04上安裝sogou輸入法的時候,碰到了一些問題,主要還是依賴包沒有正確安裝,下面讓我們一起來看如何來解決這個問題。2016-11-11
CentOS6.4安裝Apache+MySQL+PHP圖文教程
這篇文章主要介紹了CentOS6.4安裝Apache+MySQL+PHP圖文教程,需要的朋友可以參考下。2016-10-10
linux下查看yum/rpm/dpkg某軟件是否已安裝的方法
這篇文章主要介紹了在linux下查看yum/rpm/dpkg某軟件是否已安裝的方法,文中給出了詳細的示例代碼,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03
CentOS 6/7環(huán)境下通過yum安裝php7的方法
這篇文章主要介紹了CentOS 6/7環(huán)境下通過yum安裝php7的方法,簡單分析了CentOS 6/7服務器環(huán)境下使用yum安裝php7的相關命令與操作步驟,需要的朋友可以參考下2018-03-03

