您的位置:首页 > 运维架构 > Tomcat

nginx 整合tomcat 配置。upstream配置代理负载。

2015-06-08 17:23 706 查看
Nginx配置---转载开始

Nginx 的配置主要是修改 /usr/local/nginx/conf/nginx,conf文件

#配置用户和用户组

user www www;

#工作进程数,建议设置为CPU的总核数

worker_processes 2;

#全局错误日志定义类型,日志等级从低到高依次为: debug | info | notice | warn | error | crit

error_log logs/error.log info;

#记录主进程ID的文件

pid /usr/local/nginx/nginx.pid;

#一个进程能打开的文件描述符最大值,理论上该值因该是最多能打开的文件数除以进程数。但是由于nginx负载并不是完全均衡的,

#所以这个值最好等于最多能打开的文件数。执行 sysctl -a | grep fs.file 可以看到linux文件描述符。

worker_rlimit_nofile 65535;

#工作模式与连接数上限

events {

#工作模式,linux2.6版本以上用epoll

use epoll;

#单个进程允许的最大连接数

worker_connections 65535;

}

#设定http服务器,利用它的反向代理功能提供负载均衡支持

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 记录了哪些用户,哪些页面以及用户浏览器、ip和其他的访问信息

access_log logs/access.log main;

#服务器名字的hash表大小

server_names_hash_bucket_size 128;

#客户端请求头缓冲大小。nginx默认会用client_header_buffer_size这个buffer来读取header值,

#如果header过大,它会使用large_client_header_buffers来读取。

#如果设置过小HTTP头/Cookie过大 会报400 错误 nginx 400 bad request

#如果超过buffer,就会报HTTP 414错误(URI Too Long)

#nginx接受最长的HTTP头部大小必须比其中一个buffer大,否则就会报400的HTTP错误(Bad Request)。

client_header_buffer_size 32k;

large_client_header_buffers 4 32k;

#客户端请求体的大小

client_body_buffer_size 8m;

#隐藏ngnix版本号

server_tokens off;

#忽略不合法的请求头

ignore_invalid_headers on;

#指定启用除第一条error_page指令以外其他的error_page。

recursive_error_pages on;

#让 nginx 在处理自己内部重定向时不默认使用 server_name 设置中的第一个域名

server_name_in_redirect off;

#开启文件传输,一般应用都应设置为on;若是有下载的应用,则可以设置成off来平衡网络I/O和磁盘的I/O来降低系统负载

sendfile on;

#告诉nginx在一个数据包里发送所有头文件,而不一个接一个的发送。

tcp_nopush on;

#告诉nginx不要缓存数据,而是一段一段的发送--当需要及时发送数据时,就应该给应用设置这个属性,

#这样发送一小块数据信息时就不能立即得到返回值。

tcp_nodelay on;

#长连接超时时间,单位是秒

keepalive_timeout 65;

#gzip模块设置,使用 gzip 压缩可以降低网站带宽消耗,同时提升访问速度。

gzip on;
#开启gzip

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; #压缩类型

#upstream作负载均衡,在此配置需要轮询的服务器地址和端口号,max_fails为允许请求失败的次数,默认为1.

#weight为轮询权重,根据不同的权重分配可以用来平衡服务器的访问率。

upstream hostname {

server 192.168.2.149:8080 max_fails=0 weight=1;

server 192.168.1.9:8080 max_fails=0 weight=1;

}

#主机配置

server {

#监听端口

listen 80;

#域名

server_name hostname;

#字符集

charset utf-8;

#单独的access_log文件

access_log logs/192.168.2.149.access.log main;

#反向代理配置,将所有请求为http://hostname的请求全部转发到upstream中定义的目标服务器中。

location / {

#此处配置的域名必须与upstream的域名一致,才能转发。

proxy_pass http://hostname;
proxy_set_header X-Real-IP $remote_addr;

}

#启用nginx status 监听页面

location /nginxstatus {

stub_status on;

access_log on;

}

#错误页面

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

}

至此,nginx基本的负载均衡配置完成,实验中部署2台tomcat, 然后访问时返回不同的结果,在浏览器中输入地址,确实能看到不同的返回结果。nginx配置文件的内容还有待于继续学习。

-------转载结束。

自己配置的nginx+2个tomcat实现负载如下

nginx.conf配置文件如下:

#######################################################################

#

# This is the main Nginx configuration file.

#

# More information about the configuration options is available on

# * the English wiki - http://wiki.nginx.org/Main

# * the Russian documentation - http://sysoev.ru/nginx/
#

#######################################################################

#----------------------------------------------------------------------

# Main Module - directives that cover basic functionality

#

# http://wiki.nginx.org/NginxHttpMainModule

#

#----------------------------------------------------------------------

user nginx;

worker_processes 1;

error_log /var/log/nginx/error.log;

#error_log /var/log/nginx/error.log notice;

#error_log /var/log/nginx/error.log info;

pid /var/run/nginx.pid;

worker_rlimit_nofile 24791;

#----------------------------------------------------------------------

# Events Module

#

# http://wiki.nginx.org/NginxHttpEventsModule

#

#----------------------------------------------------------------------

events {

use epoll;

worker_connections 1024;

}

#----------------------------------------------------------------------

# HTTP Core Module

#

# http://wiki.nginx.org/NginxHttpCoreModule

#

#----------------------------------------------------------------------

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 0;

keepalive_timeout 65;

gzip on;

# Load config files from the /etc/nginx/conf.d directory

# The default server is in conf.d/default.conf

include /etc/nginx/conf.d/*.conf;

}

------nginx.conf配置文件结束。nginx的配置文件引用了include /etc/nginx/conf.d/*.conf 的所有配置文件,所以改该目录下的配置文件。

--------如下是default.conf配置文件开始

#

# The default server

#

upstream hostname{

server 127.0.0.1:8080 max_fails=0 weight=1;

server 127.0.0.1:8090 max_fails=0 weight=1;

}

server {

listen 80;

server_name _;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {

proxy_pass http://hostname;

proxy_set_header X-Real-IP $remote_addr;

#root /usr/share/nginx/html;

#index index.html index.htm;

# example

#ModSecurityEnabled on;

#ModSecurityConfig /etc/nginx/modsecurity.conf;

}

error_page 404 /404.html;

location = /404.html {

root /usr/share/nginx/html;

}

# redirect server error pages to the static page /50x.html

#

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root /usr/share/nginx/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;

#}

}

--------如下是default.conf配置文件结束

配置完毕后实现负载,但是每次刷新都会跳到不同服务器好像不妥,应该用 ip_hash解决session问题(待续核查)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: