我有一個正在嘗試部署的 Dockerized Web 應用程式。我正在使用 Nginx 和 uWSGI 來提供靜態檔案和管理流量。
我被一個問題困住了一段時間。我也想通過 HTTPS 為應用程式提供服務(顯然)。
我在這里添加相關資訊,如果需要更多資訊,請告訴我。
目錄結構:
root
|
'-> app (django app)
'-> proxy (nginx)
'-> Dockerfile (calling it Nginx-Dockerfile in the remainder of the post)
'-> data/certs/
'-> example.com.crt
'-> example.com.key
'-> default.conf (configuration for nginx)
'-> ... (some more files, irrelevant to the problem)
'-> docker-compose-deploy.yml
'-> Dockerfile (calling it root-Dockerfile in the remainder of the post)
'-> ... (some more files, irrelevant to the problem)
如上所示,我已經創建了 SSL 證書。
Nginx-Dockerfile:
FROM nginxinc/nginx-unprivileged:1-alpine
LABEL maintainer="hackacademy.in"
COPY ./default.conf.tpl /etc/nginx/default.conf.tpl
COPY ./uwsgi_params /etc/nginx/uwsgi_params
COPY ./data/certs/example.com.crt /etc/nginx/certs
COPY ./data/certs/example.com.key /etc/nginx/certs
COPY ./run.sh /run.sh
ENV LISTEN_PORT=8000
ENV APP_HOST=app
ENV APP_PORT=9000
USER root
RUN mkdir -p /vol/static && \
chmod 755 /vol/static && \
touch /etc/nginx/conf.d/default.conf && \
chown nginx:nginx /etc/nginx/conf.d/default.conf && \
chmod x /run.sh && \
chmod rx /etc/nginx/certs
VOLUME /vol/static
USER nginx
CMD ["/run.sh"]
默認組態檔
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/nginx/certs/example.com.crt;
ssl_certificate_key /etc/nginx/certs/example.com.key;
location /static {
alias /vol/web/;
}
location / {
uwsgi_pass ${APP_HOST}:${APP_PORT};
include /etc/nginx/uwsgi_params;
client_max_body_size 10M;
}
}
server {
listen 80;
server_name example.com;
location / {
return 301 https://$host$request_uri;
}
}
這成功地構建了影像,但 nginx 服務器給出了錯誤并且實際上并沒有達到目的。它在日志中顯示的錯誤是
nginx: [emerg] cannot load certificate "/etc/nginx/certs/example.com.crt": BIO_new_file() failed (SSL: error:02001014:system library:fopen:Not a directory:fopen('/etc/nginx/certs/example.com.crt','r') error:2006D002:BIO routines:BIO_new_file:system lib)
我已經做了我相當多的實驗和谷歌搜索,我在這里陷入了一些回圈。我是第一次部署 web 應用程式,哎呀,我什至不是 web 開發人員(我正在學習),所以請用簡單的術語幫助我。真的會很感激。
注意:我example.com在這篇文章中的所有地方都替換了我的域。
uj5u.com熱心網友回復:
我找到了解決方案。
我將 Nginx-Dockerfile 更改為
FROM nginx:1.21.3
LABEL maintainer="hackacademy.in"
COPY ./default.conf.tpl /etc/nginx/default.conf.tpl
COPY ./uwsgi_params /etc/nginx/uwsgi_params
COPY ./data/certs/example.com.crt /etc/nginx/certs/
COPY ./data/certs/example.com.key /etc/nginx/certs/
COPY ./run.sh /run.sh
ENV LISTEN_PORT=8000
ENV APP_HOST=app
ENV APP_PORT=9000
RUN mkdir -p /vol/static && \
chmod 755 /vol/static && \
touch /etc/nginx/conf.d/default.conf && \
chown nginx:nginx /etc/nginx/conf.d/default.conf && \
chmod x /run.sh && \
chmod r /etc/nginx/certs/example.com.crt && \
chmod r /etc/nginx/certs/example.com.key
VOLUME /vol/static
CMD ["/run.sh"]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/328330.html
