如何在443上使nginx透传并将80重定向到443?

How to make nginx passthrough on 443 and redirect 80 to 443?

我有一个 winstone 服务器 (Jenkins) 监听 8443。 Jenkins 有一个有效的证书,并且 Jenkins 正在成功终止证书:

JENKINS_ARGS="--httpPort=-1 --httpsKeyStore=/secure/jenkins.keystore --httpsKeyStorePassword=MY_PASSWORD --httpsPort=8443"

唯一的问题是用户现在必须去: https://example.com:8443

我不想要 URL 中的那个端口号。 我要:

https://example.com:8443 -> https://example.com
https://example.com      -> https://example.com
http://example.com       -> https://example.com

所以我想我会 运行 nginx 在与 运行ning Jenkins 相同的实例上。

所以我的问题是:

  1. 我是否必须重新配置 jenkins 以不执行证书终止,以便 nginx 只执行它?
  2. nginx 是否可以在没有证书的情况下将 80 和 443 重定向到 localhost:8443(因为 Jenkins 正在执行证书终止)?
  3. nginx 和 Jenkins 都需要终止证书吗?

抱歉提出类似问题。

我很确定 AWS ELB 不能取代 nginx 在这里做的事情,但我想我会把它扔在那里,以防 ELB 也能为我解决这个问题。

1) 不,您可以使用 Stream Module 让 Nginx Stream 将连接直接连接到 Jenkins。

请注意,这是在 1.9.0 中添加的,但不是默认构建的一部分,因此您可能需要自己 构建

它的工作原理很像 http server 块,但您必须在 http 块之外设置它。

stream {
    upstream jenkins_server {
        server jenkins:443;
    }

    server {
        listen 443;
        proxy_pass jenkins_server;
    }
}

2) 你不需要 nginx 上的证书,但你应该有一个 http 端口 80 的服务器块,它执行第 1 部分中提到的 301 到 443 流。

server {
    listen 80;
    server_name your_server_name_here;
    return 301 https://$host$request_uri;
}

3) 不,你不需要,因为你可以使用 nginx 流将 ssl 从客户端传递到 Jenkins 服务器。