【问题标题】:NGINX is forwarding HTTPS-traffic on some url's incorrectly to HTTP on port 443NGINX 将某些 url 上的 HTTPS 流量错误地转发到端口 443 上的 HTTP
【发布时间】:2020-07-22 13:01:35
【问题描述】:

这是一个 Docker 容器,NGINX 和 Jenkins 在同一个容器中,使用 supervisord 运行。 Docker 容器在 AWS ECS 中的 ELB 后面运行。

NGINX 应该将流量从 http://jenkins 转发到 https://jenkins。

流量会发生什么:

  • https://jenkins/computer/ --> 转到 https ✅

  • https://jenkins/computer --> 转到 http 和端口 443 ❌

配置:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/;
    index index.html index.htm;

    client_max_body_size 10M;

    server_name jenkins;
    ignore_invalid_headers    off;

    location / {
        allow vpnip/32;
        deny all;

        proxy_set_header        Host $host:$server_port;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto $scheme;

        # Fix the "It appears that your reverse proxy set up is broken" error.
        proxy_pass          http://127.0.0.1:8080;
        proxy_read_timeout  90;
        proxy_redirect      http://127.0.0.1:8080 https://jenkins;
        proxy_http_version 1.1;
        proxy_request_buffering off;
        proxy_buffering off; # Required for HTTP-based CLI to work over SSL
        if ($http_x_forwarded_proto != "https") {
            rewrite ^(.*)$ https://$server_name$1 permanent;
        }
    }

输出:

https://jenkins/computer
DNT: 1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-Dest: document
302 Found
Date: Tue, 21 Jul 2020 13:35:47 GMT
Location: http://jenkins:443/computer/
Server: nginx
X-Content-Type-Options: nosniff
Content-Length: 0
Connection: keep-alive

发生这种情况的原因可能是什么?

【问题讨论】:

    标签: nginx jenkins


    【解决方案1】:

    我在同一个容器中使用nginxpython/gunicon(生产Flask 服务),在我看来,您的配置比您需要的要复杂得多

    这就是我所拥有的

    http {
        .... [other stuff] ....
    
        upstream my_servers {
              server unix:/ram/gunicon_1.sock;
              server unix:/ram/gunicon_2.sock;
              }
    
        server {
            listen 800 ssl;
            server_name localhost;
    
            ssl_certificate      certkey.pem;
            ssl_certificate_key  certkey.pem;
    
            ssl_session_cache    shared:SSL:1m;
            ssl_session_timeout  5m;
    
            ssl_ciphers  HIGH:!aNULL:!MD5;
            ssl_prefer_server_ciphers  on;
    
    
            location / {
                proxy_pass http://my_servers;
            }
        }
    

    在此设置中,我在两个不同的 unix 套接字上运行 gunicorn 的两个实例,并让 nginx 在它们之间进行负载平衡(循环)。

    如果你在一个 unix 套接字上只有一个 Jenkins 实例,你可以

               proxy_pass http://unix:/ram/my_socket.sock;
    

    或者一个 IP 地址,无论你在什么上面运行 Jenkins。

    如果您有很多连接/断开连接周期,则 unix 套接字比 TCP 套接字更有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-03
      • 2019-04-19
      • 2017-09-02
      • 2019-02-02
      • 1970-01-01
      • 2018-01-25
      • 2021-09-17
      • 2021-01-20
      相关资源
      最近更新 更多