测试nginx反向代理

编译和安装Tengine

1
2
3
./configure
make
sudo make install

Tengine默认将安装在/usr/local/nginx目录。你可以用’–prefix’来指定你想要的安装目录。

configure脚本的选项

大部分的选项跟Nginx是兼容的。下面列出的都是Tengine特有的选项。如果你想查看Tengine支持的所有选项,你可以运行’./configure –help’命令来获取帮助。
–dso-path
设置DSO模块的安装路径。
–dso-tool-path
设置dso_tool脚本本身的安装路径。
–without-dso
关闭动态加载模块的功能。
–with-jemalloc
让Tengine链接jemalloc库,运行时用jemalloc来分配和释放内存。
–with-jemalloc=path
设置jemalloc库的源代码路径,Tengine可以静态编译和链接该库。

make的目标选项

大部分的目标选项跟Nginx是兼容的。下面列出的是Tengine特有的选项。
make test
运行Tengine的测试用例。你首先需要安装perl来运行这个指令。
make dso_install
将动态模块的so文件拷贝到目标目录。这个目录可以通过’–dso-path’设置。默认是在Tengine安装目录下面的modules目录。

简单操作

启动 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 中配置

1
2
3
4
5
6
7
8
9
http {
server {
listen 8080;
location / {
proxy_pass http://127.0.0.1:2014;
}
}
}

负载均衡加反向代理

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
http {
upstream myapp1 {
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 8080;
location / {
proxy_pass http://myapp1;
}
}
}

注意:

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;
}

参考

nginx 安装与反向代理测试 under MAC - SangS - 博客园