场景:应用部署在内网,部署了两个相同的tomcat(192.168.0.146:8080和192.168.0.149:8080),和一个es(192.168.0.146:9200),现在需要将内网的tomcat和es负载到一台外网主机上(10.30.22.xx),达到外网访问10.30.22.xx:8080可以访问内网tomcat以及访问10.30.22.xx:9999可以访问内网es的目的。
以下是nginx.conf配置文件内容
user root;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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;
access_log logs/access.log main;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
client_header_buffer_size 2k;
large_client_header_buffers 4 8k;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream myApp{
ip_hash;
server 192.168.0.146:8080; //需要负载的web地址
server 192.168.0.149:8080;
}
server {
listen 8080; //监听端口
server_name localhost 10.30.22.xx; //访问地址
client_max_body_size 1024M;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_connect_timeout 900s;
proxy_read_timeout 1200s;
proxy_send_timeout 1200s;
root html;
index index.html index.htm;
}
location / {
proxy_pass http://myApp;
proxy_connect_timeout 1;
proxy_read_timeout 2;
}
location /dag {
proxy_pass http://myApp;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
#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;
}
}
//如果需要转发多个应用到不通端口,可配置多个server。例如:上面的server将192.168.0.146:8080和192.168.0.149:8080转发到10.30.22.xx:8080上,下面的server将192.168.0.146:9200转发到10.30.22.xx:9999上
upstream myes{
ip_hash;
server 192.168.0.146:9200; //需要负载的web地址
}
server {
listen 9999; //监听端口
server_name localhost 10.30.22.xx; //访问地址
client_max_body_size 1024M;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_connect_timeout 900s;
proxy_read_timeout 1200s;
proxy_send_timeout 1200s;
root html;
index index.html index.htm;
}
location / {
proxy_pass http://myes;
proxy_connect_timeout 1;
proxy_read_timeout 2;
}
#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;
}
}
}