python - 如何在 Nginx 和 CherryPy 中启用反向代理但阻止直接访问端口 8080?

标签 python nginx reverse-proxy cherrypy

我在我的 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。我的大坏处:(在我纠正了这个问题之后,这两种配置都对我有用,所以我从一开始就得到了答案。

最佳答案

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

关于python - 如何在 Nginx 和 CherryPy 中启用反向代理但阻止直接访问端口 8080?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29558544/

相关文章:

api - 提供 API 的公司是否在其 API 之前使用填充程序或代理?

docker - 如何将相同的nginx.conf文件用于docker-compose和kubernetes的反向代理

python - Cython 定义 void 函数基础 c++ 头文件

django - 如何在 Digital Ocean 的源代码管理中包含 nginx 和 gunicorn 配置文件?

python - 使用用户输入调用特定的 python 类

nginx - nginx如何实现底层服务器宕机时出现的智能维护页面?

linux - nginx负载均衡器打开的文件太多

nginx - nginx 反向代理背后的服务器忽略 URL 中的相对路径

python - 使用索引值作为 pandas 数据框中的类别值

python - 如何在Python中的for循环 block 之外使用 "increment"变量?