前端和后端使用HTTPS和一个IP的Nginx。

huangapple go评论102阅读模式
英文:

Frontend and Backend with HTTPS and one IP nginx

问题

I have a pretty simple project, with a backend in Django and a frontend in React. The front end is hosted on Netlify with a custom-bought domain and has an HTTPS security layer. The Django app is hosted on a DIgitalOcean Droplet and uses docker-compose. Initially, it used to use HTTP, but there were problems with the browser that did not allow that.

So, I am trying to put HTTPS also in the backend. I am trying to add nginx as a proxy to my docker-compose, and that looks fine, the problem seems to be that I cannot use the same domain to use let's encrypt to secure my connection.

I mean, let's say my domain is exaple.com, I try to use api.exaple.com, as suggested in other StackOverflow questions, but this seems not to be working. I tried to add the domain to digital ocean, and modify the settings on GoDaddy DNS (but it keeps not allowing them ).

For reference, here is the docker-compose file:

  1. version: "3.9"
  2. services:
  3. db:
  4. ...
  5. redis:
  6. ...
  7. my_table:
  8. container_name: my_table
  9. build: .
  10. command: ["python", "-m", "gunicorn", "--bind", "0.0.0.0:5000", "-c", "gunicorn.conf.py", "mytable.wsgi"]
  11. volumes:
  12. - .:/api
  13. ports:
  14. - "5000:5000"
  15. depends_on:
  16. - db
  17. - redis
  18. celery:
  19. ...
  20. nginx:
  21. build: ./nginx
  22. ports:
  23. - 80:80
  24. - 443:443
  25. restart: always
  26. volumes:
  27. - ./nginx/conf/:/etc/nginx/conf.d/:ro
  28. - ./certbot/www:/var/www/certbot/:ro
  29. depends_on:
  30. - my_table
  31. certbot:
  32. image: certbot/certbot:latest
  33. volumes:
  34. - ./certbot/www/:/var/www/certbot/:rw
  35. volumes:
  36. my_table_postgres_db:
  37. redis_data:

And the default.conf:

  1. server {
  2. listen 80;
  3. listen [::]:80;
  4. server_name api.my-table.it www.api.my-table.it;
  5. server_tokens off;
  6. location /.well-known/acme-challenge/ {
  7. root /var/www/certbot;
  8. }
  9. location / {
  10. return 301 https://api.my-table.it$request_uri;
  11. }
  12. }
  13. server {
  14. listen 443 default_server ssl http2;
  15. listen [::]:443 ssl http2;
  16. server_name api.my-table.it;
  17. ssl_certificate /etc/nginx/ssl/live/api.my-table.it/fullchain.pem;
  18. ssl_certificate_key /etc/nginx/ssl/live/api.my-table.it/privkey.pem;
  19. location / {
  20. # ...
  21. }
  22. }

Please help me.

英文:

I have a pretty simple project, with a backend in Django and a frontend in React. The front end is hosted on Netlify with a custom-bought domain and has an HTTPS security layer. The Django app is hosted on a DIgitalOcean Droplet and uses docker-compose. Initially, it used to use HTTP, but there were problems with the browser that did not allow that.

So, I am trying to put HTTPS also in the backend. I am trying to add nginx as a proxy to my docker-compose, and that looks fine, the problem seems to be that I cannot use the same domain to use let's encrypt to secure my connection.

I mean, let's say my domain is exaple.com, I try to use api.exaple.com, as suggested in other StackOverflow questions, but this seems not to be working. I tried to add the domain to digital ocean, and modify the settings on GoDaddy DNS (but it keeps not allowing them ).

For reference, here is the docker-compose file:

  1. version: "3.9"
  2. services:
  3. db:
  4. ...
  5. redis:
  6. ...
  7. my_table:
  8. container_name: my_table
  9. build: .
  10. command: ["python", "-m", "gunicorn", "--bind", "0.0.0.0:5000", "-c", "gunicorn.conf.py", "mytable.wsgi"]
  11. volumes:
  12. - .:/api
  13. ports:
  14. - "5000:5000"
  15. depends_on:
  16. - db
  17. - redis
  18. celery:
  19. ...
  20. nginx:
  21. build: ./nginx
  22. ports:
  23. - 80:80
  24. - 443:443
  25. restart: always
  26. volumes:
  27. - ./nginx/conf/:/etc/nginx/conf.d/:ro
  28. - ./certbot/www:/var/www/certbot/:ro
  29. depends_on:
  30. - my_table
  31. certbot:
  32. image: certbot/certbot:latest
  33. volumes:
  34. - ./certbot/www/:/var/www/certbot/:rw
  35. volumes:
  36. my_table_postgres_db:
  37. redis_data:

And the default.conf:

  1. listen 80;
  2. listen [::]:80;
  3. server_name api.my-table.it www.api.my-table.it;
  4. server_tokens off;
  5. location /.well-known/acme-challenge/ {
  6. root /var/www/certbot;
  7. }
  8. location / {
  9. return 301 https://api.my-table.it$request_uri;
  10. }
  11. }
  12. server {
  13. listen 443 default_server ssl http2;
  14. listen [::]:443 ssl http2;
  15. server_name api.my-table.it;
  16. ssl_certificate /etc/nginx/ssl/live/api.my-table.it/fullchain.pem;
  17. ssl_certificate_key /etc/nginx/ssl/live/api.my-table.it/privkey.pem;
  18. location / {
  19. # ...
  20. }
  21. }

Please help me

答案1

得分: 1

你可以按照我的步骤自动更新CA密钥(Https)

  1. ## 步骤1:配置你的nginx.conf文件,填入你的电子邮件和域名,然后运行以下命令
  2. docker run -itd --name test --network=host \
  3. -v "${PWD}"/nginx.conf:/etc/nginx/conf.d/nginx.conf \
  4. -v "${PWD}"/nginx.sh:/etc/nginx/nginx.sh -v "${PWD}"/cronjob:/etc/cron.d/cronjob \
  5. -v "${PWD}"/:/etc/letsencrypt/ \
  6. williehao:nginx-cert:V20.04
  7. ## 步骤2:从你的主机上获取CA密钥
  8. ls ***(你的域名)
  9. ## 如果你没有看到CA密钥,请使用以下命令查找CA密钥的位置
  10. sudo find / -name fullchain.pem
  11. ## 步骤3:将CA密钥合并到另一个容器(例如Ant-Media-Server)中,该容器希望使用CA密钥:
  12. Docker run -v "${PWD}"/:/etc/letsencrypt/ ***

PS:更多详细信息参考

英文:

you can follow my step to automatically update CA key(Https)

  1. ## Step:1. Config your nginx.conf with your Email and Domain name and then Running 'docker run"
  2. docker run -itd --name test --network=host \
  3. -v "${PWD}"/nginx.conf:/etc/nginx/conf.d/nginx.conf \
  4. -v "${PWD}"/nginx.sh:/etc/nginx/nginx.sh -v "${PWD}"/cronjob:/etc/cron.d/cronjob \
  5. -v"${PWD}"/:/etc/letsencrypt/ \
  6. williehao:nginx-cert:V20.04
  7. ## Step:2. Get a CA Key from your host's direction
  8. ls *** (your domain name)
  9. ## if you didn't see CA key please use "find" command line to find CA key location
  10. sudo find / -name fullchain.pem
  11. ## Step3: Combine CA key to another container(APP) which wants to use CA Key: (For example Ant-Media-Server)
  12. Docker run -v "${PWD}"/:/etc/letsencrypt/ ***

PS: More detail reference

huangapple
  • 本文由 发表于 2023年2月27日 18:28:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定