码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • nginx相关使用


    请求某个ip和端口转向指定地址

    请求某个ip和端口转向指定地址
    当请求server_name + listen 时转向 proxy_pass
    localhost:8081 -> 127.0.0.1:8080

    server {
            listen       8081;   #监听端口
            server_name  localhost;   #监听地址
       
            location  / {       
               root html;  #/html目录
               proxy_pass http://127.0.0.1:8080;  #请求转向
               index  index.html index.htm;      #设置默认页       
            } 
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述
    在这里插入图片描述

    参考文章

    https://zhuanlan.zhihu.com/p/431181851?utm_id=0

    根据在浏览器输入的路径不同,跳转到不同端口的服务中。

    请求http://127.0.0.1:9000/nginx8081/ --》 http://localhost:8081/nginx8081/
    请求http://127.0.0.1:9000/nginx8083/ --》 http://localhost:8083/nginx8083/
    注意‘’location ~ /nginx8081/ ‘’ 加了/

    #根据在浏览器输入的路径不同,跳转到不同端口的服务中。
    	server {
            listen       9000;   
            server_name  localhost;   #监听地址       
            
            location  ~ /nginx8081/ {  
               proxy_pass http://localhost:8081;         
            } 
    
            location  ~ /nginx8083/ {  
               proxy_pass http://localhost:8083;         
            } 
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述
    在这里插入图片描述
    后端 起两个程序8081,8083
    在这里插入图片描述

    负载均衡

    会将请求指向localhost:8081和localhost:8083两个服务。
    用于实现多个服务器实现负载均衡。
    两个服务器相同负载

    #负载均衡
    	upstream myserver {   
          server localhost:8081;
          server localhost:8083;
        }
        
        server {
            listen       9001;   #监听端口
            server_name  localhost;   #监听地址
       
            location  / {       
               root html;  #html目录
               index index.html index.htm;  #设置默认页
               proxy_pass  http://myserver;  #请求转向 myserver 定义的服务器列表      
            } 
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述
    在这里插入图片描述

    nginx 分配服务器策略

    默认情况下是轮询

    按权重设置

    #负载均衡
    	upstream myserver {   
          server localhost:8081 weight=1;
          server localhost:8083 weight=2;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    按ip

    按请求 ip 的 hash 值分配,每个访客固定访问一个后端服务器

    upstream myserver {   
    	  ip_hash;
          server localhost:8081;
          server localhost:8083;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    自动打开文件列表

    autoindex on;  #自动打开文件列表
    
    • 1
      location / {
               # root   html;
                root   E:\gzw\ROOT;
                index  index.html index.htm;
    			autoindex on;  #自动打开文件列表
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

  • 相关阅读:
    在R中使用python虚拟环境
    【环境检查】linux环境检查:内存剩余、磁盘读写性能、hosts、innode数量、CPU等
    jedis的minIdle和maxIdle参数
    SpringBoot集成MyBatis
    如何部署lvs负载均衡集群 DR模式
    浅谈Rob Pike的五条编程规范
    【Linux篇】第六篇——Linux下的进程(冯若依曼体系结构+操作系统+进程相关内容)
    卷积神经网络(CNN)识别眼睛状态
    一款不错的SpringCloud 脚手架项目
    MySQL SQL性能优化方案(SQL优化 二)
  • 原文地址:https://blog.csdn.net/qq_40711092/article/details/126444898
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号