【问题标题】:How to enable reverse proxy but block direct access to port 8080 in Nginx and CherryPy?如何启用反向代理但阻止直接访问 Nginx 和 CherryPy 中的 8080 端口?
【发布时间】:2015-04-10 23:01:00
【问题描述】:

我在我的 Linux 机器上运行 Nginx 和 uwsgi,但我必须将我的代码移植到 Windows。所以我选择了 CherryPy 来重写我的代码,但是我遇到了问题,因为我不知道如何阻止直接访问 CherryPy 端口 8080 并且仍然在 Nginx 中启用反向代理。

这是我的配置

nginx:

upstream apps {
    server 127.0.0.1:8080;
}

server {
    listen 80;
    listen [::]:80;

    root /var/www/html;

    index index.htm index.html index.nginx.html;

    server_name localhost;

    merge_slashes off;

    large_client_header_buffers 4 64k;

    location / {

        allow 127.0.0.1;
        deny all;
        try_files $uri $uri/ =404;

        proxy_pass        http://apps;
        proxy_redirect    off;
        proxy_set_header  Host             $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    }

}

这是我的 Cherrypy 配置:

_config = {
    'global' : {
        # http server
        'server.socket_host' : "127.0.0.1",
        'server.socket_port' : 8080,
        'server.thread_pool' : 8,
        # file change reload
        'engine.autoreload_on' : False,
        # url trailing slash
        'tools.trailing_slash.on' : False,
        # logging
        'log.access_file' : os.path.join(_path, "variable/log/access_back.log"), 
        'log.error_file'  : os.path.join(_path, "variable/log/error_back.log"),
    },
    '/' : {
        'tools.encode.encoding' : "utf-8"
    }
}

编辑:

我发现我在启用站点时链接了错误的 neginx 配置,这就是为什么我可以从我的局域网访问 8080。我的大坏处:(在我纠正了这个之后,这两种配置都对我有用,所以我从一开始就得到了答案。

【问题讨论】:

  • 将主机设置为127.0.0.1 应该使它只能从本地主机访问。您是否尝试过从远程机器访问端口?

标签: python nginx reverse-proxy cherrypy


【解决方案1】:

'server.socket_host' : "127.0.0.1", 表示 CherryPy 已经只侦听您的环回接口,不会在其他接口上提供连接。换句话说,它只能从运行它的机器的 8080 端口访问。

【讨论】:

  • ITnx saaj,我已经使用了你的 webapp 中的配置。我很遗憾地说我在我的 nginx 中启用的站点中链接了错误的配置文件,这就是为什么我可以直接从局域网上的其他计算机访问 8080 的原因。我的错。
  • @DamirPrebeg 是的,我注意到 cmets 看起来很眼熟 :-) 没问题。
猜你喜欢
  • 2021-05-13
  • 1970-01-01
  • 2015-10-16
  • 1970-01-01
  • 1970-01-01
  • 2020-05-14
  • 2020-08-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多