이 글을 읽고 Nginx 설정을 시작하기에 앞서, 아래의 글을 읽고 오면 흥미가 조금 더 생길 것이다.
https://codenme.tistory.com/82
[Nginx 의 설정 방법]
우선 별도의 ubuntu 컨테이너를 하나 만들어주자.
docker run -dit -p 80:8080 --name myos ubuntu:20.04
우분투 쉘 진입
docker exec -it myos /bin/bash
쉘에 진입한 뒤 apt-get update를 실행한 뒤, nginx를 설치하자.
apt-get install nginx=1.18.0-0ubuntu1.4 //1.18.0-0ubuntu1 버전의 nginx 설치
여기서, 1.18.0-0ubuntu1 버전을 사용하고 싶은데, 해당 버전이 바뀌어서 위의 명령어가 동작하지 않을 수 있다.
그럴 때는 아래의 명령어를 실행하고, 확인된 버전을 가지고 실행하면된다.
apt-cache policy nginx
성공적으로 설치가 되면 시간대를 설정해야하는데, 아시아 -> 서울에 해당하는 번호를 입력하면 된다.
이제 find 명령어로 nginx 의 설정파일을 열어보자
find -name nginx.conf //해당 파일의 위치를 찾는 명령어
결과: ./etc/nginx/nginx.conf
**버전마다 천차만별이기에, 이런게 있다는 것만 우선 알아두자!
설정 파일을 보면 worker_processes auto; 로, 기본적으로 프로세스의 개수는 자동으로 설정되어있다.
nginx.conf
/etc/nginx/conf.d/mySebSite.com.conf 형태로 각 사이트 별 설정 파일을 include하는 식으로 사용함.
아래의 http{} 부분이 주로 보게되는 설정 영역이다.
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
##
//http 블록의 설정을 이 2가지 폴더에 기능, 사이트 별로 설정 파일을 만들어 넣으면, 전체 설정파일에 include되며 적용된다.
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
실제 "서버" 설정 파일은
/etc/nginx/sites-abvilable/default 에 들어가 있다. ( sites-enable에 심볼릭 링크로 걸려있다)
enable에 모든 사이트 설정을 넣어넣고, 그때그때 실제로 서비스하는 사이트를 sites-available에 넣어서 사용하는 것으로 유추된다.
우선, 포트를 80 -> 8080 으로 변경했다. 그 이유는 우리가 아까 nginx를 구동하기위해 ubunt내에 별도의 ubunt 컨테이너를 아래와 같이 만들어서 접근했기 때문이다.
docker run -dit -p 80:8080 --name myos ubuntu:20.04
즉, 아무런 포트 입력 없이 111.111.111.111 을 주소로 요청을 하면
111.111.111.111:80 -> nginx를 구동하는 우분투 컨테이너의 8080 으로 매핑이 되기 때문이다.
따라서 8080으로 변경해줘야 올바르게 동작한다.
server {
listen 8080 default_server;
listen [::]:8080 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; //주소를 쳤을 때,(ip, 도메인 등) 이 서버는 어디에서 해당 html 파일의 경로를 찾아서 리턴할 것인가?
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html; //주소만(ip, 도메인 등) 쳤을 때,나오는 디폴트 html
//디폴트 index.html 없으면 index.htm, 이것도 없으면 index.nginx-debian.html 가 렌더링 된다.
server_name _; // 도메인이름이 없다면 그대로, 있다면 도메인 이름을 넣는다.
//location 경로에 있는 파일을 찾아보고 없다면 에러를 리턴하는 곳
location / { //기본 경로 192.168.0.171 설정시 /blog -> 192.168.0.171/blog가 된다.
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404; //매칭되는 경로 없다면 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;
#}
}
현재는 root /var/www/html 에서 html 파일을 가져오고, index.html, index.htm~~~~ 를 반환하게 되어있다.
ec2의 public ip 로 접속 시 아래와 같은 화면이 나온다.
현재 root /var/www/html/ 에는 아래와 같은 html 파일이 있다.
현재 index.html, index.htm 이 없기에, index.nginx-debian.html이 나타난 것.
location 설정
location이 지정되지 않는다면, root /var/www/html 의 경로를 따르지만, 특정 요청 url은 다른 경로에서 가져오도록 설정할 수 있다.
~~~/blog 는 이제 /var/www 로 매칭된다.
이제 ~~~~~/blog 는 root /var/www 에서 html 을 가져올 것이고
ex) ip번호/blog/image1.html -> /var/www/blog/image1.html
ip번호/blog. ->. /var/www/blog/index.html. ( 디폴트 html 설정은 그대로)
그 외는 root/var/www/html에서 가져올 것.
ex) ip번호/image2.html -> /var/www/html/image2.html
위의 blog를 만든 방식처럼
location /flask 도 하나 만들어주자.
이제 /var/www/html/blog/ , /var/www/html/flask/ 경로에 index.nginx-debian.html 을 만들어서 올바르게 동작하나 확인해보자.
위 사진과 같이 /var/www/ 경로로 이동해서 mkdir blog , mkdir flask 로 폴더를 만들어주고, 각각
/var/www/html 폴더에서 cp 작업으로 기본으로 가지고 있던 index.nginx-debian.html 파일을 복사한 뒤, 해당 html 파일에서
"welcome nginx" ->. welcome flask, welcone blog 등으로 변경해주자.
여기서 /var/www/html 폴더 내에서 ../flask/index.html 의 의미는 뭘까?
../. -> 부모 폴더 즉 "/var/www" 이다.
즉 /var/www/html/index.nginx-debian.html 을 /var/www/flask/index.html 로 복사하는 명령어이다.
이제 복사된 html 파일의 텍스트를 수정해 각 html을 식별하자
결과
올바르게 설정한대로 매핑이 된 것을 볼 수 있다.
'DevOps' 카테고리의 다른 글
Apache VS Nginx 비교분석 (0) | 2023.04.20 |
---|---|
[도커] Docker Compose (0) | 2023.04.19 |
[도커] 도커 활용 (0) | 2023.04.19 |
[도커] Docker File 기본 사용법 (0) | 2023.04.18 |
[도커] Mac에서 도커 세팅법 (0) | 2023.04.14 |