Publicidad
Publicidad

Más contenido relacionado

Publicidad

Último(20)

Nginx Https 적용하기.pptx

  1. 사설 인증서 적용(개발서버) CentOS7
  2. Intro Kafka Cluster incoming message - without kafka client library support (e.g. Android or IOS app, IOT devices, …) - send message using Rest APIs https - 443 port : web page (for test) - 8443 port : rest service
  3. 방화벽 개방(HTTPS) • 443, 8443 [root@master ~]# firewall-cmd --permanent --zone=public --add-port=8443/tcp success [root@master ~]# firewall-cmd --permanent --zone=public --add-port=443/tcp success [root@master ~]# firewall-cmd --reload success [root@master ~]# firewall-cmd --list-all
  4. 사설 인증서 생성 [root@master ~]# mkdir /root/ssl/ [root@master ~]# cd ssl [root@master ssl]# openssl req -x509 -days 358000 -nodes -newkey rsa:2048 > -keyout /root/ssl/nginx-ssl.key -out /root/ssl/nginx-ssl.crt Generating a 2048 bit RSA private key ........+++ .....................................................................................................+++ writing new private key to '/root/ssl/nginx-ssl.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:KR State or Province Name (full name) []:Seoul Locality Name (eg, city) [Default City]: Organization Name (eg, company) [Default Company Ltd]:Kopo Organizational Unit Name (eg, section) []:smart finance Common Name (eg, your name or your server's hostname) []:devfmd Email Address []:a@a.com 인증서 만료일 : 미지정시 30일, 테스트용으로 몇 백년 지정 openssl req -x509 -days 358000 -nodes -newkey rsa:2048 -keyout /root/ssl/nginx-ssl.key -out /root/ssl/nginx-ssl.crt 도메인명 일치해야 함(불일치시 클라이언트에서 인증서 오류발생으로 접속 불가)
  5. Nginx 디렉토리로 인증서 카피 인증서 복사를 mv가 아닌 cp명령으로 수행해야 하는 이유 : https://stackoverflow.com/questions/37994513/
  6. Nginx 설정(443) server { listen 80; #server_name localhost; server_name devfmd; return 301 https://$host$request_uri; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } } server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name devfmd; root /usr/share/nginx/html; ssl_certificate "/etc/nginx/nginx-ssl.crt"; ssl_certificate_key "/etc/nginx/nginx-ssl.key"; ssl_session_cache shared:SSL:1m; ssl_session_timeout 10m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } /etc/nginx/conf.d/default.conf
  7. host명 설정 서버에서 설정 로컬 PC에서 설정
  8. Nginx 시작 및 브라우저 테스트 • systemctl start nginx
  9. 인증서 확인
  10. 결과 확인 • http://devfmd 접속시 https://devfmd로 자동 이동(301)
  11. Nginx 설정(8443) /etc/nginx/conf.d/test.conf server { listen 8443 ssl http2; listen [::]:8443 ssl http2; server_name devfmd; ssl_certificate "/etc/nginx/nginx-ssl.crt"; ssl_certificate_key "/etc/nginx/nginx-ssl.key"; ssl_session_cache shared:SSL:1m; ssl_session_timeout 10m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location /api { proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 300; send_timeout 3000; proxy_pass http://testme/api; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }
  12. 테스트 준비 • nginx -s reload • 백엔드 어플리케이션 준비
  13. 포스트맨 준비 ssl 검증 옵션을 off 한다 : 제일 간편하나 권장하지 않음
  14. 포스트맨 테스트
  15. 포스트맨 준비(w/ 인증서) • 사설 인증서 등록 • pem, crt, key를 등록 • pem은 기존 crt로부터 생성 • pfx는 기존 crt, key로부터 생성 https://learning.postman.com/docs/sending-requests/certificates/ https://www.tp-link.com/kr/support/faq/3330/ openssl x509 -in nginx-ssl.crt -out nginx-ssl.pem -outform PEM openssl pkcs12 -export -in nginx-ssl.crt -inkey nginx-ssl.key -out nginx-ssl.pfx
  16. 포스트맨에 사설 인증서 등록
  17. 포스트맨에 사설 인증서 등록 서버에서 다운로드한 인증서 파일을 등록한다. 도메인명은 인증서 CN명과 같아야 함 서버에서 다운로드한 인증서 파일을 등록한다.
  18. 포스트맨에 사설 인증서 등록 등록 결과
  19. 테스트 결과 https 정상 통신 확인
  20. [참고] curl https request curl --location --request GET 'https://devfmd:8443/api' --header 'Content-Type: application/json'-v --cacert nginx-ssl.crt
  21. 정식 인증서 적용 (운영서버) Ubuntu 18.04 사전 준비 사항 : 도메인, 고정IP -> 없을 경우 ngrok 이용하여 설치
  22. Nginx 설치 • apt-get install nginx • {Nginx Home Directory}/sites-available/default upstream testme { server 127.0.0.1:8030; } server { listen 80 default_server; listen [::]:80 default_server; location @rewrites { rewrite ^(.+)$ /index.html last; } location / { proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 300; send_timeout 3000; proxy_pass http://testme; } } default
  23. Certbot 설치 • $ sudo apt-get update • $ sudo apt-get install software-properties-common • $ sudo add-apt-repository universe • $ sudo add-apt-repository ppa:certbot/certbot • $ sudo apt-get update # 만약 다음과 같은 에러가 난다면? E: The repository 'http://ppa.launchpad.net/certbot/certbot/ubuntu focal Release' does not have a Release file. $ sudo apt-add-repository -r ppa:certbot/certbot
  24. Certbot 인증서 발급(Let’s Encrpyt) • certbot --nginx -d 도메인명 예> certbot --nginx -d sort.example.net • 발급 결과 root@ip-172-31-6-254:/etc/letsencrypt/live/sort.example.net# ll total 12 drwxr-xr-x 2 root root 4096 Dec 10 16:25 ./ drwx------ 3 root root 4096 Dec 10 16:25 ../ -rw-r--r-- 1 root root 692 Dec 10 16:25 README lrwxrwxrwx 1 root root 41 Dec 10 16:25 cert.pem -> ../../archive/sort.example.net/cert1.pem lrwxrwxrwx 1 root root 42 Dec 10 16:25 chain.pem -> ../../archive/sort.example.net/chain1.pem lrwxrwxrwx 1 root root 46 Dec 10 16:25 fullchain.pem -> ../../archive/sort.example.net/fullchain1.pem lrwxrwxrwx 1 root root 44 Dec 10 16:25 privkey.pem -> ../../archive/sort.example.net/privkey1.pem
  25. 결과 확인 http 접속시 https 자동 이동 certobot 인증서 발급시 nginx default 파일에 https 관련 코드와 301 리다이렉션 코드 자동으로 추가됨
  26. Certbot 인증서 자동갱신 크론탭 등록 • certbot renew --dry-run • /etc/cron.d/에 크론탭 자동 등록
  27. 고정IP, 도메인이 없는 경우 • ngrok 또는 localtunnel 적용 고려(임시방편) ngrok : 하나의 서버에 대해 무료, 서버 재시작시 도메인 변경됨 localtunnel : 도메인명.loca.lt 형태로 고정되나, 중계서버 간헐적 불안정 VS https://localtunnel.github.io/www/ https://ngrok.com/
  28. 참고자료 • CentOS에서 Nginx, Certbot 설치하고 인증서 적용하기 https://stove99.github.io/linux/2019/08/27/install-lets-encrypt-to-nginx-in-centos/ • Root CA 인증서 발급 및 적용 절차 https://www.sslcert.co.kr/guides/NGINX-SSL-Certificate-Install https://velog.io/@twkim8548/Nginx%EC%97%90%EC%84%9C-SSL-%EC%A0%81%EC%9A%A9%ED%95%B4%EC%84%9C-Https-%EB%A1%9C- %EC%A0%91%EC%86%8D-%EB%90%98%EA%B2%8C-%ED%95%B4%EB%B3%BC%EA%B9%8C

Notas del editor

  1. 인증서 만료는 디폴트 30일 : https://serverfault.com/questions/920461/why-openssl-ignore-days-for-expiration-date-for-self-signed-certificate
  2. openssl pkcs12 -export -in nginx-ssl.crt -inkey nginx-ssl.key -out nginx-ssl.pfx Enter Export Password: Verifying - Enter Export Password:
  3. curl --location --request GET 'https://devfmd:8443/api' --header 'Content-Type: application/json'-v --cacert nginx-ssl.crt
  4. 필요시 {nginx home directory}/nginx.conf에서 user root; 설정
  5. https://jamie95.tistory.com/184
  6. /etc/nginx/sites-available/default 파일 하단에서 301 리다이렉션 부분 주석처리 후, if문만 상단 80 섹션으로 이동시킨다. (이렇게 하면 80 을 443 으로 이동시키면서 http를 사용하는 다른 서비스 유지 가능) server { listen 80 default_server; listen [::]:80 default_server; if ($host = smart.wonyong.net) { return 301 https://$host$request_uri; }
  7. https://kibua20.tistory.com/151
Publicidad