일.환경
Centos:
$ cat /etc/redhat-releas
CentOS Linux release 7.3.1611 (Core)
필수는 아닙니다. 설정 방법은 동일하며 nginx 컴파일 설치 과정도 동일합니다. 익숙한 것을 사용하면 됩니다
nginx:
$ /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.9.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module
configure arguments 의 with-http_ssl_module 은 필수입니다. ./configure 시 이 옵션을 추가하여 ssl 모듈을 활성화해야 합니다. 그렇지 않으면 번거롭습니다 (phpize 와 같은 것이 없으므로 매개변수를 추가하여 재컴파일하여 필요에 따라 덮어써야 합니다)
무료 SSL 인증서:
https://letsencrypt.org/getting-started/
Let's Encrypt 는 비교적 신뢰할 수 있으며 비영리 조직으로 HTTPS 화를 추진하는 것을 목표로 합니다. 따라서 세계 평화에 기여합시다 (비록 주로 무료이기 때문이지만~)
P.S. 인증서 유효기간은 3 개월이며 만료되면 갱신해야 합니다. 도구로 자동으로 갱신할 수 있으며 그렇게 번거롭지 않습니다
node:
$ nvm ls
v6.10.0
-> v7.9.0
default -> stable (-> v7.9.0)
node -> stable (-> v7.9.0) (default)
stable -> 7.9 (-> v7.9.0) (default)
필수는 아닙니다. nginx 설정 완료 후 역방향 프록시가 정상적으로 작동하는지 확인하는 데 사용됩니다. HTTP 서버를 시작할 수 있는 다른 방법이라면何でも 됩니다
이.무료 SSL 인증서 신청
https://letsencrypt.org/getting-started/ 로 이동하여 단계를 따르십시오. With Shell Access 방식을 권장합니다. certbot 이라는 명령줄 도구를 제공하며 대화식 설정으로 직관적인 조작이 가능합니다
또한 명령줄 도구를 사용하는 장점은 인증서 만료 시 자동으로 갱신할 수 있으며 명령 하나만 실행하면 된다는 것입니다. 이를 cron 작업에 추가하면 방해받지 않습니다. 심지어 매일 자동으로 실행할 수도 있으며 공식에서도 이를 권장합니다. 이는 하트비트 역할을 하여 생존을 나타내고 스크리닝으로 인한 취소를 방지합니다:
Note:
if you're setting up a cron or systemd job, we recommend running it twice per day (it won't do anything until your certificates are due for renewal or revoked, but running it regularly would give your site a chance of staying online in case a Let's Encrypt-initiated revocation happened for some reason). Please select a random minute within the hour for your renewal tasks.
certbot 의 단계를 따르십시오. 예를 들어 이 문서 환경의 단계 는 다음과 같습니다:
# yum 저장소 추가
yum -y install yum-utils
yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional
# certbot 설치
sudo yum install certbot
# 인증서만 요청, 자동 설정 안 함
certbot certonly
모든 것이 정상이라면 다음과 같은 메시지가 표시됩니다:
Generating key (2048 bits): /etc/letsencrypt/keys/0000_key-certbot.pem
Creating CSR: /etc/letsencrypt/csr/0000_csr-certbot.pem
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/<mydomain>/fullchain.pem. Your cert will
expire on 2017-07-17. To obtain a new or tweaked version of this
certificate in the future, simply run certbot again. To
non-interactively renew *all* of your certificates, run "certbot
renew"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
인증서와 개인 키는 /etc/letsencrypt/live/<mydomain>/ 에 저장됩니다:
$ ls /etc/letsencrypt/live/<mydomain>/
cert.pem chain.pem fullchain.pem privkey.pem README
cert.pem 은 인증서 (서버 인증서), privkey.pem 은 개인 키입니다. 이 두 가지만 있으면 됩니다. 또한 chain.pem 은 루트 인증서와 중간 인증서 등 서버 인증서 외의 인증 프로세스에 필요한 인증서 체인입니다. fullchain.pem 은 완전한 인증서 체인으로 cert.pem 과 chain.pem 의 내용을 포함합니다
마지막으로 자동 갱신을 활성화합니다:
# 갱신 테스트
certbot renew --dry-run
모든 것이 정상이라면 cron 작업을 추가합니다:
crontab -e
# 매일 1:11 에 인증서 갱신
1 1 * * * certbot renew >> ~/cron/cert.log --renew-hook "/usr/local/nginx/sbin/nginx -s reload"
# 모든 작업 보기
crontab -l
주의: 인증서 갱신 후 nginx 를 자동으로 재시작하려면 --renew-hook 를 반드시 추가해야 합니다. 그렇지 않으면 만료된 인증서를 계속 사용하게 됩니다. 인증서는 갱신되지만 적용되지 않습니다
삼.nginx HTTPS 역방향 프록시 설정
ssl 모듈을 확인합니다:
$ /usr/local/nginx/sbin/nginx -V
configure arguments 에 with-http_ssl_module 이 있으면 ssl 모듈이 이미 있다는 것입니다. 그렇지 않으면 매개변수를 추가하여 재컴파일하고 기존 nginx 실행 파일을 덮어써야 합니다. 구체적인 단계는 nginx 재컴파일로 ssl 모듈 추가 를 참조하십시오
설정 파일을 수정합니다:
# 백업
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf_
# 편집
vi /usr/local/nginx/conf/nginx.conf
# HTTPS server 아래 내용 수정
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /etc/letsencrypt/live/<mydomain>/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/<mydomain>/privkey.pem;
ssl_session_timeout 5m;
location / {
proxy_pass http://localhost:7777;
}
}
# 설정 구문 테스트
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
# nginx 재시작
/usr/local/nginx/sbin/nginx -s reload
모든 것이 정상이라면 이제 사이트 전체의 HTTPS 요청이 7777 포트로 전달되도록 설정되었습니다
P.S. 특정 경로 전달을 설정해야 하는 경우 location 경로 매칭 규칙을 수정하면 됩니다. 자세한 내용은 Nginx 설정 개요 를 참조하십시오
사.HTTP 서버 시작하여 프록시 작동 확인
HTTPS 연결은 nginx 가 관리하므로 뒤의 HTTP 서비스는 거의 변경할 필요가 없습니다. HTTP 요청을 받기 때문이지만 nginx 에서 전달된 것입니다 (단, cookie 등은 전달 후 문제가 발생할 수 있어 proxy_cookie_path 를 설정해야 합니다. 나중에 설명하겠습니다)
const http = require('http');
const PORT = 7777;
http.createServer((req, res) => {
res.end('hoho, ' + PORT);
}).listen(PORT);
console.log('server is ready, listening ' + PORT);
https:<mydomain>/ 에 접속하여 hoho, 7777 이 표시되면 프록시가 작동하는 것입니다
주소 표시줄에 아름다운 초록색 자물쇠가 표시됩니다. 또한 일반적으로 DNS 해석 규칙을 설정하고 HTTP 요청을 HTTPS 로 강제 리디렉션하거나 더 적절한 HSTS 방식 등을 설정해야 하지만 이는 나중의 일입니다
아직 댓글이 없습니다