nginx 安装与反向代理测试 under MAC

安装

在 Mac 下可以直接使用 homebrew 安装 nginx

brew search nginx

brew install nginx

启动 nginx: sudo nginx,访问 8080 应能看到欢迎界面

nginx -V 查看 nginx 的启动参数,配置文件的位置默认是 

--conf-path=/usr/local/etc/nginx/nginx.conf 
 
#重新加载配置|重启|停止|退出 nginx
nginx -s reload|reopen|stop|quit 
 

反向代理 

 
假设希望别人输入 localhost:8080 时直接跳转到 localhost:2014,那么在 nginx.conf 中配置
 
http {
  server {
    listen 8080;

    location / {
      proxy_pass http://127.0.0.1:2014;
    }
  }
}

 

负载均衡加反向代理

myapp1 是一组网页的集合,这里作为一个变量

http {
    upstream myapp1 {
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

    server {
        listen 8080;

        location / {
            proxy_pass http://myapp1;
        }
    }
}

  

注意:

1. location 后面的东西是一个 uri 地址,/ 的话表示根目录的请求直接跳转到 myapp1 上,当然 uri 还可以更长一些,比如 /name 之类的,具体怎么替换还要结合 proxy_pass 来处理,这里分为两种情况

If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive:

location /name/ {
    proxy_pass http://127.0.0.1/remote/;
}

If proxy_pass is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed, or the full normalized request URI is passed when processing the changed URI:

location /some/path/ {
    proxy_pass http://127.0.0.1;
}

 

参考

1. http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

2. https://www.nginx.com/blog/load-balancing-with-nginx-plus-part2/?_ga=1.269115441.1947437472.1438069067

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值