지난글

2018/07/30 - [Server/AWS] - AWS(nginX)와 내부 서버를 reverse proxy로 연결하기

2018/08/06 - [Server/AWS] - AWS(nginX)와 내부 서버를 reverse proxy로 연결하기(2)



 * 설정순서


1. 내부 네트워크에 있는 서버에 openVPN, easy-rsa를 설치 및 구성

2. EC2에 openVPN 설치 및 구성

3. EC2에 프록시 서버 역할을 할 nginX 설치 및 ssl 구성

4. 사내 방화벽(FG-60E)에서 내부서버로 포트포워딩 설정

5. iptables 설정

6. openVPN 연결 확인


3. EC2에 프록시 서버 역할을 할 nginX 설치 및 ssl 구성


nginx를 설치해줍니다.

#  yum install nginx


nginx를 설치하고나서 nginx의 설정을 해줍니다.

#  cd /etc/nginx

# vi nginx.conf


--------------------------- nginx.conf -----------------------------

worker_processes  1;


events {

   worker_connections  1024;

}


http {

   include       mime.types;

   default_type  application/octet-stream;

   sendfile        on;

   keepalive_timeout  65;


   server {

       listen     80;

       server_name  image.hameekorea.net;

       client_max_body_size 30M;

       keepalive_timeout 5;

       return 301 https://$server_name$request_uri;


   }

   # HTTPS server

   server {

       listen     443 default_server ssl;

       server_name  image.hameekorea.net;


       ssl_certificate      /etc/letsencrypt/live/[도메인]/fullchain.pem;

       ssl_certificate_key  /etc/letsencrypt/live/[도메인]privkey.pem;


       ssl_session_cache    shared:SSL:1m;

       ssl_session_timeout  5m;


       location / {

               proxy_redirect off;

               proxy_pass_header Server;

               proxy_set_header Host $http_host;

               proxy_set_header X-Real-IP $remote_addr;

               proxy_set_header X-Scheme $scheme;

               proxy_pass http://192.168.0.200:8080/;

       }


   }

}

ssl을 통해서만 요청을 받을 예정이므로, 80번 포트로 오게 되면 443포트(ssl) 로 넘어가도록 설정해줍니다.

openvpn이 설정이 되면 내부 서버 로 접근이 가능하므로, 요청이 온 uri에 대해서 내부서버의(:8080)로 넘겨서 응답을 받을수 있도록 설정해줍니다.

위의 설정에서 “ssl_certificate”와 “ssl_certificate_key”의 뒤의 값들은 Let’s encrypt를 통해서 생성된 파일들의 경로를 넣어주면 됩니다.

(*위의 설정에서 80 포트 listen이 포함되어 있는 server절을 삭제하거나 주석처리를 하지 않으면 80번 포트가 inbound rule에서 빠져있을경우 에러가 발생합니다.)


Let’s encrypt를 설치하고 인증서를 발급받습니다.

인증서를 발급받기 전에 웹서버는 중단시켜 놓습니다.

Let’s encrypt 발급을 위해 certbot을 다운 받습니다.

# curl -O https://dl.eff.org/certbot-auto

“certbot-auto” 라는 파일을 다운 받는데, 해당 파일에 실행 권한을 추가해줍니다.

# chmod +x certbot-auto


실행권한을 추가를 해준다음에 아래의 명령어를 수행해줍니다.

certbot를 수행하기 위해서는 python2.7 버전 이상이 설치되어 있어야 한다. 없을 경우에 자동으로 다운을 받아서 설치를 이어서 할수 있습니다.

# ./certbot-auto certonly --debug


Bootstrapping dependencies for Amazon... (you can skip this with --no-bootstrap)

yum is /usr/bin/yum

yum is hashed (/usr/bin/yum)

Loaded plugins: priorities, update-motd, upgrade-helper

amzn-main                                                                                                                                                                                 | 2.1 kB 00:00:00

amzn-updates                                                                                                                                                                              | 2.5 kB 00:00:00

Package gcc-4.8.5-1.22.amzn1.noarch already installed and latest version

Package 1:openssl-1.0.2k-12.109.amzn1.x86_64 already installed and latest version

Package 1:openssl-devel-1.0.2k-12.109.amzn1.x86_64 already installed and latest version

Package system-rpm-config-9.0.3-42.28.amzn1.noarch already installed and latest version

Package ca-certificates-2017.2.14-65.0.1.17.amzn1.noarch already installed and latest version

Package python27-devel-2.7.14-1.123.amzn1.x86_64 already installed and latest version

Package python27-virtualenv-15.1.0-1.14.amzn1.noarch already installed and latest version

Package python27-pip-9.0.3-1.26.amzn1.noarch already installed and latest version

Resolving Dependencies

--> Running transaction check

---> Package augeas-libs.x86_64 0:1.0.0-5.7.amzn1 will be installed

---> Package libffi-devel.x86_64 0:3.0.13-16.5.amzn1 will be installed

---> Package python27-tools.x86_64 0:2.7.14-1.123.amzn1 will be installed

