反向代理
服务器向用户提供的
配置反向代理服务器
1、更改配置文件
location / {
proxy_pass http://www.atguigu.com;
# root html;
# index index.html index.htm;
}
配置proxy_pass之后 root和index就不起作用,所以可以将其注释掉;
通过反向代理,将请求转发到www.atguigu.com;
代理到一个服务器上
location / {
proxy_pass http://192.168.102;
# root html;
# index index.html index.htm;
}
2、配置负载均衡
默认一个服务器一次的轮询;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 和server同一级别
# 开始定义在server中设置别名
upstream bieming {
server 192.168.44.102:80;
server 192.168.44.103:80;
}
server {
listen 80;
server_name locallhost;
location / {
# 这里的http://后面可以随便去一个名字,然后在upstream中进行定义就可以了
proxy_pass http://bieming;
# root html;
# index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
3、负载均衡策略
upstream bieming {
server 192.168.44.102:80 weight=8;
server 192.168.44.103:80 weight=2;
}
可以通过weight来调整负载均衡的策略;
4、动静分离
把静态资源部署在nginx中,在请求Tomcat的时候都要先去Nginx,所以直接把静态资源放到Nginx中,实现动静分离;
动静分离配置文件
location / {
# 这里的http://后面可以随便去一个名字,然后在upstream中进行定义就可以了
proxy_pass http://bieming;
# root html;
# index index.html index.htm;
}
location /js {
root html;
index index.html index.htm;
}
location /img {
root html;
index index.html index.htm;
}
location /css {
root html;
index index.html index.htm;
}
精确匹配优先,/js /css /img精确度高于/,所以在关于静态资源访问的时候就会通过配置的:root来查找,相应的静态资源,对于其他不是静态资源的请求,就会被location /下的配置处理;从而实现动静分离;
但是当静态资源太多的时候,上面这样配置就会显得很笨,所以可以采用正则表达式的方式来进行配置处理静态资源的location
location ~*/(js|img|css) {
root html;
index index.html index.htm;
}
5、URLRewrite
rewrite ^/([0-9]+).html$ /index.jsp?pageNum=$1 break;
break:匹配到规则之后就不继续往后匹配;
负载均衡+URLRewrite
# 负载均衡
upstream bieming {
server 192.168.44.102:80 weight=8;
server 192.168.44.103:80 weight=2;
}
server {
listen 80;
server_name locallhost;
location / {
# 重写URL
rewrite ^/([0-9]+).html$ /index.jsp?pageNum=$1 break;
# 这里的http://后面可以随便去一个名字,然后在upstream中进行定义就可以了
proxy_pass http://bieming;
# root html;
# index index.html index.htm;
}
# 动静分离
location ~*/(js|img|css) {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
6、防盗链
# 动静分离
location ~*/(js|img|css) {
## 只允许来之192.168.44.101的才可以引用当前资源;否则返回403
valid_referers 192.168.44.101;
if($invalid_referer){
return 403;
}
root html;
index index.html index.htm;
}
这里的192.168.44.101是域名
HTTPs证书配置
不安全的Http协议
Https原理
非对称加密的非安全情况
也就是钓鱼网站,客户端请求到假的服务端,或者说请求过程中被拦截,钓鱼网站代替我们去请求服务端;