**동아리 UMC에서 제공한 자료 토대로 공부/복습한 내용**
이전 내용에서 이어짐
https://dodo-studying.tistory.com/559
1. NGINX에 있는 설정 파일 확인
- ubuntu에서는 /etc/nginx/sites-available 디렉토리 내에 있는 default가 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;
# }
#}
- root : 여기서 server block 속 root는 정적 콘텐츠를 찾는 시작 디렉토리를 의미
- index : 기본적인 요청에 대해 index 뒤의 파일들을 찾아서 웹 상으로 보여준다는 것을 의미
- root /var/www/html 라고 적혀 있으니 /var/www/html로 이동하면 index.nginx-debian.html 파일이 있는 것을 확인 가능
cd /var/www/html
ls
- 내부 확인하면 아래와 같음
cat index.nginx-debian.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
- 맨 처음에 봤던 페이지임
정리하자면,
- root /var/www/html을 통해 정적 콘텐츠를 찾아낼 시작 디렉토리를 설정
- index를 통해 기본 요청이 온 경우 어떤 파일을 줄지 설정
- / 요청에 대해 /var/www/html/index.nginx-debian.thml을 응답으로 줌
2. NGINX에서의 정적콘텐츠 호스팅
1. /temp/ 호스팅
- /etc/nginx/sites-available/default로 이동
cd /etc/nginx/sites-available
cat default
- 작성 권한 설정
sudo chmod u+w /etc/nginx/sites-available/default
- /temp을 추가 (아래 코드)
location /temp{
root /var/www;
index temp.html
try_files $uri $uri/ =404;
}
- 예시 :
##
# 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;
}
location /temp{
root /var/www;
index temp.html
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:/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;
# }
#}
- /var/www로 이동
cd /var/www
- 설정 변경되었으니 재실행
sudo systemctl restart nginx
- temp 디렉토리 생성 > temp 디렉토리 안에서 temp.html 생성
sudo mkdir temp
cd ./temp
sudo vi temp.html
- html 문서 작성
<h1> hi Server! <h1>
- /temp로 접속 > 아래와 같이 뜨면 성공
2. /temp/test.html 호스팅
- /var/www/temp로 이동 > test.html 생성
cd /var/www/temp
sudo vi test.html
- 원하는 내용 입력
<h1> nginx static file hosting practice </h1>
<h2> let's do staic file hosting without index default setting</h2>
- /temp/test.html로 접속 > 아래와 같이 뜨면 성공
3. location block으로 여러 경우 다르게 호스팅
1. /web/
- nginx 설정 파일을 수정
location /web{
root /var/www;
index welcome.html
try_files $uri $uri/ =404;
}
- /var/www/web 디렉토리 생성 > 안에 welcome.html 생성
cd /var/www
sudo mkdir web
cd ./web
sudo vi welcome.html
- welcome.html 내용 입력
<h1>welcome!</h1>
2. /web/happy.html
- /var/www/web 디렉토리 안에 happy.html 생성
sudo vi happy.html
- happy.html 내용 입력
<h1>happy!</h1>
3. /text/hello.txt
- nginx 설정 파일을 수정
location /text{
root /var/www;
try_files $uri $uri/ =404;
}
- /var/www/text 디렉토리를 생성 > 안에 hello.txt 파일 생성
cd /var/www
sudo mkdir text
cd ./text
sudo vi hello.txt
- hello.txt 내용 입력
hello world!
'Server > node.js' 카테고리의 다른 글
[CICD] Node.js + Github Action + Elastic Beanstalk 무중단 배포 (2) (0) | 2024.08.28 |
---|---|
[CICD] Node.js + Github Action + Elastic Beanstalk 무중단 배포 (1) (0) | 2024.08.28 |
[AWS] EC2 구축 (3-完) - node.js (0) | 2024.08.20 |
[AWS] EC2 구축 (2) - node.js (0) | 2024.08.20 |
[AWS] EC2 구축 (1) - node.js (0) | 2024.08.20 |