nginx从1.9.0版本开始,新增了ngx_stream_core_module模块,使nginx支持四层负载均衡。默认编译的时候该模块并未编译进去,需要编译的时候添加--with-stream参数,使其支持stream代理
- nginx stream配置
https://nginx.org/en/docs/stream/ngx_stream_core_module.html
worker_processes auto;
error_log /var/log/nginx/error.log info;
events {
worker_connections 1024;
}
# stream段的配置要与http段在同级目录
stream {
upstream app {
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:8081;
}
server {
listen 8000;
proxy_pass app;
}
}
#######################################
stream {
upstream zk_server {
server 172.16.3.8:2181 weight=5;
}
server {
listen 2181 tcp;
proxy_responses 1;
proxy_timeout 20s;
proxy_pass zk_server;
}
}
#######################################
upstream job {
server 127.0.0.1:8088;
}
server {
listen 80;
server_name jobs.example.com;
access_log /var/log/nginx/job.log main;
client_max_body_size 0;
location / {
root html;
index index.html index.htm;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Referer $http_referer;
proxy_pass http://job;
}
}
#######################################
# 使用http中的upstream配置
http {
upstream websrvs {
server 10.0.0.7:80 weight=1;
server 10.0.0.17:80 weight=1;
}
server {
listen 80;
location / {
proxy_pass http://websrvs/;
}
}
}
#如果location定义其uri时使用了正则表达式模式(包括~,~*,但不包括^~),则proxy_pass之后必须不能使用uri; 用户请求时传递的uri将直接附加至后端服务器之后
server {
server_name HOSTNAME;
location ~|~* /uri/ {
proxy_pass http://host:port; #proxy_pass后面的url 不能加/
}
}
proxy_pass服务url配置
location /web {
index index.html
proxy_pass http://10.0.0.1:8080;
# 8080后无 / 符号,需要将localtion后面url附加到proxy_pass指定的url后面,类似root
##proxy_pass后不带/符号,访问 http://nginx/web 等于访问 http://10.0.0.1:8080/web/
}
location /web {
index index.html
proxy_pass http://10.0.0.1:8080/;
# 8080后有 / 符号,相当于置换,即访问/web时实际返回proxy_pass指定url带/的内容,类似alias
##proxy_pass后带/符号,访问http://nginx/web/index.html 等于访问 http://10.0.0.1:8080
}
- 生产配置
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 5s;
proxy_send_timeout 5s;
proxy_read_timeout 5s;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
upstream fn {
server 192.168.99.104:6501;
server 192.168.99.104:6502;
server 192.168.99.104:6503;
}
server {
listen 6601;
server_name 192.168.99.104;
location / {
proxy_pass http://fn;
#proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forearded-For $proxy_add_x_forwarded_for;
#proxy_cache my_cache
#proxy_cache_key $host$uri#is_args$args;
#proxy_cache_valid 200 304 302 1d;
index index.html index.htm;
}
}
}