nginx 二次代理转发

一、背景
近期部署项目时遇到一个问题,由阿里云解析到一个公网iP,由该公网ip的nginx进行转发。但转发时,该项目为前后端分离,需要解析到远程服务器的xxx目录,这通过一个nginx转发不好解决。通过第一个nginx转发到远程服务器的nginx端口,由远程服务器的nginx进行本地转发即可。
二、步骤
1.比如远程服务器内网ip为192.168.10.11,ip公网ip服务器的nginx转发到192.168.10.11:的80端口(nginx端口)。

upstream config_upstream{
  server 192.168.10.11:80   max_fails=3 fail_timeout=3s weight=10;
}

server {
        listen       80;
        server_name  config.xxxx.com;

          location / {
                        proxy_next_upstream error timeout invalid_header http_500 http_503;
                        proxy_pass  http://config_upstream;
                        proxy_set_header X-Forwarded-Proto https;
                        proxy_set_header   Host             $host;
                        proxy_set_header   X-Real-IP        $remote_addr;
                        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_redirect     off;
                        proxy_connect_timeout      300;
                        proxy_send_timeout         300;
                        proxy_read_timeout         300;
                        #proxy_send_lowat          12000;
                        proxy_buffer_size          128k;
                        proxy_buffers              8 64k;
                        proxy_busy_buffers_size    128k;
                        proxy_temp_file_write_size 128k;
                }
    }

 

2.到远程服务器192.168.10.11上安装nginx进行转发。

upstream config_upstream{
  server 127.0.0.1:9990   max_fails=3 fail_timeout=3s weight=10;
}

server {
        listen       80;
        server_name  config.xxxx.com;


          location / {
                 root /data2/java/deploy/web/disconf/html;
                 if ($query_string) {
                         expires max;
                 }
            }
          location ~ ^/(api|export) {
                        proxy_next_upstream error timeout invalid_header http_500 http_503;
                        proxy_pass  http://config_upstream;
                        proxy_set_header X-Forwarded-Proto https;
                        proxy_set_header   Host             $host;
                        proxy_set_header   X-Real-IP        $remote_addr;
                        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_redirect     off;
                        proxy_connect_timeout      300;
                        proxy_send_timeout         300;
                        proxy_read_timeout         300;
                        #proxy_send_lowat          12000;
                        proxy_buffer_size          128k;
                        proxy_buffers              8 64k;
                        proxy_busy_buffers_size    128k;
                        proxy_temp_file_write_size 128k;
                }
}


原文链接:https://blog.csdn.net/qq_33633013/article/details/102958596

posted @ 2021-10-28 12:01  向往明天-tsingyun  阅读(4393)  评论(0编辑  收藏  举报