nginx反向代理到在本地主机上运行的后端 [英] nginx reverse proxy to backend running on localhost

查看:91
本文介绍了nginx反向代理到在本地主机上运行的后端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:事实证明,我下面的设置确实有效.以前,我会重定向到端口 36000,但这是由于我的后端应用程序上的某些配置设置导致的.

EDIT: It turns out that the my setup below actually works. Previously, I was getting redirections to port 36000 but it was due to some configuration settings on my backend application that was causing it.

我不完全确定,但我相信我可能想使用 nginx 设置反向代理.

I am not entirely sure, but I believe I might be wanting to set up a reverse proxy using nginx.

我有一个应用程序在端口 36000 的服务器上运行.默认情况下,端口 36000 不可​​公开访问,我的目的是让 nginx 侦听公共 url,将对该 url 的任何请求定向到在端口 36000 上运行的应用程序. 在这整个过程中,用户不应该知道他/她的请求正在发送到运行在我服务器 36000 端口上的应用程序.

I have an application running on a server at port 36000. By default, port 36000 is not publicly accessible and my intention is for nginx to listen to a public url, direct any request to the url to an application running on port 36000. During this entire process, the user should not know that his/her request is being sent to an application running on my server's port 36000.

更具体地说,假设我的网址是 http://domain.somehost.com/

To put it in more concrete terms, assume that my url is http://domain.somehost.com/

在访问 http://domain.somehost.com/ 后,nginx 应该接收请求并将其重定向到一个应用程序在端口 36000 上的服务器上运行,应用程序进行一些处理,并将响应传回.端口 36000 不可​​公开访问,不应作为任何网址的一部分出现.

Upon visiting http://domain.somehost.com/ , nginx should pick up the request and redirect it to an application already running on the server on port 36000, the application does some processing, and passes the response back. Port 36000 is not publicly accessible and should not appear as part of any url.

我尝试了如下设置:

server {
    listen 80;
    server_name domain.somehost.com
    location / {
        proxy_pass http://127.0.0.1:36000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

并将其包含在我的主要 nginx.conf 中

and including that inside my main nginx.conf

但是,它要求我将端口 36000 设为可公开访问,我正在努力避免这种情况.端口 36000 也显示为 Web 浏览器中转发 URL 的一部分.

However, it requires me to make port 36000 publicly accessible, and I'm trying to avoid that. The port 36000 also shows up as part of the forwarded url in the web browser.

有没有什么办法可以做同样的事情,但不让端口 36000 可访问?

Is there any way that I can do the same thing, but without making port 36000 accessible?

谢谢.

推荐答案

下面的配置来自一个有效的 nginx 配置,主机名和端口已更改.

The config below is from a working nginx config, with the hostname and port changed.

您可能需要将侦听端口 36000 的服务器设置为 upstream 服务器(请参阅 http://nginx.org/en/docs/http/ngx_http_upstream_module.html).

You need to may be able to set the server listening on port 36000 as an upstream server (see http://nginx.org/en/docs/http/ngx_http_upstream_module.html).

server {
        listen   80;
        server_name domain.somehost.com;

        location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $host;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://localhost:36000/;
                proxy_redirect http://localhost:36000/ https://$server_name/;
        }
}

这篇关于nginx反向代理到在本地主机上运行的后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