728x90

https설정은 할일이 매우많다

 

로그인이 들어가는 순간 그 서버는 거의 필수적으로 https를 사용해야 된다고 생각한다.

 

그래서 관련 포스팅을 해본다.

 

 

기본적으로 https설정은 유료이다.

 

단, Certbot을 사용하면 무료로 설정할 수 있다.

 

Let's encrypt에서 발행해주는 https인증서는 그 수명이 3개월인데, 간단한 설정으로 자동 무한발급을 받을수 있다.

( lets' encrypt의 정책이 바뀌지 않는다면)

 

https://certbot.eff.org/lets-encrypt/ubuntubionic-nginx

 

Certbot - Ubuntubionic Nginx

Different Internet services are distinguished by using different TCP port numbers. Unencrypted HTTP normally uses TCP port 80, while encrypted HTTPS normally uses TCP port 443. To use certbot –webroot, certbot –apache, or certbot –nginx, you should have an

certbot.eff.org

 

위의 사이트에서 자신의 os - web server에 맞게 검색하여 가이드라인을 따른다면

 

세팅을 아주 무난히 마칠수 있다.

 

문제는, 늘 세팅 이후의 나의 환경에 맞게 설정하는 부분이다.

 

다들, 그냥 이렇게 저렇게 하라고만 할뿐 어떻게 내 환경에 맞게 설정해야 될지는 아무도 알려주지 않아 내가 적는다.

 

0. 최소한의 웹서버 설정

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;
        root /home/ubuntu/myproject/build;
        index index.html index.htm index.nginx-debian.html;

        server_name mydomain;

        location / {
                try_files $uri $uri/ =404;
        }
}

 

1. Http요청 Https redirect시키기

 

웹의 기본 요청방식(통신방식)은 http이기 때문에, 사용자들의 http요청을 https로 redirect시킬 필요가 있다.

 

/etc/nginx/sites-available/defulat 파일에서 설정을 해보도록 하자 (본 파일은  nginx.conf에 include됨)

 

server 블록을 하나 만들어 http요청을 모두  https로 redirect시킬 수 있다.

server {
    server_name mydomain;

    if ($host = mydomain) {
        return 301 https://mydomain
    }

    return 404;
}

 

2. 내 프로젝트 붙이기

 

이제 막, 서버를 개설중인 것이라면 내 프로젝트를 nginx/apache서버에 붙여줄 필요가 있다.

 

어떻게 만들어졌냐에 따라 크게 달라지지만

 

먼저, html로 만든 간단한 웹사이트의 경우이다.

server {
    client_max_body_size 100M; // 너무 큰 요청은 곤란하다..

    server_name mydomain;
    root /var/www/html; // 내 프로젝트 위치를 잡아주면됨. 특히, 그중에서도 index.html의 위치
    index index.html // 위에서 말한 index.html 파일명을 명시

    // https 설정!!
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mydomain/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mydomain/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

 

다음으로, react로 만든 프로젝트이다.

...

server {
  server_name mydomain;
  root /home/ubuntu/myproject/build; // 내 프로젝트 위치를 잡아주면됨. 특히, 그중에서도 index.html의 위치
  index index.html // 위에서 말한 index.html 파일명을 명시


  location / {
  	try_files $uri $uri/ /index.html;
  }

  location ~* \.(?:manifest|appcache|html?|xml|json|service-worker\.js)$ {
  	expires -1;
  }

  location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
    expires 1M;
    access_log off;
    add_header Cache-Control "public";
  }

  location ~* \.(?:css|js)$ {
    expires 1y;
    access_log off;
    add_header Cache-Control "public";
  }

  // https 설정!!
  listen 443 ssl; # managed by Certbot
  ssl_certificate /etc/letsencrypt/live/mydomain/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/mydomain/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

 

728x90
반응형