Nginx 여러 HTTPS 도 메 인 설정

4857 단어
본 고 는 개인 Github 블 로그 와 동기 화 되 었 습 니 다.https://github.com/johnnian/Blog/issues/6어서 오 세 요.
최근 에 위 챗 애플 릿 을 하고 있 습 니 다. 수중 에 있 습 니 다.
  • 클 라 우 드 서버: CentOS 7
  • 여러 개의 1 급 도 메 인 이름
  • 개발 테스트 과정 에서 어떤 이유 로 수중 에 있 는 A, B 도 메 인 이름 이 클 라 우 드 서버 의 443 포트 를 동시에 가리 키 게 하려 면 HTTPS 를 지원 합 니 다.
    Nginx 는 TLS 프로 토 콜 의 SNI 확장 을 지원 합 니 다 (같은 IP 에 여러 개의 서로 다른 인증서 의 도 메 인 이름 을 지원 할 수 있 음). Nginx 를 다시 설치 해서 TLS 를 지원 하면 됩 니 다.
    Nginx 설치
    [root]#  wget http://nginx.org/download/nginx-1.12.0.tar.gz
    [root]#  tar zxvf nginx-1.12.0.tar.gz
    [root]#  cd nginx-1.12.0
    [root]#  ./configure --prefix=/usr/local/nginx --with-http_ssl_module \
    --with-openssl=./openssl-1.0.1e \
    --with-openssl-opt="enable-tlsext"
    
    

    비고: 설치 과정 에서 클 라 우 드 서버 의 환경 에 라 이브 러 리 가 부족 한 것 을 발 견 했 습 니 다. 다운로드 한 후에 Nginx 의 ./configure 명령 을 다시 실행 합 니 다. 구체 적 인 조작 은 다음 과 같 습 니 다.
    [root]#  wget https://nchc.dl.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
    [root]#  tar zxvf pcre-8.35
    [root]#  yum -y install gcc
    [root]#  yum -y install gcc-c++
    [root]#  yum install -y zlib-devel
    
    [root]#  ./configure --prefix=/usr/local/nginx --with-http_ssl_module \
    --with-openssl=./openssl-1.0.1e \
    --with-openssl-opt="enable-tlsext" \
    --with-pcre=./pcre-8.35
    
    

    Nginx 설정
    도 메 인 이름 을 구 매 할 때 도 메 인 이름 공급 자가 무료 SSL 인증 서 를 가지 고 있 으 면 직접 사용 합 니 다.없 으 면 Let 's Encrist 를 사용 하여 무료 CA 인증 서 를 만 들 수 있 습 니 다.
    Nginx 설정 열기: vi /etc/nginx/nginx.conf
        
        ...
        server {
            listen       443 ssl;
            listen       [::]:443 ssl;
            server_name  abc.com;
            root         /usr/share/nginx/html;
            
            ssl_certificate "/root/keys/abc.com.pem";
            ssl_certificate_key "/root/keys/abc.com.private.pem";
            include /etc/nginx/default.d/*.conf;
            
            location / {
            }
            error_page 404 /404.html;
                location = /40x.html {
            }
            error_page 500 502 503 504 /50x.html;
                location = /50x.html {
            }
        }
        
        server {
            listen       443 ssl;
            listen       [::]:443 ssl;
            server_name  def.com;
            root         /usr/share/nginx/html;
            
            ssl_certificate "/root/keys/def.com.pem";
            ssl_certificate_key "/root/keys/def.com.private.pem";
            include /etc/nginx/default.d/*.conf;
            
            location / {
            }
            error_page 404 /404.html;
                location = /40x.html {
            }
            error_page 500 502 503 504 /50x.html;
                location = /50x.html {
            }
        }
         
        
    

    설정 완료 후 Ngixn: nginx -s reload 다시 불 러 오기
    무료 CA 인증서 신청
    SSL 인증서 가 없 는 경우 아래 방법 으로 CA 인증서 인 Let 's Encript 를 무료 로 받 을 수 있 습 니 다.
    단계 1: Let 's Encrypt 공식 클 라 이언 트 인 CetBot 설치
    [root]#  yum install -y epel-releasesudo 
    [root]#  yum install -y certbot
    

    단계 2: Nginx 프로필 을 설정 하고 서버 모듈 (80 포트 감청) 에 아래 설정 을 추가 합 니 다.
    CertBot 은 서버 도 메 인 이름 을 검증 할 때 무 작위 파일 을 생 성 합 니 다. 그리고 CertBot 서버 는 HTTP 를 통 해 이 파일 에 접근 할 수 있 도록 Nginx 설정 을 확보 해 야 합 니 다.
    server {
        listen       80 default_server;
        
        ...
        
        location ^~ /.well-known/acme-challenge/ {   
            default_type "text/plain";   
            root     /usr/share/nginx/html;
        }
        
        location = /.well-known/acme-challenge/ {   
            return 404;
        }
    }
    
    

    Nginx 다시 불 러 오기: nginx -s reloadSTEP 3: SSL 인증서 신청
    [root]# certbot certonly --webroot -w /usr/share/nginx/html/ -d your.domain.com
    
    

    설치 과정 에서 CA 인증 서 를 업데이트 하 는 데 사용 할 메 일 입력 을 알려 줍 니 다.
    설치 에 성공 하면 기본적으로 /etc/letsencrypt/live/your.domain.com/ 에서 CA 인증 서 를 생 성 합 니 다.
    |-- fullchain.pem 
    |-- privkey.pem
    

    단계 4: Nginx 설정
    server {
        listen       443 ssl;
        listen       [::]:443 ssl;
        server_name  def.com;
        root         /usr/share/nginx/html;
        
        ssl_certificate "/etc/letsencrypt/live/your.domain.com/fullchain.pem";
        ssl_certificate_key "/etc/letsencrypt/live/your.domain.com/privkey.pem";
        include /etc/nginx/default.d/*.conf;
        
        location / {
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
    
    

    설정 완료, Nginx 다시 불 러 오기
    단계 5: 인증서 자동 업데이트
    명령 행 에서 먼저 아 날로 그 업데이트 인증 서 를 진행 합 니 다.
    certbot renew --dry-run
    

    아 날로 그 업데이트 가 성공 하면 crontab -e 명령 을 사용 하여 자동 업데이트 작업 을 사용 합 니 다.
    [root]# crontab -e
    
    30 2 * * 1 /usr/bin/certbot renew  >> /var/log/le-renew.log
    
    

    관련 참고
  • Let's Encript
  • Nginx 다운로드 연결
  • crontab
  • 좋은 웹페이지 즐겨찾기