简体   繁体   English

如何在docker中设置正确的Nginx反向代理配置

[英]How to set the right Nginx reverse proxy configuration in docker

I deployed Nginx reverse proxy in docker, and it belong to the bridge network which using 172.16.10.0/24 .我在 docker 中部署了 Nginx 反向代理,它属于使用172.16.10.0/24的桥接网络。 And I have the other web app in docker which in different bridge network 172.16.20.0/24 .我在 docker 中有另一个 web 应用程序,它位于不同的桥接网络172.16.20.0/24中。 In order to let Niginx reverse proxy to connect web app, I have set Nginx reverse proxy to join the 172.16.20.0/24 as well.为了让 Niginx 反向代理连接 web 应用程序,我设置了 Nginx 反向代理加入172.16.20.0/24为好。

My web app is hosting in http://localhost:8899, and I have bind host:8899 --> container:80.我的 web 应用托管在 http://localhost:8899 中,并且我绑定了 host:8899 --> container:80。 What I want to try is: when someone visit https://mydomain, and reverse proxy should pass to http://localhost:8899.我想尝试的是:当有人访问 https://mydomain 时,反向代理应该传递给 http://localhost:8899。

My nginx config is as follow:我的 nginx 配置如下:

server {
    listen 80;
    listen [::]:80;
    server_name mydomain;
    return 301 https://$host$request_uri;
}


server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name mydomain;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ssl_certificate /ssl/my_domain_cert.pem;
    ssl_certificate_key /ssl/my_domain.key;

    location / {
        proxy_set_header Host $host;
        proxy_set_header Cookie $http_cookie;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Host $host;
        proxy_pass http://localhost:8899;
        proxy_read_timeout 90;
    }
}

However, when i connect to https://mydomain, the error is SSL handshake failed (Error code 525).但是,当我连接到 https://mydomain 时,错误是 SSL 握手失败(错误代码 525)。 How should I fix the problem?我应该如何解决这个问题?

The 525 HTTP error means, there is no valid SSL certificate installed. 525 HTTP 错误表示,没有安装有效的 SSL 证书。

The nginx conf is searching for the SSL certificate files in these locations: nginx conf 正在以下位置搜索 SSL 证书文件:

ssl_certificate /ssl/my_domain_cert.pem;
ssl_certificate_key /ssl/my_domain.key;

Unless you created a SSL certificate in your Dockerfile or created one before and put them in these locations, you have to MANUALLY create a SSL certificate.除非您在 Dockerfile 中创建了 SSL 证书或之前创建了一个并将它们放在这些位置,否则您必须手动创建 SSL 证书。

How to create a key and pem file:如何创建密钥和 pem 文件:
https://www.digitalocean.com/community/tutorials/how-to-create-a-self-signed-ssl-certificate-for-nginx-on-centos-7 https://www.digitalocean.com/community/tutorials/how-to-create-a-self-signed-ssl-certificate-for-nginx-on-centos-7
How to get.pem file from.key and.crt files? 如何从.key 和.crt 文件中获取.pem 文件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM