前后端交互
nginx
安装目录:d:/nginx
http://127.0.0.1:9090/
web服务器,不能作为Servlet 容器独立运行,所以通常的工作方式是Nginx配合Tomcat来协同工作。
反向代理
正向代理,比如要访问youtube,但是不能直接访问,只能先找个***软件,通过***软件才能访问youtube. ***软件就叫做正向代理。
反向代理,指的是用户要访问youtube,但是youtube悄悄地把这个请求交给bilibili来做,那么bilibili就是反向代理了。访问nginx,但是nginx把请求交给tomcat来做。
nginx.conf:
location / {
proxy_pass http://127.0.0.1:8111;
}
location / 表示处理所有请求
proxy_pass http://127.0.0.1:8111; 表示把请求都交给http://127.0.0.1:8111来处理动静分离
nginx在处理静态文件的吞吐量上面比tomcat好很多,通常他们俩配合,不会把所有的请求都如本例所示的交给tomcat, 而是把静态请求交给nginx,动态请求,如jsp, servlet,ssm, struts等请求交给tomcat. 从而达到动静分离的效果。
nginx.conf:
location ~\.(css|js|png)$ {
//表示所有的css js png访问都由nginx来做,访问的地址是tomcat_8111的路径
root C:/Users/X7TI/Downloads/tomcat_8111/webapps/ROOT;
}负载均衡
负载均衡的概念就是当访问量很大的时候,一个 Tomcat 吃不消了,这时候就准备多个 Tomcat,由Nginx按照权重来对请求进行分配,从而缓解单独一个Tomcat受到的压力
nginx.conf:
//增加一个upstream ,用来指向这两个tomcat
upstream tomcat_8111_8222{
server 127.0.0.1:8111 weight=1;
server 127.0.0.1:8222 weight=2;
}
//然后修改location,反向代理到上述配置。
location / {
proxy_pass http://tomcat_8111_8222;
}
//weight表示权重,值越大,被分配到的几率越大负载均衡 session问题
当同一个用户第一次访问tomcat_8111 并且登录成功, 而第二次访问却被分配到了tomcat_8222, 这里并没有记录他的登陆状态,那么就会呈现未登录状态了,严重伤害了用户体验
- 解决办法一: ip_hash
通过ip地址标记用户,如果多次请求都是从同一个ip来的,那么就都分配到同一个tomcat.
- 大量请求来之某个局域网,那么相当于就没有负载均衡了
- 如果tomcat_8111 挂了,那么此时nginx只能把请求交给tomcat_8222,但是这里却没有记录session,用户体验依然受影响。
在upstream最后加上ip_hash;
- 解决办法二: redis+tomcat-sessoin-manager
当tomcat1需要保存session值的时候,就可以把它放在Redis上,需要取的时候,也从Redis上取
- 启动redis
- 给两个tomcat使用jar包
jedis-2.5.2.jar, commons-pool2-2.0.jar, tomcat-redis-session-manager1.2.jar。 //放在tomat8111的lib目录下
- 配置两个tomcat
tomcat/conf/context.xml: <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="127.0.0.1" port="6379" database="0" maxInactiveInterval="60" />
- 重启
#user nobody;
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;
keepalive_timeout 65;
#gzip on;
upstream tomcat_8111_8222{
server 127.0.0.1:8111 weight=1;
server 127.0.0.1:8222 weight=5;
}
server {
listen 9090;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcat_8111_8222;
}
location ~\.(css|js|png)$ {
root C:/Users/Administrator/Downloads/tomcat_8111/webapps/ROOT;
}
#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;
}
# 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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}nignx作为服务器显示前端
server {
listen 9090;
server_name localhost;
root C:\Users\Administrator\WebstormProjects\vvv\dist;
index index.php index.html index.htm;
add_header Access-Control-Allow-Origin "*";
default_type 'text/html';
charset utf-8;
#rewrite_log on;
}