EC2에서 Nginx 호스팅

ewillwin·2023년 1월 2일
0

TSMtech Record

목록 보기
22/39

Nginx 설치

sudo apt install nginx

를 통해 설치
-> /etc에 "nginx" directory가 생김


Nginx

Nginx는 웹 서버임 (정적 컨텐츠를 제공해주는 proxy 서버)


Nginx 관리 파일 설정

  1. /etc/nginx/site_available 생성
sudo mkdir /etc/nginx/site-available
  1. /etc/nginx/site-enabled 생성
sudo mkdir /etc/nginx/site-enabled
  1. 기본 config 파일 설정 (conf 파일은 "/etc/nginx/nginx.conf")
sudo vi /etc/nginx/nginx.conf
  1. nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  1. default.conf
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debiian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html/dist;

        # Add index.php to the list if you are using PHP
        index index.html;

        server_name eco-map.co.kr;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.html;
        }

        # pass PHP scripts to FastCGI server
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/eco-map.co.kr/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/eco-map.co.kr/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


}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name example.com;i
#
#       root /var/www/example.com;
#       index index.html;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}

server {
    if ($host = eco-map.co.kr) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    if ($host = www.eco-map.co.kr){
        return 301 https://$host$request_uri;
    } # managed by Certbot

        listen 80 default_server;
        listen [::]:80 default_server;
        server_name eco-map.co.kr;
    return 404; # managed by Certbot




}
  1. nginx 실행
sudo service start nginx
  1. status 확인
sudo service status nginx
  1. file들 설명
  • sites-available: 가상 서버에 관련된 서버의 설정들이 위치하는 directory. 해당 directory에 존재하는 설정 file들은 사용하지 않아도 존재함/ 비활성화된 사이트들의 설정파일 위치
  • sites-enabled: "sites-available"에 존재하는 설정 file들 중, 사용하는 설정 file만 link하여 사용할 수 있도록 하는 directory/ 활성화된 사이트들의 설정파일 위치
  • nginx.conf: nginx 관련 설정을 block 단위로 설정하는 곳. 여기서 "sites-enabled"에 존재하는 file들을 불러옴/ 메인 설정 파일
    -> "available"에 설정 file을 씀
    -> "enabled"에 available 설정 file을 link
    -> "nginx.conf" file에서 "enabled"의 "default" file을 불러옴

-> enabled의 "default" file을 수정해주면 link되어 자동 적용 됨


nginx.conf 구성

nginx.conf 파일의 구성은 directive로 이루어져 있음
Directive = Block + Context

directive를 끝내는 방법

  • 세미클론
  • 중괄호 블록

directive 주석

  • #

nginx.conf의 Origin directive 구성

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
       . . .
}

http {
       . . .
}

https://growing-nyang.tistory.com/75
domain name 변경은 위 링크 참고

profile
Software Engineer @ LG Electronics

0개의 댓글