💗推荐阅读文章💗
- 🌸JavaSE系列🌸👉1️⃣《JavaSE系列教程》
- 🌺MySQL系列🌺👉2️⃣《MySQL系列教程》
- 🍀JavaWeb系列🍀👉3️⃣《JavaWeb系列教程》
- 🌻SSM框架系列🌻👉4️⃣《SSM框架系列教程》
解压两个Tomcat,命名为Tomcat-8080、Tomcat9090:
为了区分访问的是不同的两个Tomcat,这里修改一下两个Tomcat的首页;
Tomcat-8080不用修改,修改Tomcat-9090的配置文件(conf/server.xml):
Tomcat启动时会占用8005端口,Tomcat-8080服务器启动已经占用了8005,因此Tomcat-9090这台服务器我们改成8015;
该端口是HTTP访问的端口,我们改为9090(Tomcat-8080服务器不用修改);
Tomcat启动时会占用8009端口,Tomcat-8080服务器启动已经占用了8009,因此Tomcat-9090这台服务器我们改成8019;
访问http://localhost:8080:
访问http://localhost:9090:
注意:Nginx的所在目录不能包含中文!
Nginx的默认端口是80,由于HTTP协议的默认端口也是80,因此我们直接在浏览器上输入:http://localhost即可访问到Ngxin服务器;
当然,你输入:http://localhost:80也能访问;
关闭Nginx很简单,直接把刚刚的CMD窗口关闭即可;
但Nginx在Windows操作系统上会有些Bug,大家最好测试一下关闭后是否还能访问到Nginx,如果还能访问到,那就意味着Nginx没有关闭,我们需要打开任务管理器来关闭;
按住windows+r,打开运行窗口,输入taskmgr打开任务管理器:
在任务管理器中关闭Nginx相关的所有进程:
还记得一开始的那张图吗:
客户端以后都是访问到Nginx(代理服务器),然后由Nginx将请求转发到Tomcat(web服务器)
所以,我们要在Nginx中进行相关配置;
编辑nginx/conf/nginx.conf配置文件:
怕自己编写错误的那就直接复制我的吧(注意,一定要将配置编写在http模块下):
# 定义一个集群组
upstream web_cluster{# 第一台机器的地址server 127.0.0.1:8080;# 第二台机器的地址server 127.0.0.1:9090;
}
location / {proxy_pass http://web_cluster;index index.html index.htm;
}
#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 {# 定义一个集群组upstream web_cluster{# 第一台机器的地址server 127.0.0.1:8080;# 第二台机器的地址server 127.0.0.1:9090;}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;server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;#location / {# root html;# index index.html index.htm;#}location / {proxy_pass http://web_cluster;index index.html index.htm;}#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;# }#}}
把Tomcat-8080、Tomcat9090、Nginx等服务器都启动;
访问:http://lcoalhost,发现两台web服务器能够同时提供服务;
Nginx默认的集群策略是负载均衡,即将请求平均分到到每台web服务器;