본문 바로가기
NGINX

NGINX 파일 HOSTING 하기(in linux ubuntu 18.04)

by NOMADFISH 2020. 3. 27.

nginx ubuntu hosting


NGINX hosting config

  • 앞의 posting한 window와 거의 같지만, linux 계열에 설치된 nginx의 경우 config 파일의 위치나 구성이 약간 다릅니다.
/etc/nginx/nginx.conf
  • 의 경로에 파일이 nginx의 기본 config 파일 입니다.
  • 또한 윈도우와 약간 다른 방식으로 설정을 할 것입니다. 현재 위의 nginx.conf 파일을 직접 수정하지 않고, nginx.conf가 include하는 다른 파일을 수정하여, 원래 기본 설정을 손대지 않은 상태에서 수정가능 하도록 해보겠습니다.
  • 아래가 설치만 한상태의 nginx.conf 파일 입니다.
  • server 설정부분이 없고, 대신, include라는 키워드가 보입니다. 
  • include /etc/nginx/sites-enabled/*; 부분의 의미는 /etc/nginx/sites-enabled 경로 아래 모든 파일들을 이 설정에 가져다 붙이 겟다는 의미 입니다.
  • 저부분을 무시하고 'http { ' 내부에 직접 server 설정을 해도 상관은 없습니다.
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # 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; # 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
        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # 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;
#       }
#}

NGINX Site config 방법

  • /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가지 선택 사항이 있다.
    1. port 변경, 추가
    2. location 변경, 추가
    3. 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 해야 적용됨을 잊지 말자

'NGINX' 카테고리의 다른 글

NGINX uWSGI 연결하기  (0) 2020.08.05
NGIX 설치(on linux ubuntu 18.04)  (0) 2020.03.27
NGINX 파일 HOSTING 하기(on Windows)  (0) 2020.03.25
NGINX 설치(on windows)  (0) 2020.03.25