[Nginx] Configuration

반응형

이번글에서는 Nginx의 configuartion에 대해 알아보도록 하겠습니다.

1. nginx.conf

nginx의 configuration을 관리하는 파일입니다.

저의 nginx conf 파일은 /etc/nginx/ 에 위치하고 있습니다. 이 nginx.conf 파일을 수정해 nginx의 configuartion을 변경할 수 있습니다.

image.png

처음 nginx을 다운받으면 아래와 같이 default configuartion이 작성되어 있습니다.

nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

         location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {

            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
}

이번글에서는 위의 conf 파일을 지우고 비어있는 상태에서 하나하나 채워나가보도록 하겠습니다.

2. Virtual Host

먼저 static 값을 return 하는 virtual host를 하나 생성해보겠습니다.

nginx를 설치하고난뒤 nginx가 설치된 ip로 접근해보면 아래와 같이 defualt 화면이 제공되고 있습니다. 우리는 이를 지우고 새로운 virtual host가 image 를 return 하도록 변경하겠습니다.

image.png

먼저 site의 root에 /sites/demo/ directory를 생성하고 해당 directory에 아래와 같은 static file들을 추가합니다. 각자 아무거나 하나씩 html/css/png 파일을 생성하도록 합니다.

image.png

다음으로 nginx.conf 파일의 내용을 싹다 지우고 아래와 같이 events와 http의 뼈대만 남겨놓습니다. 이 두개의 뼈대가 없으면 nginx는 실행되지 않습니다.

remove all content of file

> nginx.conf

nginx.conf

events {

}

http {

}

이제 virtual host를 하나 작성하도록 하겠습니다. 각각의 virtual host는 server block 안에 작성합니다.

virtual host의 server_name은 ip 혹은 domain으로 입력합니다. 저는 현재 nginx가 설치된 server의 domain이 없으므로 ip 를 사용하도록 하겠습니다.

만약 domain을 사용할 경우 server name은 wild card character를 사용할 수 있습니다. 예를 들어 *.mydomain.com을 입력한 경우 모든 sub domain에 대한 virtual host를 등록할 수 있습니다.

아래와 같이 작성합니다. 😎

events {

}

http {

        ## virtual host
        server {

                listen 80;
                server_name 54.180.79.141;

                root /sites/demo;
        }
}

위의 코드를 해석해보면 80 port를 listen 하고 있는 54.180.79.141 virtual host가 root directory로 /sites/demo를 바라보고 있습니다.

변경한 내용을 저장후 아래와 같은 명령어를 실행해 nginx를 reload 합니다. reload 하지 않으면 nginx.conf의 변경한 내용이 반영되지 않습니다.

reload nginx

## reload 전에 nginx.conf에 문제가 없는지 확인합니다.
nignx -t

## reload 합니다.
systemctl reload nginx

reload 후 다시한번 nginx가 설치된 ip로 접근하면 아래와 같이 return 하는 화면이 변경되었습니다.

image.png

음 뭔가 이상하네요..? 😅 css가 제대로 적용되지 않은것 같습니다.

curl을 통해 style.css를 확인해보면, nginx가 style.css를 text/plain으로 인식하고 있는 것을 확인할 수 있습니다.

curl style.css

curl -I http://[nginx]/style.css

image.png

이를 변경하기 위해선 nginx.conf 에 mime.types을 아래와 같이 추가합니다.

events {

}

http {

        ## content type
        include mime.types;

        ## virtual host
        server {

                listen 80;
                server_name 54.180.79.141;

                root /sites/demo;
        }
}

mime.type은 모든 확장자에 대한 data type을 정의해놓은 파일입니다.

image.png

이제 nginx는 css 확장자를 text/css로 인식할 것 입니다. nginx를 reload 하고 확인해보면 아래와 같이 정상적으로 css가 적용된 것을 확인할 수 있습니다.

image.png


참고 자료 : https://www.udemy.com/course/nginx-fundamentals/


반응형

'Nginx' 카테고리의 다른 글

[Nginx] Logging  (0) 2020.08.27
[Nginx] Try Files & Named Location  (0) 2020.08.27
[Nginx] Redirect & Rewrite  (0) 2020.08.27
[Nginx] Location Block & Variables  (1) 2020.08.27
[Nginx] Overview & Install  (0) 2020.08.27

댓글

Designed by JB FACTORY