1.Linux服务器安装Nginx
docker安装请移步:Linux使用docker安装Nginx
使用yum安装:
wget https://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx
#安装c的编译环境
yum -y install gcc gcc-c++ autoconf automake make
yum -y install pcre pcre-devel
2.修改nginx.conf文件,配置多个地址的反向代理
nginx安装目录在 /etc/nginx 位置
cd /etc/nginx
修改nginx.conf 目录
upstream backend_servers {
server your_rpc1:8887;
server your_rpc2:8888;
}
......
server {
listen 18888;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /link/app {
proxy_pass http://backend_servers/app;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
nginx.conf 内文件格式具有严格的限制,location内只能有 一个proxy_pass , 因此需要在外部定义
upstream backend_servers {
server your_rpc1:8887;
server your_rpc2:8888;
}
还是很简单的,并且不会让前端请求地址改来改去了(前端不会说我了)
3.解决连续两次调用配置的反向代理地址,一次正确,一次404的问题
在上述的配置基础上,添加
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
proxy_connect_timeout 5s;
proxy_read_timeout 30s;
proxy_http_version 1.1;
proxy_set_header Connection "";
如果是发生404,或者其它错误,让它尝试下一个地址.设置一下最大时间.
location /link/app {
proxy_pass http://backend_servers/app;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
proxy_connect_timeout 5s;
proxy_read_timeout 30s;
proxy_http_version 1.1;
proxy_set_header Connection "";
}