nginx 在两个不同的端口上接受 HTTP 和 HTTPS 请求

baz*_*ire 5 nginx https

我有一个 nginx 服务器设置(两个配置文件),其中有两个 gunicorn Web 服务器设置和运行。一个 gunicorn 是生产,另一个是登台。

我希望 nginx 向 xyz.com 提供 http 请求以及向 xyz.com 提供 https 请求到生产 gunicorn 服务器 @ 127.0.0.1:8000。

我已经完成了这个:

server {
   listen 80;
   server_name xyz.com;
   return 301 https://$http_host$request_uri;
}

server {
   listen 443 ssl;
   server xyz.com;
   ..... <<< ssl stuff
  location /{
      .... proxy_stuff
      proxy_pass http://127.0.0.1:8000;
  }
}
Run Code Online (Sandbox Code Playgroud)

我还希望到 xyz.com:8080 的 http 流量和到 xyz.com:8080 的 https 流量到达临时服务器 @ 127.0.0.1:8081。我已经能够获得 https 流量到 xyz.com:8080 工作如下:

server {
   listen 8080 ssl;
   server_name xyz.com;
   ...... << ssl stuff
   location / {
      ...... << proxy stuff
      proxy_pass http://127.0.0.1:8081;
   }
}
Run Code Online (Sandbox Code Playgroud)

但我似乎找不到一种方法将 xyz.com:8080 上的 http 流量重定向到 xyz.com:8080 上的 https 流量。我尝试了与使用端口 80 进行的重定向相同的重定向,但没有成功。

可以使用一些方向。

Tim*_*Tim 8

根据您所说的,您想在端口 8080 上侦听 http 和 https,我认为这是不可能的。为不同的端口设置不同的服务器块,使用里面的位置块,您可以将相同的 proxy_pass 传递到您喜欢的任何地方。

这可能是最接近您所说的内容,即侦听 8080 http、8081 https 并从 http 转发到 https。重写可能不完全正确,但你明白了。

server {
  listen 8080; # HTTP
  server_name example.com;
  rewrite ^ https://example.com:8081$request_uri? redirect;
  # rewrite ^ https://example.com:8081 redirect; # Alternate rewrite
}

server {
  listen 8081 ssl;
  server_name example.com;
  // ...... << ssl stuff
  location / {
    // ...... << proxy stuff to forward to http
    proxy_pass http://127.0.0.1:8080;
    // If you are not proxying to a service on the same server you can use the line below
    // proxy_pass http://example.com:8080; 
  }
}
Run Code Online (Sandbox Code Playgroud)