nginx負載均衡配置
文件目錄結構
nginx版本:1.9.7
這裏主要介紹配置文件目錄conf
目錄:/usr/local/nginx/conf
包含配置文件:nginx.conf
backends/demo.cn.conf
sites/demo.cn.conf
具體文件配置如下:
nginx.conf:
#user nobody;
worker_processes 1;
#nginx異常日誌
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 {
fastcgi_intercept_errors on;
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""::::" "$Host"';
#nginx接收請求的日誌
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
#keepalive_timeout 65;
#include mime.types;
#default_type application/octet-stream;
charset utf-8 ;
server_names_hash_bucket_size 128;
client_header_buffer_size 2k;
large_client_header_buffers 4 4k;
client_max_body_size 8m;
#sendfile on;
tcp_nopush on;
keepalive_timeout 600s;
#fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2
#keys_zone=TEST:10m
#inactive=5m;
fastcgi_connect_timeout 600s;
fastcgi_send_timeout 600s;
fastcgi_read_timeout 600s;
fastcgi_buffer_size 10m;
fastcgi_buffers 8 10m;
fastcgi_busy_buffers_size 10m;
fastcgi_temp_file_write_size 10m;
#fastcgi_cache TEST;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
open_file_cache max=204800 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
#gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#gzip on;
#負載分發具體配置文件
include backends/*; 數據轉發配置
include sites/*;
}
數據轉發配置文件
backends/demo.cn.conf
#mvc為tomcat下係統工程名字
upstream mvc{
server 192.168.3.51:8088; 部署係統的兩台服務器之一(tomcat)
server 192.168.3.51:8089; 部署係統的兩台服務器之一(tomcat)
}
正文處理配置文件
sites/demo.cn.conf
server{
listen 80; 監聽端口
server_name demo.cn; 請求主域名
location ~ /mvc/(.*){ 請求路徑中帶有 /mvc/的處理
proxy_pass https://mvc;
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;
}
}
本次demo的應用名稱為mvc,通過nginx進行訪問的路徑為 demo.cn/mvc/
在局域網訪問時 需要配置host 127.0.0.1 demo.cn
至此,demo全部配置完成,擴展使用nginx的其他功能需閱讀nginx官方文檔
最後更新:2017-04-21 09:30:47