有时候,我们会遇到一个服务器要部署多个服务,但是对外却只有一个端口,如果这些都是运行在同个服务器容器的程序,如tomcat里多个java web应用,那么是没什么问题的。
但是,如果有的是java应用,有的是node应用,有的是其他的,那我们可以使用nginx来搭建一个简单的反向代理服务。
安装
- http://nginx.org/en/download.html 选择相应的包下载
- 编译
/configure --help查询详细参数(参考本教程附录部分:nginx编译参数)
拷贝以下命令执行:作用是参数设置,设置为自己想要存放的目录即可
./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
# 若嫌弃命令行太长,那就不执行命令行执行该命令
./configure --prefix=/usr/local/nginx
make
make install
配置
修改conf/nginx.conf
其中主要需要修改是 location ...这部分
如下配置设置了将/test1指向了localhost:8082,则该端点下的所有请求都会被转发至localhost:8082
proxy_set_header作用: 由于nginx代理了请求,所以localhost:8082服务收到的请求方IP事实上是nginx所在的ip地址,所以需要通过这个配置将真实的ip地址存放到header中的X-Forwarded-For上
client_max_body_size 100M的作用在于配置允许单一请求最大为100MB,默认是1MB
server {
listen 80;
server_name localhost;
client_max_body_size 100M;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.html;
}
location /test1 {
proxy_pass http://localhost:8082;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /test2 {
proxy_pass http://localhost:8080;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#....
}
启动
nginx目录下运行
./sbin/nginx -c ./conf/nginx.conf
3万+

被折叠的 条评论
为什么被折叠?



