濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Nginx反向代理及負(fù)載均衡如何實(shí)現(xiàn)(基于linux)

Nginx反向代理及負(fù)載均衡如何實(shí)現(xiàn)(基于linux)

熱門標(biāo)簽:長(zhǎng)沙回?fù)芡夂粝到y(tǒng) 山東電信外呼系統(tǒng)靠譜嗎 ai電話機(jī)器人營(yíng)銷 江蘇自動(dòng)外呼系統(tǒng)一般多少錢 比較穩(wěn)定的外呼系統(tǒng) 400 電話 申請(qǐng)費(fèi)用 鸚鵡螺號(hào)航海地圖標(biāo)注時(shí)間 云南云電銷機(jī)器人招商 信貸電銷機(jī)器人系統(tǒng)

這里來(lái)試驗(yàn)下nginx的反向代理。

反向代理(Reverse Proxy)方式是指以代理服務(wù)器來(lái)接受internet上的連接請(qǐng)求,然后將請(qǐng)求轉(zhuǎn)發(fā)給內(nèi)部網(wǎng)絡(luò)上的服務(wù)器,并將從服務(wù)器上得到的結(jié)果返回給internet上請(qǐng)求連接的客戶端,此時(shí)代理服務(wù)器對(duì)外就表現(xiàn)為一個(gè)反向代理服務(wù)器。

在我們的java項(xiàng)目中的體現(xiàn)就是,通過(guò)80端口訪問(wèn),Nginx接收到,然后進(jìn)行轉(zhuǎn)發(fā)給tomcat服務(wù)器,再將服務(wù)器的結(jié)果給返回。

這里需要修改nginx.conf文件。

upstream backend {
  #代理的IP weight權(quán)重大的,接收的訪問(wèn)量就大,反之
  server localhost:8084 weight=50;
  server localhost:8088 weight=50;
}

將接收的請(qǐng)求進(jìn)行轉(zhuǎn)發(fā):

# / 所有做負(fù)載均衡 + 反向代理
    location / {
      root  /data/wwwroot1;
      index index.html index.htm;#索引文件
      proxy_pass  http://backend;
    }

這樣,通過(guò)請(qǐng)求nginx的請(qǐng)求,就可以被分配轉(zhuǎn)發(fā)到tomcat上去。這里我是定義了兩臺(tái)tomcat服務(wù)器,同時(shí)用來(lái)做負(fù)載均衡的處理。通過(guò)設(shè)置weight,可以控制訪問(wèn)量。

具體配置代碼如下;

#user nobody;
# worker 工作進(jìn)程 一般設(shè)置 CPU數(shù) * 核數(shù)
worker_processes 1;
 
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
 
#pid    logs/nginx.pid;
 
# 設(shè)置連接特性
events {
  worker_connections 1024;#1個(gè)worker產(chǎn)生多少個(gè)連接數(shù)
}
 
# 配置HTTP服務(wù)器的主要段
http {
  include    mime.types;
  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壓縮功能設(shè)置
  gzip on;
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 6;
  gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
  gzip_vary on;
   
  #設(shè)定負(fù)載均衡后臺(tái)服務(wù)器列表
  upstream backend {
    #代理的IP weight權(quán)重大的,接收的訪問(wèn)量就大,反之
    server localhost:8084 weight=50;
    server localhost:8088 weight=50;
  }
   
   
   
  server {
    listen    2022;
    server_name localhost;
    charset utf-8;
    access_log logs/wwwroot2.access.log main;
    location / {
      root  /data/wwwroot2;
      index index.html index.htm;#索引文件
    }
  }
  # 虛擬主機(jī)段
  server {
    listen    80;
    server_name localhost;
    root /data/wwwroot1;
    charset utf-8;
    #訪問(wèn)日志
    access_log logs/wwwroot1.access.log main;
    # / 所有做負(fù)載均衡 + 反向代理
    location / {
      root  /data/wwwroot1;
      index index.html index.htm;#索引文件
      proxy_pass  http://backend;
    }
 
    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;
  #  }
  #}
 
}

 測(cè)試結(jié)果發(fā)現(xiàn),通過(guò)訪問(wèn)80端口的地址,展現(xiàn)的結(jié)果是基本五五開(kāi)的。

還會(huì)隨機(jī)訪問(wèn)到

以上兩個(gè)截圖,分別對(duì)應(yīng)了我的兩個(gè)tomcat服務(wù)器下的測(cè)試文件。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

標(biāo)簽:運(yùn)城 嘉興 澳門 拉薩 烏海 亳州 衡陽(yáng) 齊齊哈爾

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Nginx反向代理及負(fù)載均衡如何實(shí)現(xiàn)(基于linux)》,本文關(guān)鍵詞  Nginx,反向,代理,及,負(fù)載,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Nginx反向代理及負(fù)載均衡如何實(shí)現(xiàn)(基于linux)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Nginx反向代理及負(fù)載均衡如何實(shí)現(xiàn)(基于linux)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    鲁山县| 从化市| 灵台县| 武清区| 华坪县| 县级市| 台江县| 陵川县| 内黄县| 阳信县| 景东| 临夏县| 衡南县| 锡林郭勒盟| 岱山县| 万宁市| 贵阳市| 观塘区| 晋州市| 宾阳县| 汕头市| 安义县| 高安市| 吴桥县| 黄浦区| 田林县| 无为县| 嘉黎县| 磐石市| 墨脱县| 长垣县| 洛川县| 兴安盟| 瓦房店市| 汝城县| 泗洪县| 钟祥市| 望江县| 磐安县| 稷山县| 宜黄县|