--> Finished Dependency Resolution


Dependencies Resolved


=================================================================================================================================================================================================================

Package                                             Arch Version                                               Repository Size

=================================================================================================================================================================================================================

Installing:

augeas-libs                                         x86_64 1.0.0-5.7.amzn1                                         amzn-main 345 k

libffi-devel                                        x86_64 3.0.13-16.5.amzn1                                       amzn-main 23 k

python27-tools                                      x86_64 2.7.14-1.123.amzn1                                      amzn-updates 712 k


Transaction Summary

=================================================================================================================================================================================================================

Install  3 Packages


Total download size: 1.1 M

Installed size: 2.9 M

Is this ok [y/d/N]: y

Downloading packages:

(1/3): augeas-libs-1.0.0-5.7.amzn1.x86_64.rpm                                                                                                                                             | 345 kB 00:00:00

(2/3): libffi-devel-3.0.13-16.5.amzn1.x86_64.rpm                                                                                                                                          | 23 kB 00:00:00

(3/3): python27-tools-2.7.14-1.123.amzn1.x86_64.rpm                                                                                                                                       | 712 kB 00:00:00

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Total                                                                                                                                                                            5.0 MB/s | 1.1 MB 00:00:00

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

 Installing : python27-tools-2.7.14-1.123.amzn1.x86_64                                                                                                                                                      1/3

 Installing : libffi-devel-3.0.13-16.5.amzn1.x86_64                                                                                                                                                         2/3

 Installing : augeas-libs-1.0.0-5.7.amzn1.x86_64                                                                                                                                                            3/3

 Verifying  : augeas-libs-1.0.0-5.7.amzn1.x86_64                                                                                                                                                            1/3

 Verifying  : libffi-devel-3.0.13-16.5.amzn1.x86_64                                                                                                                                                         2/3

 Verifying  : python27-tools-2.7.14-1.123.amzn1.x86_64                                                                                                                                                      3/3


Installed:

 augeas-libs.x86_64 0:1.0.0-5.7.amzn1                              libffi-devel.x86_64 0:3.0.13-16.5.amzn1   python27-tools.x86_64 0:2.7.14-1.123.amzn1


Complete!

Creating virtual environment...

Installing Python packages...

Installation succeeded.

Traceback (most recent call last):

 File "/opt/eff.org/certbot/venv/bin/letsencrypt", line 7, in <module>

   from certbot.main import main

 File "/opt/eff.org/certbot/venv/local/lib/python2.7/dist-packages/certbot/main.py", line 10, in <module>

   import josepy as jose

 File "/opt/eff.org/certbot/venv/local/lib/python2.7/dist-packages/josepy/__init__.py", line 41, in <module>

   from josepy.interfaces import JSONDeSerializable

 File "/opt/eff.org/certbot/venv/local/lib/python2.7/dist-packages/josepy/interfaces.py", line 8, in <module>

   from josepy import errors, util

 File "/opt/eff.org/certbot/venv/local/lib/python2.7/dist-packages/josepy/util.py", line 4, in <module>

   import OpenSSL

 File "/opt/eff.org/certbot/venv/local/lib/python2.7/dist-packages/OpenSSL/__init__.py", line 8, in <module>

   from OpenSSL import rand, crypto, SSL

 File "/opt/eff.org/certbot/venv/local/lib/python2.7/dist-packages/OpenSSL/rand.py", line 12, in <module>

   from OpenSSL._util import (

 File "/opt/eff.org/certbot/venv/local/lib/python2.7/dist-packages/OpenSSL/_util.py", line 6, in <module>

   from cryptography.hazmat.bindings.openssl.binding import Binding

ImportError: No module named cryptography.hazmat.bindings.openssl.binding



위의 실행결과화면에서 가장 마지막 아래에서 에러를 발견할 수 있다. 위와 같은 에러가 발생하였을 경우에는 아래의 명령어를 수행해주면 됩니다.

(에러 관련 내용 : https://www.lesstif.com/pages/viewpage.action?pageId=54952117 )


# echo -e "import site\nsite.addsitedir('/opt/eff.org/certbot/venv/lib64/python2.7/dist-packages')" > /opt/eff.org/certbot/venv/lib64/python2.7/site-packages/sitecustomize.py

그 후에 다시 “./certbot-auto certonly --debug” 명령어를 다시 수행해줍니다.



실행 과정 nginx 모듈버전을 선택을 할수가 있어서 해당 번호를 선택하고 인증서 발급을 진행하였습니다.

아래와 같은 메시지가 마지막에 보이게 된다면 발급에 성공한 것입니다.

# IMPORTANT NOTES:

#  - Congratulations! Your certificate and chain have been saved at

#    /etc/letsencrypt/live/example.com/fullchain.pem. Your cert will

#    expire on 2018-mm-dd. To obtain a new version of the certificate in

#    the future, simply run Certbot again.

#  - 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


Let’s encrypt를 통한 인증서가 발급이 다 되었다면, 생성된 인증서의 경로를 nginx.conf에 입력해주면 끝이 납니다.





+ Recent posts