1、正向代理与反向代理的区别:
    正向代理:
    隐藏用户的行为;比如聚合视频会员,vpn翻墙;
    反向代理:
    隐藏真实应用服务器ip地址,这样就防止直接攻击我们的部署我们应用服务的服务器;
    2、配置文件详解

    1. server {
    2. # 使用nginx创建了监听端口号码为80
    3. listen 80;
    4. //访问域名
    5. server_name localhost;
    6. #location的配置是根据url规则进行匹配 /代表拦截所有
    7. location / {
    8. #访问我们的根目录的html
    9. root html;
    10. #默认查找index
    11. index index.html index.htm;
    12. }
    13. }

    如果是自定义则将root替换为alias

    配置反向代理:

    1. server {
    2. # 使用nginx创建了监听端口号码为80
    3. listen 80;
    4. //访问域名
    5. server_name localhost;
    6. #location的配置是根据url规则进行匹配 /代表拦截所有
    7. location / {
    8. #配置反向代理
    9. proxy_pass http://127.0.0.1.8080;
    10. #默认查找index
    11. index index.html index.htm;
    12. }
    13. }

    负载均衡:
    专业术语:“上游服务器”其实就是真实服务器;

    1. #定义上游服务器(需要被nginx真实代理访问的服务器) 默认是轮训机制
    2. upstream backServer{
    3. server 127.0.0.1:8080;
    4. server 127.0.0.1:8081;
    5. }
    6. server {
    7. # 使用nginx创建了监听端口号码为80
    8. listen 80;
    9. //访问域名
    10. server_name www.mayikt.com;
    11. #location的配置是根据url规则进行匹配 /代表拦截所有
    12. location / {
    13. ### 指定上游服务器负载均衡服务器
    14. proxy_pass http://backServer;
    15. #默认查找index
    16. index index.html index.htm;
    17. }
    18. }

    指定权重
    注意:负载均衡效果谷歌浏览器需要直接从浏览器中访问才可以生效,
    火狐浏览器没有问题;

    1. upstream backServer{
    2. server 127.0.0.1:8080 weight=1;
    3. server 127.0.0.1:8081 weight=2;
    4. }
    5. server {
    6. # 使用nginx创建了监听端口号码为80
    7. listen 80;
    8. //访问域名
    9. server_name www.mayikt.com;
    10. #location的配置是根据url规则进行匹配 /代表拦截所有
    11. location / {
    12. ### 指定上游服务器负载均衡服务器
    13. proxy_pass http://backServer;
    14. #默认查找index
    15. index index.html index.htm;
    16. }
    17. }

    3、五种负载均衡策略
    轮询,
    指定权重:指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况;
    ip_hash:根据来访的ip进行hash值取余来指定访问的机器,这样就可以解决集群环境中session不共享的问题;
    fair;
    url_hash;
    最后两种依赖于第三方;
    4、服务器集群带来的问题
    分布式session一致性问题,可以使用spring-session或者Token解决,其实底层都是借助了redis做分布式缓存;
    分布式调度重复执行,分布式任务调度平台 xxl-job;
    分布式日志收集问题,可以使用elk_kafka进行日志收集;

    5、待收拾:
    根据域名转发到不同的服务;

    1. #配置多个server
    2. server {
    3. listen 80;
    4. server_name a.mayikt.com;
    5. location / {
    6. proxy_pass http://192.168.0.101:8080;
    7. index index.html index.htm;
    8. }
    9. error_page 500 502 503 504 /50x.html;
    10. location = /50x.html {
    11. root html;
    12. }
    13. }
    14. server {
    15. listen 80;
    16. server_name b.mayikt.com;
    17. location / {
    18. proxy_pass http://192.168.0.101:8081;
    19. index index.html index.htm;
    20. }
    21. error_page 500 502 503 504 /50x.html;
    22. location = /50x.html {
    23. root html;
    24. }
    25. }

    根据项目名转发到不同的服务;(记住proxy_pass端口号后的/),可解决跨域

    1. #使用Nginx搭建API网关保持域名和端口一致
    2. location /b {
    3. proxy_pass http://192.168.18.190:8081/;
    4. index index.html index.htm;
    5. }
    6. location /a {
    7. proxy_pass http://192.168.18.190:8080/;
    8. index index.html index.htm;
    9. }

    nginx-1.17.3.zipNginx.docx