gpt4 book ai didi

nginx - 如果请求与路径不匹配,我如何让 Nginx 返回 444?

转载 作者:行者123 更新时间:2023-12-05 05:22:53 25 4
gpt4 key购买 nike

简短版:

我想使用 NGINX 作为反向代理,以便访问面向公众的 URL 的客户端从位于代理后面的内部 Gunicorn 服务器获取 API 数据:

external path (proxy) => internal app
<static IP>/ABC/data => 127.0.0.1:8001/data

我没有得到正确的位置映射。

长版:

我是第一次设置 NGINX,并试图将其用作 Gunicorn 提供的 rest api 的反向代理。 API 在 127.0.0.1:8001 提供。我可以从服务器访问它并获得适当的响应,所以我相信那部分工作正常。它使用 Supervisord 持续运行。

我想在 <static IP>/ABC/data 外部访问 API 端点之一.在 Gunicorn 服务器上,此端点位于 localhost:8001/data。 .最终我想通过 NGINX 为其他网络应用程序提供服务,其根类似于 <static IP>/foo , <static IP>/bar等。这些 Web 应用程序中的每一个都来自独立的 Python 应用程序。但是目前,当我尝试从外部访问端点时,会收到 444 错误代码,因此我认为我没有正确配置 NGINX。

我在 config posted on the Guincorn site 中对 NGINX 配置进行了首次尝试。 .我将其拆分为全局配置和站点特定配置,而不是单一配置。我的全局配置在 etc/nginx/nginx.conf看起来像:

user ops;
worker_processes 1;
pid /run/nginx.pid;
error_log /tmp/nginx.error.log;

events {
worker_connections 1024; # increase if you have lots of clients
accept_mutex off; # set to 'on' if nginx worker_processes > 1
use epoll;
# 'use epoll;' to enable for Linux 2.6+
# 'use kqueue;' to enable for FreeBSD, OSX
}

http {
include mime.types;
# fallback in case we can't determine a type
default_type application/octet-stream;
access_log /tmp/nginx.access.log combined;
sendfile on;

server_tokens off;

server {
# if no Host match, close the connection to prevent host spoofing
listen 80 default_server;
return 444;
}

gzip on;
gzip_disable "msie6";

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

然后我的站点特定配置在 /etc/nginx/sites-available 中(并且在 /etc/nginx/sites-enabled 中有符号链接(symbolic link))是:

upstream app_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response

# for UNIX domain socket setups
# server unix:/tmp/gunicorn_abc_api.sock fail_timeout=0;

# for a TCP configuration
server 127.0.0.1:8001 fail_timeout=0;
}

server {
# use 'listen 80 deferred;' for Linux
# use 'listen 80 accept_filter=httpready;' for FreeBSD
listen 80 deferred;
client_max_body_size 4G;

# set the correct host(s) for your site
server_name _;

keepalive_timeout 100;

# path for static files
#root /path/to/app/current/public;

location /ABC {
# checks for static file, if not found proxy to app
try_files $uri @proxy_to_app;
}

location @proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# enable this if and only if you use HTTPS
# proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;
proxy_pass http://app_server;
}

# error_page 500 502 503 504 /500.html;
# location = /500.html {
# root /path/to/app/current/public;
# }
}

配置通过 service nginx checkconfig ,但我最终在我的访问日志中看到以下内容:

XXX.XXX.X.XXX - - [09/Sep/2016:01:03:18 +0000] "GET /ABC/data HTTP/1.1" 444 0 "-" "python-requests/2.10.0"

我想我没有正确配置路由。如有任何建议,我们将不胜感激。

更新:

我现在可以使用它并进行一些更改。我注释掉了以下 block :

  server {
# if no Host match, close the connection to prevent host spoofing
listen 80 default_server;
return 444;
}

除非有有效路由,否则我不知道如何获得返回 444 的行为。我愿意,但我仍然停留在这部分。这个 block 似乎吃掉了所有传入的请求。我还将应用程序配置更改为:

upstream app_server {
server 127.0.0.1:8001 fail_timeout=0;
}

server {
# use 'listen 80 deferred;' for Linux
# use 'listen 80 accept_filter=httpready;' for FreeBSD
listen 80 deferred;
client_max_body_size 100M;

# set the correct host(s) for your site
server_name $hostname;

keepalive_timeout 100;

location /ABC {
# checks for static file, if not found proxy to app
try_files $uri @proxy_to_app;
}

location @proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# enable this if and only if you use HTTPS
# proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;
rewrite ^/ABC/(.*) /$1 break;
proxy_pass http://app_server;
}

}

基本上我似乎必须明确设置 server_name并使用 rewrite获取到应用服务器的正确映射。

最佳答案

这对我来说很好,只有在没有其他服务器名称匹配时才返回 444(挂断连接):

server {
listen 80;
server_name "";
return 444;
}

关于nginx - 如果请求与路径不匹配,我如何让 Nginx 返回 444?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39412753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com