Nginx反向代理及負(fù)載均衡如何實(shí)現(xiàn)(基于linux)
這里來(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é)果是基本五五開的。

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

以上兩個(gè)截圖,分別對(duì)應(yīng)了我的兩個(gè)tomcat服務(wù)器下的測(cè)試文件。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
nginx中(13: Permission denied)權(quán)限問(wèn)題的解決辦法
"nginx 13: Permission denied" 錯(cuò)誤通常表示nginx進(jìn)程沒有足夠的權(quán)限來(lái)訪問(wèn)特定的文件或目錄,本文就來(lái)介紹一下解決方法,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
Ubuntu16.04上為Nginx創(chuàng)建自簽名SSL證書
SSL證書是實(shí)現(xiàn)HTTPS的關(guān)鍵組成部分,本文主要介紹了Ubuntu16.04上為Nginx創(chuàng)建自簽名SSL證書,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05
Nginx+Tomcat群集的實(shí)現(xiàn)示例
這篇文章主要介紹了Nginx+Tomcat群集的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
淺析nginx 客戶端返回499的錯(cuò)誤碼的問(wèn)題
我們服務(wù)器客戶端一直有返回錯(cuò)誤碼499的日志,以前覺得比例不高,就沒有仔細(xì)查過(guò),最近有領(lǐng)導(dǎo)問(wèn)這個(gè)問(wèn)題,為什么耗時(shí)只有0.0幾秒,為啥還499了?最近幾天就把這個(gè)問(wèn)題跟蹤定位了一下,這里做個(gè)記錄,對(duì)nginx返回499錯(cuò)誤碼相關(guān)知識(shí)感興趣的朋友一起看看吧2022-10-10
Nginx如何根據(jù)前綴路徑轉(zhuǎn)發(fā)到不同的Flask服務(wù)
這篇文章主要介紹了Nginx如何根據(jù)前綴路徑轉(zhuǎn)發(fā)到不同的Flask服務(wù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
云服務(wù)器使用寶塔搭建Python環(huán)境,運(yùn)行django程序
本文詳細(xì)講解了在云服務(wù)器使用寶塔搭建Python環(huán)境,運(yùn)行django程序的方法。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12

