/etc/nginx/sites-enabled 를 수정하여, 파일 호스팅을 해보도록 하겠습니다.
일단 먼저 위 경로로 이동합니다.
cd /etc/nginx/sites-enabled/
nginx.conf가 먼저 include하고 있는 config directory이다.
내부에 default 라는 파일이 있다.
ls -la
default -> /etc/nginx/sites-available/default
/etc/nginx/sites-available/default 에 symbol link가 걸려 있음을 알 수 있다.
sites-available 에서 config를 작업 한 후 완료가 되면 sites-enabled에 symbo link를 걸어 nginx에 적용되는 방식이라고 생각된다.
일단 default를 수정한다. include /etc/nginx/sites-enabled/*; 가 이미 http 블록 안에 걸려 있으므로 server config만 만들어 내면 된다.
default내부에 아래와 같은 기본 설정이 볼 수 있다. 앞 포스팅의 window의 nginx config와 거의 동일 하다.
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;
}
server_name은 _ 되 있으므로 모든 domain을 커버한다.
listen포트는 전부 80 port
root /var/www/html로 기본 파일 경로가 설정되어 있어, root로 설정된 하위 폴더의 파일들이 기본적으로 hosting된다.
저 root를 변경하면 개발자가 원하는 folder의 파일들을 hosting 할 수 있다.
만약 다른 방법을 원한다면 3가지 선택 사항이 있다.
port 변경, 추가
location 변경, 추가
server_name 변경, 추가
server {
listen 3000 default_server; #1
listen [::]:3000 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 www.image.com; #2
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
#proxy_pass http://127.0.0.1:3003/;
}
location /images/ { #3
root /var/www/images
}
위 코드의 #1번 을 보면 port를 3000으로 변경하였다. 이제 3000번 포트로 요청를 보내면 위 config에 설정된 형태로 동작한다.
위 코드의 #2번 을 보면 www.image.com domain으로 요청이 들어온 경우 위 config에 설정된 형태로 동작한다.
위 코드의 #3번 을 보면 url path에 /image로 들어온 경우 아래와 경로를 root로 잡게 된다.
위와 같은 3가지 방법을 섞어 쓰거나, 하나만 설정하여 쓰는 방법으로 다양한 방식으로 요청을 구분하여 처리 할 수 있다.
모든 config설정을 마친 후 에는 service nginx reload 또는 stop, start 해야 적용됨을 잊지 말자