nginx docker容器中react应用的SSL证书

jucafojl  于 2023-03-22  发布在  Nginx
关注(0)|答案(1)|浏览(129)

我有一个工作的docker nginx容器,里面运行着一个react应用程序。我没有成功添加我的ssl证书,这些证书已经在node.js后端应用程序中运行。
下面是我的dockerfile的样子:

FROM nginx
COPY build/ /usr/share/nginx/html
COPY certs/abc.de_ssl_certificate.cer /etc/nginx/certs/
COPY certs/*.abc.de_private_key.key /etc/nginx/certs/
COPY nginx.conf /etc/nginx/conf.d/default.conf

我尝试创建一个nginx.conf文件并自动将其复制到容器中,但只要我尝试使用配置文件,浏览器就无法访问服务器。只有http请求被转发到https。我尝试了这样的方法:

server {
  listen 80;
  server_name abc.de;

  location / {
    return 301 https://$host$request_uri;
  }
}

server {
  listen 443 ssl;
  server_name abc.de;

  ssl_certificate /etc/nginx/certs/abc.de_ssl_certificate.cer;
  ssl_certificate_key /etc/nginx/certs/*.abc.de_private_key.key;

    location / {
    proxy_pass http://localhost:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}
js4nwp54

js4nwp541#

我使用这段代码来代理传递ssl和ssl加密的REST API,但应该对你也有效:

server
{
    listen 443 ssl http2;
    server_name abc.de;
    ssl on;
    ssl_certificate /etc/ssl/abc.de/cert.crt;
    ssl_certificate_key /etc/ssl/abc.de/cert.key;

    location /
    {
        proxy_pass https://localhost:3000;
        proxy_ssl_server_name on;
        proxy_set_header Accept-Encoding "";
        proxy_set_header Cache-Control no-cache;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header X-Real-IP $remote_addr;

    }
}

正如你所看到的,你的代码中缺少一些设置:
在服务器块上:

ssl on;

在外景地:

proxy_ssl_server_name on;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';

它应该解决ssl问题(此设置用于CA验证的证书),用于自签名证书,位置:

proxy_ssl_trusted_certificate /etc/ssl/abc.de/cert.pem;
 proxy_ssl_verify              off;

相关问题