如何将DEVPI与https一起服务



我在http://

上运行了一个开箱即用的DevPi-Server

我需要让它在https://上工作。

我已经有域的证书。

我遵循了nginx-site-config的文档,并创建了具有指向我证书的server{}块的/etc/nginx/conf.d/domain.conf文件(下面摘录(。

但是,我的devpi-server --start --init完全忽略了任何/所有NGINX配置。如何将DEVPI服务器指向使用NGINX配置?是否有可能,还是我完全错过了重点?

/etc/nginx/conf.d/domain.conf文件内容:

server {
    server_name localhost $hostname "";
    listen              8081 ssl default_server;
    listen              [::]:8081 ssl default_server;
    server_name         domain;
    ssl_certificate     /root/certs/domain/domain.crt;
    ssl_certificate_key /root/certs/domain/domain.key;
    ssl_protocols       TLSv1.1 TLSv1.2;
    ssl_ciphers         EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;
    gzip             on;
    gzip_min_length  2000;
    gzip_proxied     any;
    gzip_types       application/json;
    proxy_read_timeout 60s;
    client_max_body_size 64M;
    # set to where your devpi-server state is on the filesystem
    root /root/.devpi/server;
    # try serving static files directly
    location ~ /+f/ {
        # workaround to pass non-GET/HEAD requests through to the named location below
        error_page 418 = @proxy_to_app;
        if ($request_method !~ (GET)|(HEAD)) {
            return 418;
        }
        expires max;
        try_files /+files$uri @proxy_to_app;
    }
    # try serving docs directly
    location ~ /+doc/ {
        try_files $uri @proxy_to_app;
    }
    location / {
        # workaround to pass all requests to / through to the named location below
        error_page 418 = @proxy_to_app;
        return 418;
    }
    location @proxy_to_app {
        proxy_pass https://localhost:8081;
        proxy_set_header X-outside-url $scheme://$host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
    }
}

这是我对Superuser上相同问题的答案。

devpi对NGINX一无所知,它将仅服务于HTTP流量。当我们想通过HTTPS与Web应用程序进行交互时,我们作为客户需要与可以处理它(NGINX(的前端进行交谈,该前端将与我们的Web应用程序进行通信。NGINX的这种应用称为反向代理。作为反向代理,我们还可以从NGINX提供静态文件的能力,而不是获得我们的Web应用程序本身进行静态文件的能力(因此,"尝试使用..." 位置块(。p>这是我用于DEVPI的完整工作NGINX配置。请注意,这是/etc/nginx/nginx.conf文件,而不是像您这样的域配置,因为我在Docker中运行了Nginx和DevPi,但您应该能够拔出所需的内容:

worker_processes 1;
events { 
    worker_connections 1024; 
}
http {
    # Define the location for devpi
    upstream pypi-backend {
        server localhost:8080;
    }
    # Redirect HTTP to HTTPS
    server {
        listen 80;
        listen [::]:80;
        server_name _;
        return 301 https://$host$request_uri;
    }
    server {
        listen 443 ssl;
        server_name example.co.uk; # This is the accessing address eg. https://example.co.uk
        root /devpi/server; # This is where your devpi server directory is
        gzip             on;
        gzip_min_length  2000;
        gzip_proxied     any;
        proxy_read_timeout 60s;
        client_max_body_size 64M;
        ssl_certificate             /etc/nginx/certs/cert.crt; Path to certificate
        ssl_certificate_key         /etc/nginx/certs/cert.key; Path to certificate key
        ssl_session_cache           builtin:1000  shared:SSL:10m;
        ssl_protocols               TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers                 HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
        ssl_prefer_server_ciphers   on;
        access_log                  /var/log/nginx/pypi.access.log;
        # try serving static files directly
        location ~ /+f/ {
            error_page 418 = @pypi_backend;
            if ($request_method !~ (GET)|(HEAD)) {
                return 418;
            }
            expires max;
            try_files /+files$uri @pypi_backend;
        }
        # try serving docs directly
        location ~ /+doc/ {
            try_files $uri @pypi_backend;
        }
        location / {
            error_page 418 = @pypi_backend;
            return 418;
        }
        location @pypi_backend {
            proxy_pass              http://pypi-backend; # Using the upstream definition
            proxy_redirect          off;
            proxy_set_header        Host $host;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-outside-url $scheme://$host:$server_port;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        X-Forwarded-Host $server_name;
        }
    }
}

使用此配置和在http://localhost:8080上运行的DEVPI使用NGINX,您应该能够使用适当的DNS https://example.co.uk访问https://localhost或与计算机访问。请求将是:

client (HTTPS) > Nginx (HTTP) > devpi (HTTP) > Nginx (HTTPS) > client

这也意味着您需要确保Nginx正在运行自己,因为DevPi Start不会更好。您至少应该查看nginx欢迎页面。

最新更新