mac安装Nginx&Nginx常见的命令&Nginx反向代理、负载均衡

1.Mac安装Nginx
brew install nginx
2.查看nginx相关的目录
brew info nginx
查看的结果如下
nginx: stable 1.23.1 (bottled), HEAD
HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server
https://nginx.org/
/opt/homebrew/Cellar/nginx/1.23.1 (26 files, 2.2MB) *
  Poured from bottle on 2022-08-22 at 15:35:18
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/nginx.rb
License: BSD-2-Clause
==> Dependencies
Required: openssl@1.1 ✔, pcre2 ✔
==> Options
--HEAD
	Install HEAD version
==> Caveats
Docroot is: /opt/homebrew/var/www

The default port has been set in /opt/homebrew/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /opt/homebrew/etc/nginx/servers/.

目录信息如下:
Docroot is: /opt/homebrew/var/www
conf:/opt/homebrew/etc/nginx/nginx.conf
load:/opt/homebrew/etc/nginx/servers/
3.启动nginx&停止nginx
启动:
nginx
停止
nginx -s stop
4.验证是否启动成功
localhost:8080(我是修改成了80端口)
5.修改了conf配置文件 需要重新加载(要在启动状态下,直接重新加载命令会出错)
nginx  启动
nginx -s reload 重新加载配置文件

反向代理&负载均衡

反向代理
  • 正向代理:A到C(知道目的地),但路是不通的,通过B搭桥使路联通,最终使A成功到达C,这是正向代理
  • 反向代理:A到B,B又将A带领到最终真正的目的地C,实现A到C,A不知道自己的目的地是C,这就是反向代理
  • 负载均衡:上面的反向代理,不是仅仅通过B将A带领到C,可能是是B1,B2,B3…,通过不同的B带领到C,这是负载均衡
反向代理配置(在nginx配置文件中)

访问localhost:82 会去访问 IP地址:端口

server {
    listen 82;
    server_name localhost;
    location / {
        proxy_pass http://IP地址:端口; 	#反向代理配置,将请求转发到指定服务
    }
}
负载均衡配置

访问localhost:82 会去访问 IP地址1:端口1 和 IP地址2:端口2
targetserver 是自定义的

#upstream指令可以定义一组服务器
upstream targetserver{	
    server IP地址1:端口1 weight=10; # 权重的分配的请求概率大
    server IP地址2:端口2 weight=5;
}

server {
    listen    82;
    server_name  localhost;
    location / {
        proxy_pass http://targetserver;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值