개발/개발관련

[개발관련] NginX 역할 및 컨피그 관련

mabb 2023. 3. 13. 17:19
반응형

▷NginX의 주요 역할 2가지
1) 정적 콘텐츠 제공
2) 리버스 프록시 서버 기능

-> NginX를 동작시키는 것은  config 파일로 셋팅한다.

config 파일로 주요 동작을 셋팅하는 기본적인 맥락은 다음과 같다.

서버, 포트 등 조건을 설정해놓고 조건에 해당하면 특정 경로에 저장된 정적인 리소스 반환하기
서버, 포트 등 조건을 설정해놓고 조건에 해당하면  다른 서버로 넘기기

 

▷config파일의 구성
1) 기본 지시문 (simple directive)
name parameters;으로 구성된다.

2) 블록 지시문 (block directive)
중괄호로 감싼 지시문 블록. 컨텍스트(context)라고 한다.
http{
server{
    location{
    }
  }
}

▷NginX 설정값들에 대한 세세한 셋팅은 다음의 링크를 참조한다.
https://nginx.org/en/docs/http/ngx_http_core_module.html#directives

 

 

▷NginX 설치 시 초기 설정 파일을 확인해본다.

초기 셋팅은 127.0.0.1:80 으로 접근 시
index.html이라는 정적인 리소스를 반환하도록 되어 있다.
proxy기능이나 FastCGI기능의 예시는 주석처리가 되어 있다.


아래는 기본 셋팅을 이해하기 위한 config의 형식과 내용이다.
<값>, [생략가능값], ...목록

user <user> [group] : 유저와 그룹을 정의한다.
error_log <file> : error log가 저장될 파일 정의
pid <file> : 메인프로세스의 PID가 저장될 파일 정의
event{...} : 연결처리에 대한 지시문 컨텍스트 정의
worker_processes <number|auto> :  작업자 프로세스의 CPU 코어 설정
worker_connections <number> : 작업자 프로세스 최대 동시 연결 수
worker_rlimit_nofile <number> : 작업자 프로세스의 최대 오픈 가능 파일 수 제한 설정
http{...} : http서버에 대한 지시문 컨텍스트 정의
include : 해당 파일을 포함시킨다.
default_type <mime-type> : 응답 Mime유형 정의
access_log <path> : 로그 쓰기 경로, 형식, 구성 정의
sendfile <on|off> : sendfile() on/off 정의
keepalive_timeout <timeout> : 연결이 서버측에서 열린상태로 유지되는 시간 제한
listen <address[:port]> :연결 수락 주소와 port 설정
server_name <name>:모든 서버이름 나열. 가상 서버 이름 설정
location [= | ~ | ~* | ^~ ] uri{...} :요청 URI 셋팅
root <path> : 요청에 대한 루트디렉토리 설정
index <file ...> : 인덱스로 사용할 파일 설정
error_page <code...> [=[response]] url; : 특정 에러 코드 시 보여줄 url 설정

/etc/nginx/nginx.conf


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
user  nginx;
#user <user> [group] : 유저와 그룹 정의
worker_processes  auto;
 
error_log  /var/log/nginx/error.log notice;
#error_log <file> : error log가 저장될 파일 정의

pid        /var/run/nginx.pid;
#pid <file> 메인프로세스의 PID가 저장될 파일 정의
 

# event{...} : 연결처리에 대한 지시문 컨텍스트 정의
events {
    worker_connections  1024;
#worker_connections <number> : 작업자 프로세스 최대 동시 연결 수
}


# http{...} : http서버에 대한 지시문 컨텍스트 정의
http {
    include       /etc/nginx/mime.types;
#include : 해당 파일을 포함시킨다.
    default_type  application/octet-stream;
#default_type <mime-type> : 응답 Mime유형 정의
 
    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  /var/log/nginx/access.log  main;
#access_log <path> : 로그 쓰기 경로, 형식, 구성 정의
 
    sendfile        on;
#sendfile <on|off> : sendfile() on/off 정의
    #tcp_nopush     on;
 
    keepalive_timeout  65;
#keepalive_timeout <timeout> : 연결이 서버측에서 열린상태로 유지되는 시간 제한
 
    #gzip  on;
 
    include /etc/nginx/conf.d/*.conf;
}
 
cs

 

/etc/nginx/conf.d/default.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#server{...} : 가상 서버 구성 설정
server {
    listen       80;
#listen <address[:port]>
연결 수락 주소와 port 설정
    server_name  localhost;
#server_name <name>
모든 서버이름 나열. 가상 서버 이름 설정
 
    #access_log  /var/log/nginx/host.access.log  main;

#location [= | ~ | ~* | ^~ ] uri{...} :요청 URI 셋팅
    location / {
        root   /usr/share/nginx/html;
#root <path> : 요청에 대한 루트디렉토리 설정
        index  index.html index.htm;
#index <file ...> : 인덱스로 사용할 파일 설정
    }
 
    #error_page  404              /404.html;
 
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
#error_page <code...> [=[response]] url;
: 특정 에러 코드 시 보여줄 url 설정
    location = /50x.html {
        root   /usr/share/nginx/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;
    #}
}
 
cs
반응형