這里來(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í)有所幫助,也希望大家多多支持腳本之家。