首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Dockerized nginx在ssl模式下失败

问Dockerized nginx在ssl模式下失败
EN

Stack Overflow用户
提问于 2020-11-26 19:05:17
回答 1查看 21关注 0票数 0

我在docker上使用nginx:https://hub.docker.com/_/nginx

我的Dockerfile,在images/nginx目录下:

代码语言:javascript
复制
FROM nginx
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY self-signed.crt /etc/ssl/private
COPY self-signed.key /etc/ssl/private

我的docker-compose.yml

代码语言:javascript
复制
version: '3.5'
services:

  nginx:
    build: images/nginx
    restart: on-failure
    ports:
      - 9080:80

这是我的配置:

代码语言:javascript
复制
server {
    listen 443 ssl;
    # listen 80;  <---- This works!

    server_name localhost;

    ssl_certificate /etc/ssl/private/self-signed.crt;
    ssl_certificate_key /etc/ssl/private/self-signed.key;

    location /api/ {
        set                $gateway             api:1234;
        include            uwsgi_params;
        uwsgi_pass         $gateway;

        proxy_set_header   Host                 $http_host;
        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;
    }

}

这是我的nginx

代码语言:javascript
复制
root@68f9026bb4fd:/# nginx -V
nginx version: nginx/1.19.4
built by gcc 8.3.0 (Debian 8.3.0-6) 
built with OpenSSL 1.1.1d  10 Sep 2019
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.19.4/debian/debuild-base/nginx-1.19.4=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'

在listen 80处于活动状态的情况下使用http版本时,它可以工作:

代码语言:javascript
复制
» http --headers http://localhost:9080/api/status
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 21
Content-Type: application/json
Date: Thu, 26 Nov 2020 10:55:11 GMT
Server: nginx/1.19.4

ngnix容器也有日志条目:

代码语言:javascript
复制
nginx_1     | 172.24.0.1 - - [26/Nov/2020:10:55:11 +0000] "GET /api/status HTTP/1.1" 200 21 "-" "HTTPie/2.2.0" "-"

但是,如果我现在启用listen 443 ssl指令,并禁用listen 80,则什么也不能用:

代码语言:javascript
复制
» http --headers --verify no https://localhost:9080/api/status

http: error: ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer')) while doing a GET request to URL: https://localhost:9080/api/status

并且nginx容器绝对没有日志条目。没什么。什么都没有!

我的证书/密钥在那里,具有正确的权限(我想):

代码语言:javascript
复制
root@805e850405b1:/# ls -lrt /etc/ssl/private/
total 8
-rw------- 1 root root 3272 Nov 26 10:07 self-signed.key
-rw-rw-r-- 1 root root 1870 Nov 26 10:07 self-signed.crt

这里发生什么事情?nginx是否正在接收请求?若否,原因为何?如果是,为什么我看不到任何日志条目?为什么它不能正确回复?

EN

回答 1

Stack Overflow用户

发布于 2020-11-26 19:13:47

因此,很明显,docker compose中的端口映射对于ssl是错误的。

它应该是:

代码语言:javascript
复制
version: '3.5'
services:

  nginx:
    build: images/nginx
    restart: on-failure
    ports:
      - 9080:80
      - 9443:443
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65020802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档