2011-03-25 18 views
29

Tôi có 2 máy chủ,Proxy nginx mọi hoạt động đến nginx từ xa

  1. với xx.xx.xx.xx IP, nằm ở Đức ... (chạy frontend: nginx (nội dung tĩnh), phụ trợ: apache2)

  2. với yy.yy.yy.yy IP, nằm ở Ý ...

Mọi yêu cầu tại thời điểm này được gửi đến máy chủ với xx.xx.xx.xx IP, Tôi làm cách nào để ủy quyền ll giao thông từ xx.xx.xx.xx để yy.yy.yy.yy sử dụng nginx ...

  request       proxy, request 
Internet  ->  xx.xx.xx.xx(nginx)   ->    yy.yy.yy.yy(nginx, Apache) 
      <-         <- 
      response       proxy, response 

Thanks ...

+0

Bạn có thử đọc tài liệu? proxy_pass _http: //yy.yy.yy.yy – CyberDem0n

+0

chắc chắn! nhưng làm thế nào để đọc X-Real-IP ở phía xa (ip: yy.yy.yy.yy) ?? – user676674

+0

Cảm ơn, TẤT CẢ! – user676674

Trả lời

80

Đối với những người khác. Câu trả lời cho chủ đề là cấu hình nginx như:

server { 
    listen 80; 
    server_name mydomain.com; 
    location/{ 
     access_log off; 
     proxy_pass http://mydomain.com:8080; 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header Host $host; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    } 
} 
+0

'Vị trí /' có khớp với tất cả các đường dẫn không? hoặc chỉ đường dẫn gốc '\'? – pomo

+1

@pomo theo hướng dẫn tại đây http://nginx.org/en/docs/http/ngx_http_core_module.html#location nó sẽ khớp với tất cả các truy vấn lồng nhau như site.com/index.html nhưng không phù hợp với truy vấn gốc aka site.com/ –

1

Bạn có thể sử dụng upsteream như:

upstream xx.xx.xx.xx:8080{ 
    #ip_hash; 
    server xx.xx.xx.xx:8080 max_fails=2 fail_timeout=2s; 
    server yy.yy.yy.yy:8181 max_fails=2 fail_timeout=2s; 
} 

sau đó bạn có thể sử dụng cookie hoặc tiêu đề để thiết lập các yêu cầu như:

location /app { 
     if ($cookie_proxy_override = "proxy-target-A") { 
      rewrite . http://xx.xx.xx.xx:8080/app; 
      proxy_set_header X-Real-IP  $remote_addr; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      break; 
     } 
     if ($cookie_proxy_override = "proxy-target-B") { 
      rewrite . http://yy.yy.yy.yy:8181/webreg; 
      proxy_set_header X-Real-IP  $remote_addr; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      break; 
     } 
     proxy_pass http://xx.xx.xx.xx:8080/webreg; 
     proxy_set_header X-Real-IP  $remote_addr; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    }