实战演示
Window下实战
windows下通过powershell启动jar包:
-
java -jar .\包名 --server.port=8080
-
java -jar .\包名 --server.port=8081
浏览器访问8080和8081都可以访问成功;
但是这个是不合理的,所以在中间加一个Nginx代理,用户的请求到达Nginx,用户只需要访问一个80端口,由Nginx帮我们自动的转发请求到后台服务器上;
这就是反向代理,代理服务器端;
我们还希望8081端口的服务器上多一些请求,808端口的服务器上少一些请求,这也可以通过在Nginx中的配置进行实现;
所以需要做两个事情:
- 配置Nginx的监听端口为80端口,实现反向代理
- 配置8081和8080端口服务器的负载比重
配置操作:
-
打开 nginx - conf - nginx.conf
-
在80端口下做配置:
- 配置反向代理 配置负载均衡
-
重新加载nginx配置文件
进入nginx.exe所在目录,cmd命令: nginx -s reload
-
通过powershall后端观察日志文件,改变权重,多次访问80端口,就可以观察到加权不同的情况下,nginx代理情况的负载比例;
实际项目中的配置:
-
反向代理
-
负载均衡
-
动静分离
html… 静态请求可以通过nginx处理,不用去请求tomcat
-
重写
80 http rewrite —> https 443
就是说原来http 80端口的请求,我想让他走到 https 443端口
配置文件详解:
http默认端口80;https默认端口443;
# 1、全局配置 # 全局配置中的所有配置都可以生效; 比如指定用户、进程信息、进程... #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; # 2、events事件 # 里面有最大连接数、监听的事件... events { # 现在的事件是1024个 worker_connections 1024; } # 3、http # 静态资源文件基本的小配置都在这;里面有多个server{},就可以配置不同个服务 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; # 这里配置了80端口,80端口也是http的默认端口 server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; 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,所以监听443端口 # 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; # } #} }
全局配置 events { 事件配置 worker_connections 1024; } http { http全局配置 upstream xx{ // 负载均衡配置 是为了给下面server中的location中的proxy代理配置负载 # 服务器资源 server 127.0.0.1:8080 weight=1; server 127.0.0.1:8081 weight=2; } server { server配置 listen 80; server_name localhost; location /{ # 如果访问80端口的根目录,那么就走到这里请求配置 www.kuangstudy.com # xxx 129.xxx # 反向代理配置 proxy_pass http://kuangstudy } location /admin{ # xxx 47.xxx 如果访问根目录下的admin,就会走47这个服务器 www.kuangstudy.com/admin } } server { server配置 listen 443; server_name localhost; // 代理配置 } }