简体   繁体   English

配置 NGINX 以将 HTTPS 请求代理到服务器 HTTP

[英]Configure NGINX to proxy HTTPS requests to a server HTTP

I'm tring to use POSTGRest API to deal with some queries in a Postgrsql server, but the API doesn't accept HTTPS request as stated in the API documentation:我正在尝试使用 POSTGRest API 来处理 Postgrsql 服务器中的一些查询,但 API 不接受 HTTPS 请求,如 API 文档中所述:

PostgREST aims to do one thing well: add an HTTP interface to a PostgreSQL database. PostgREST 旨在做好一件事:向 PostgreSQL 数据库添加 HTTP 接口。 To keep the code small and focused we do not implement HTTPS.为了保持代码小而集中,我们没有实现 HTTPS。 Use a reverse proxy such as NGINX to add this, here's how.使用诸如 NGINX 之类的反向代理来添加它,方法如下。

Our nginx server is able to deal with HTTPS requests, but I was unable to find a nginx configuration example that can proxy those HTTPS requests and pass them to POSTGRest API as an HTTP request in order to get the json file from above mentioned API.我们的 nginx 服务器能够处理 HTTPS 请求,但我找不到一个 nginx 配置示例可以代理这些 HTTPS 请求并将它们作为 HTTP 请求传递给 POSTGRest API,以便从上述 API 获取 json 文件。

Here what I have tried so far:到目前为止,我已经尝试过:

server {
        listen 443 ssl http2;

        location = /api {
            return 301 http://$host/$request_uri;
        }
} ```

Any help would be amazing!

There's a minimal example in the docs on how to use PostgREST with Nginx in the Hardening PostgREST section .强化 PostgREST 部分中,文档中有一个关于如何将 PostgREST 与 Nginx 结合使用的最小示例。 You can adapt it to HTTPS by adding that location section to the server configured for HTTPS, like this:您可以通过将该位置部分添加到为 HTTPS 配置的服务器来使其适应 HTTPS,如下所示:

http {
  
  # ...

  # upstream configuration
  upstream postgrest {
    server localhost:3000;
  }

  # ...

  server {

    listen 443 ssl http2;

    # Other configuration for HTTPS (certs, etc.)
    # ...

    # expose to the outside world
    location /api/ {
      default_type  application/json;
      proxy_hide_header Content-Location;
      add_header Content-Location  /api/$upstream_http_content_location;
      proxy_set_header  Connection "";
      proxy_http_version 1.1;
      proxy_pass http://postgrest/;
    }

    # ...

  }

}

For clarity, the comments with #... reference any other code you may already have in your configuration.为清楚起见,带有#...的注释引用了您的配置中可能已有的任何其他代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM