Nginx로 포트포워딩을 하기 위해서 EC2에 설치한다.
sudo apt update
sudo apt install nginx
sudo dpkg -l nginx
nginx는 ufw 방화벽과 관련된 3개의 프로필이 존재한다.
1. Nginx Full - 80 & 443 포트를 둘다 연다.
2. Nginx HTTP - 80 포트만 연다.
3. Nginx HTTPS - 443 포트만 연다.
이후 SSL을 적용할 예정이기 때문에 Nginx Full 프로필을 사용한다.
프로필 허용
sudo ufw allow 'Nginx full'
방화벽 다시 로드
sudo ufw reload
방화벽 상태 확인
sudo ufw status
완료 된 후, 서버도메인:80을 입력해서 Nginx 기본 랜딩 페이지가 뜨면 성공이다.
EC2의 /etc/nginx 경로로 오게되면 nginx.conf 파일과 conf.d 폴더가 존재한다.
다음 명령어를 통해 nginx.conf 파일을 연다.
sudo vim nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 1024;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
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;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;
}
mail {
# See sample authentication script at:
# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# auth_http localhost/auth.php;
# pop3_capabilities "TOP" "USER";
# imap_capabilities "IMAP4rev1" "UIDPLUS";
#server {
# listen localhost:110;
# protocol pop3;
# proxy on;
#}
#server {
# listen localhost:143;
# protocol imap;
# proxy on;
#}
}
worker_processess : 몇개의 워커 프로세스를 생성할 것인지 지정하는 지시어이며 1이면 모든 요청을 하나의 프로세스로 실행하겠다는 뜻이다.
CPU 멀티코어 시스템에서 1이면 하나의 코어만으로 요청을 처리하는 것이다. 보통 auto로 사용하는 경우가 많다.
error_log : 로그 레벨을 설정하는 지시어
pid : nginx의 마스터 프로세스 id정보가 저장된다.
events 블록 : 이벤트 블록은 네트워크 동작 방법과 관련된 설정이다.
http 블록 : 하위에 server 블록, 그리고 location 블록을 갖는 루트 블록이다. 여기에서 선언된 값은 하위 블록에 상속된다.
블록 내부에서 중복되는 값은 conf.d 폴더의 하위 설정으로 빼 공통으로 적용 할 수 있다.
http 블럭 내부의 include /etc/nginx/conf.d/*.conf; 를 통해서 server블록을 작성하였다.
/etc/nginx/conf.d 디렉토리에 sites.conf 파일을 만들어 다음과 같이 작성 하였다.
server {
listen 80;
server_name [서버도메인];
location / {
proxy_pass http://[서버도메인]:[포트];
}
location /api {
proxy_pass http://[서버도메인]:[포트];
}
}
기본 요청은 react 웹페이지로 보내지만, api가 붙인 요청이 들어오면 서버로 보내게 설정해두었다.
[출처]
https://juneyr.dev/nginx-basics
https://narup.tistory.com/209