nginx reverse proxy multiple backends

后端 未结 2 1501
忘了有多久
忘了有多久 2020-11-30 21:48

Here is my situation: I will have one frontend server running nginx, and multiple backends servers running apache + passenger with different rails applications. I am NOT try

相关标签:
2条回答
  • 2020-11-30 22:01

    You can match the different URLs with server {} blocks, then inside each server block, you'd have the reverse proxy settings.

    Below, an illustration;

    server { 
      server_name client.domain.com;
    
      # app1 reverse proxy follow
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://x.x.x.100:80;
    
    }
    
    server { 
      server_name client2.domain.com;
    
      # app2 reverse proxy settings follow
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://x.x.x.101:80;
    }
    

    Also, you may add further Nginx settings (such as error_page and access_log) as desired in each server {} block.

    0 讨论(0)
  • 2020-11-30 22:09

    @mohamnag's comment is right. proxy_pass is only allowed inside a location

    See:

    http://wiki.nginx.org/HttpProxyModule#proxy_pass

    https://www.nginx.com/resources/admin-guide/reverse-proxy/

    So the correct config would be

    server { 
        server_name client.domain.com;
    
        location / {
            # app1 reverse proxy follow
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://x.x.x.100:80;
        }
    }
    
    server { 
        server_name client2.domain.com;
    
        location / {
            # app2 reverse proxy settings follow
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://x.x.x.101:80;
        }
    }
    
    0 讨论(0)
提交回复
热议问题