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

时间:2023-02-18 16:51:44

安装

在 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