80번 포트로 열린 NGINX(3.38.222.149:80) 웹 서버에 요청을 보내는 것이다.
즉 우리가 웹 서버에 아이피주소(3.38.222.149)를 입력하면
자동으로 80번 포트가 붙고 NGINX에 요청이 간 후 응답이 온 것이다.
크롬에서 개발자도구(F12)를 실행해보자.
실제로 NGINX가 미리 정해진 html 코드 (정적 콘텐츠)를
응답으로 반환하는 것을 확인할 수 있다.
그렇다면 여기서 의문점이 생긴다!
어떤 상황에 어떤 응답을 주어야하는지를 어떻게 판단하는 것일까?
3.2 NGINX에서의 정적콘텐츠 호스팅
앞서 운영체제를 ubuntu로 설정하였으므로,
아래의 명령어를 입력하여 디렉토리에 접근하면
nginx의 설정파일을 확인할 수 있다.
(로컬이 아닌 원격 접속 터미널에서 입력해야 한다!)
cd /etc/nginx/sites-available cat default
설정파일은 다음과 같다.
##
# 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 {
listen 80 default_server;
listen [::]:80 default_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.debian.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;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/var/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;
#}
}
# 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;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
NGINX 웹 서버가 실행될 때는
위의 설정 파일을 읽으면서 실행이 시작된다.
위의 설정파일에서 server 블록을 집중적으로 살펴보자.
1) root
- 정적 콘텐츠를 찾는 시작 디렉토리
2) index
- 기본적인 요청에 대해 index 뒤의 파일을 찾아 보여줌
실제로 아래의 명령어를 입력하여
해당 디렉토리의 파일을 확인해보면 html 코드를 확인해 볼 수 있다.
cd /var/www/html ls cat index.nginx-debian.html
이는 앞서 우리가 'Welcome to NGINX" 개발자 도구에서 확인했던
html 코드와 일치한다!
(80번 포트로 요청을 보냈을 때 왔던 응답 코드)
[ 💡NGINX 응답 순서 정리 ]
1) root /var/www/html : 정적 콘텐츠를 찾을 시작 디렉토리 설정
2) index : 기본요청이 온 경우 어떤 파일을 응답할지 설정
3) 응답 : /var/www/html/index.nginx-debian.html 을 응답으로 반환
3.3 NGINX 설정 파일에서의 location 블록
NGINX의 설정파일을 변경하여
(아이피주소)/temp 웹 브라우저에 접속했을 때
새로운 정적 페이지가 뜨도록 실습해보자!
먼저 아래의 명령어를 입력하여
vi에디터에서 /etc/nginx/sites-available/default 코드를 변경해보자.
즉 그냥 www.naver.com으로 쓰면 www.naver.com:80 으로 접속하게 되는데 이는 웹 서버로 요청하는 것이다.
하지만 어떻게 응답이 WAS에서 가능할까?
어떻게 우리가 원하는 아래와 같은 로직이 가능한지 살펴보자.
3.6 Reverse Proxy
Reverse Proxy 란?
- 클라이언트와 서버간의 통신을 중계
- 클라이언트와 서버의 중간에 위치하여 보안/성능을 개선
- 클라이언트의 요청을 받고 이를 다시 본 서버로 보내주는 대리자 역할
[ 참고 ]
서버가 요청을 받기만 하는 것이 아니다!
connect() 시스템 콜과 같이 서버 프로세스가 내부적으로 요청을 보내는 시스템콜고 가능하다
즉 서버도 다른 서버 프로세스에게 요청이 가능하다!
[ 포워드 프록시 VS 리버스 프록시 ]
1) 포워드 프록시 : 외부 컴퓨터 (다른 컴퓨터) 서버로 요청을 보냄
2) 리버스 프록시 : 내부 컴퓨터 (같은 컴퓨터) 서버로 요청을 보냄
3.7 NGINX 에서 리버스 프록시 설정하기
리버스 프록시를 설정하기 위해
NGINX 설정 파일( /etc/nginx/sites-available/default )에 아래의 프록시 설정 코드를 추가한다.
(설정 파일 변경 방법 위에 참고)
location / {
proxy_pass http://localhost:3000; <- 프록시 설정
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}