在nginx的proxy_pass字段中指定实际的内部IP是否正确?

我现在要通过nginx在Google Compute Engine上部署我的node.js应用程序,但在完成所有configuration之后,我用“502 Bad Gateway”(502错误网关)错误触发了墙。

然后,在挖掘了这么多小时之后,我终于发现/etc/nginx/sites-available/server.conf中的proxy_pass字段应该是您在GCE控制台中显示的实际内部IP地址,而不是http://127.0.0.1 。 conf文件如下(有些值只是为了说明的目的):

 server { listen 80; listen 443 ssl; server_name example.com; ssl_certificate /etc/nginx/ssl/example.crt; ssl_certificate_key /etc/nginx/ssl/example.key; location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-Proto $scheme; proxy_pass http://10.102.24.152:8888; } } 

但是,我所遵循的所有教程都将proxy_pass的值设置为http://127.0.0.1 。 但在我的情况下,根本没有工作。 教程的例子如下:

  • https://github.com/ShoppinPal/gcp-scripts/wiki/Setup-Nginx-on-Google-Cloud
  • http://support.ghost.org/setup-ssl-self-hosted-ghost/
  • https://www.digitalocean.com/community/questions/problem-with-ssl-and-ghost

所以我现在想知道是否是使服务器正常工作的正确方法。 或者这是否导致安全问题容易发生? 或者,也许127.0.0.1是正确的数字,我身边还有其他问题?

是的,放入实例IP地址是一个合理的事情。

您应该小心不要使用公共IP,因为您可能会收取带宽费用。 我对GCE不熟悉,但在AWS中,您有一个可在Internet上路由的外部IP,以及您networking中的内部IP。 AWS和GCE之间可能存在差异,有人可能会纠正我。