Ngigx+Tomcat配置动静分离,负载均衡

由于公司使用过Ngnix对于刚接触Nginx来说感觉有些好奇,于是研究了下。

本人在windows下使用的版本nginx-1.8.1:

1html. 启动Nlinux创建文件gnix

  双击nginx-1.8.1文件夹中nginx.exe,当任务管理器中存在两个nginx进程时,则说明启动成功!

2. Ngnix常用命令

  nginx -php教程s stop 强制关闭

nginx -s quit 安全关闭

nginx -s reload 改变配置文件的时候,重启nginx工作进程,来时配置文件生效

nginx -s reopen 打开日志文件

3. Nginx配置

  下面配置综合了网上的资料html代码,记下,防止自己忘记。

1 #Nginx所用用户和组
2 #user nobody;
3 #工作的子进程数量(通常等于CPU数量或者2倍于CPU)
4 worker_processes 1;
5
6 #错误日志存放路径
7 #error_log logs/error.log;
8 #error_log logs/error.log notice;
9 #error_log logs/error.log info;
10
11 #指定pid存放文件
12 #pid logs/nginx.pid;
13
14
15 events {
16 #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue
17 #use epoll;
18
19 #使用epoll模型提高性能 win下不需要
20 #use epoll;
21 #允许最大连接数
22 worker_connections 1024;
23 }
24
25
26 http {
27 #扩展名与文件型映射
28 include mime.types;
29 #默认
30 default_type application/octet-stream;
31
32 #定义日志格式
33 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
34 # '$status $body_bytes_sent "$http_referer" '
35 # '"$http_user_agent" "$http_x_forwarded_for"';
36
37 #access_log logs/access.log main;
38
39 # 启用内核复制模式,应该保持开启达到最快IO效率
40 sendfile on;
41 #tcp_nopush on;
42
43 #keepalive_timeout 0;
44 # HTTP1.1支持持久连接alive
45 # 降低每个连接的alive时间可在一定程度上提高可响应连接数量,所以一般可适当降低此值
46 keepalive_timeout 65;
47
48 # 启动gzip压缩功能设置,有效降低网络
49 gzip on;
50 gzip_min_length 1k; #最小1K
51 gzip_buffers 4 16k;
52 gzip_http_version 1.0;
53 gzip_comp_level 2;
54 gzip_types text/plain application/x-javascripttext/css application/xml;
55 gzip_vary on;
56
57 # 静态文件缓存
58 # 最大缓存数量,文件未使用存活期
59 open_file_cache max=655350 inactive=20s;
60 # 验证缓存有效期时间间隔
61 open_file_cache_valid 30s;
62 # 有效期内文件最少使用次数
63 open_file_cache_min_uses 2;
64
65 #xbq add
66 #upstream作负载均衡,在此配置需要轮询的服务器地址和端口号,max_fails为允许请求失败的次数,默认为1.
67 #weight为轮询权重,根据不同的权重分配可以用来平衡服务器的访问率。
68 upstream hostname {
69 server 127.0.0.1:9000 max_fails=0 weight=2;
70 server 127.0.0.1:9001 max_fails=0 weight=2;
71 }
72
73 server {
74 listen 8181;
75 server_name localhost;
76
77 #charset koi8-r;
78 #access_log logs/host.access.log main;
79
80 root /img; #在nginx-1.8.1文件夹中新建img文件夹,用于存放静态资源
81
82 location / {
83 #root html;
84 #index index.html index.htm;
85 #xbq add
86 proxy_pass http://hostname;
87 #下面三条指令允许重新定义和添加一些将被转移到被代理服务器的请求头部信息
88 # 请求头中Host信息
89 proxy_set_header Host $host;
90 # 真实的客户端IP
91 proxy_set_header X-Real-IP $remote_addr;
92 # 代理路由信息,此处取IP有安全隐患
93 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
94 # 真实的用户访问协议
95 proxy_set_header X-Forwarded-Proto $scheme;
96
97 # 默认值default,
98 # 后端response 302时 tomcat header中location的host是http://192.168.1.62:8080
99 # 因为tomcat收到的请求是nginx发过去的, nginx发起的请求url host是http://192.168.1.62:8080
100 # 设置为default后,nginx自动把响应头中location host部分替换成当前用户请求的host部分
101 # 网上很多教程将此值设置成 off,禁用了替换,
102 # 这样用户浏览器收到302后跳到http://192.168.1.62:8080,直接将后端服务器暴露给浏览器
103 # 所以除非特殊需要,不要设置这种画蛇添足的配置
104 proxy_redirect default;
105 client_max_body_size 10m; #允许客户端请求的最大单文件字节数
106 client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
107 proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间
108 proxy_read_timeout 90; #连接成功后,后端服务器响应时间
109 proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
110 proxy_buffers 6 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
111 proxy_busy_buffers_size 64k;#高负荷下缓冲大小(proxy_buffers*2)
112 proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
113
114 }
115
116 #xbq add
117 #配置Nginx动静分离,定义的静态页面直接从/usr/nginxStaticFile(Nginx发布目录)读取。
118 location ~\.(gif|jpg|jpeg|png|css|js|php)$ {
119
120 #expires定义用户浏览器缓存的时间为7天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力 E:/staticResource;
121 expires 7d;
122 }
123
124 #xbq add
125 #启用nginx status 监听页面
126 location /nginxstatus {
127 stub_status on;
128 access_log on;
129 }
130
131 #error_page 404 /404.html;
132
133 # redirect server error pages to the static page /50x.html
134 #
135 error_page 500 502 503 504 /50x.html;
136 location = /50x.html {
137 root html;
138 }
139
140 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
141 #
142 #location ~ \.php$ {
143 # proxy_pass http://127.0.0.1;
144 #}
145
146 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
147 #
148 #location ~ \.php$ {
149 # root html;
150 # fastcgi_pass 127.0.0.1:9000;
151 # fastcgi_index index.php;
152 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
153 # include fastcgi_params;
154 #}
155
156 # deny access to .htaccess files, if Apache's document root
157 # concurs with nginx's one
158 #
159 #location ~ /\.ht {
160 # deny all;
161 #}
162 }
163
164
165 # another virtual host using mix of IP-, name-, and port-based configuration
166 #
167 #server {
168 # listen 8000;
169 # listen somename:8080;
170 # server_name somename alias another.alias;
171
172 # location / {
173 # root html;
174 # index index.html index.htm;
175 # }
176 #}
177
178
179 # HTTPS server
180 #
181 #server {
182 # listen 443 ssl;
183 # server_name localhost;
184
185 # ssl_certificate cert.pem;
186 # ssl_certificate_key cert.key;
187
188 # ssl_session_cache shared:SSL:1m;
189 # ssl_session_timeout 5m;
190
191 # ssl_ciphers HIGH:!aNULL:!MD5;
192 # ssl_prefer_server_ciphers on;
193
194 # location / {
195 # root html;
196 # index index.html index.htm;
197 # }
198 #}
199
200 }