nginx.conf 관련 내용
nginx의 기본 설정 파일인 nginx.conf에서 설정
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
error_log : nginx의 에러로그가 쌓이는 경로이다.
error_log /var/log/nginx/error.log warn;
pid : nginx의 프로세스 아이디 (pid)가 저장되는 경로이다.
pid /var/run/nginx.pid;
worker_connections : Worker Process가 동시에 처리할 수 있는 접속자의 수를 나타낸다. 기본은 1024로 설정되어져 있다.
events {
worker_connections 1024;
}
include : 포함시킬 외부파일을 정의한다. mime.types란 파일에 작성되어진 내용들을 현재 파일로 가져오는 것을 뜻한다.
http {
include /etc/nginx/mime.types;
}
http의 마지막 줄에 있는 include는 서버 속성이 정의되어져 있는 파일을 포함시킨다는 명령어이다. /etc/nginx/conf.d에 .conf로 끝나는 파일들을 포함시켜 가져온다는 것을 뜻한다.
http {
include /etc/nginx/conf.d/*.conf;
}
default_type : 웹서버의 기본 Content-Type을 정의한다.
http {
default_type application/octet-stream;
}
log_format : 로그 형식을 지정한다. 후술한 로그 형태에 따라 로그가 작성되고 기록된다.
http {
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 : 접속 로그가 쌓이는 경로이다.
http {
access_log /var/log/nginx/access.log main;
}
sendfile : sendfile() 함수의 사용여부를 지정한다. sendfile() 함수는 한 파일의 디스크립터와 다른 파일의 디스크립터 간에 데이터를 복사하는 것으로 커널 내부에서 복사가 진행된다.
http {
sendfile on;
# sendfile off;
}
keepalive_timeout : 클라이언트에서 연결이 유지될 시간을 정의한다. 기본은 65로 설정되어져 있다.
http {
keepalive_timeout 65;
}
default.conf 관련 내용
default.conf는 nginx.conf를 통해 include된 서버 설정관련 파일
default2.conf, default3.conf 등 여러 개의 파일을 추가하여 서버관련 설정을 추가로 만들어 포함 가능
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
listen : 해당 포트로 들어오는 요청을 해당 server {} 블록의 내용에 맞게 처리하겠다는 것을 뜻한다.
server {
listen 80;
# http포트인 80번 포트를 통해 들어오는 요청을 해당 블록의 내용에 맞게 처리한다.
}
server_name : 호스트 이름을 지정한다. 가상 호스트가 있는 경우 해당 호스트명을 써넣으면 된다. 만약 로컬에서 작업하고 있는 내용을 nginx를 통해 띄우려고 하는 경우에는 localhost라고 적으면 된다.
server {
server_name localhost;
}
error_page : 요청결과의 http 상태코드가 지정된 http 상태코드와 일치할 경우, 해당 url로 이동한다. 보통 403, 404, 502 등의 에러처리를 위해 사용한다.
url 결과에 따라 이후에 나오는 location = /50x.html와 일치하면 /usr/share/nginx/html 경로에 존재하는 50x.html 파일을 보여준다.
server {
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
location / : 처음 요청이 들어왔을 때 ( server_name이 127.0.0.1인 경우 -> 127.0.0.1로 요청이 들어왔을 때 ) 보여줄 페이지들이 속해있는 경로와 초기 페이지인 index를 지정해준다. / url로 접속했을 경우 index.html, index.htm로 정의된 파일을 보여준다.
server {
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
Location은 ngx_http_core_module에 정의되어 있는 중요한 지시어
request URI에 따른 설정을 하는 곳
URI에 대한 매칭은 텍스트 값을 prefix로 하여서 매칭하거나, 주어진 정규식을 이용해서 매칭할 가능
정규식 패턴에 사용되는 기호
= | 패턴과 정확하게 일치 할 때 사용. URI검색시 가장 우선순위가 높습니다. $host정보에 /test.png로 정확하게 매치가 될 때 사용합니다. 하지만, 만약 "=/test"와 같이 사용할 경우, "$host/test"는 일치하여서 매칭이 되지만, "$host/test/"는 매칭되지 않아버리므로, 주의를 기울여서 사용해야 합니다. |
~ | 대소문자를 구별하여 정규표현식과 일치할 때 사용 보통은 아래의 기호를 더 많이 사용하게 됩니다. |
~* | 대소문자 구별하지 않고 정규표현식과 일치할 때 사용 ex) location ~* \.(gif|jpg|jpeg)$ { return 200 "found" } |
^~ | 지정한 패턴으로 시작할 때 사용. |
아무기호도 없이 텍스트를 사용하면, prefix로 사용해서 해당 텍스트로 시작하는 URI를 찾는다. | |
@name | 이름을 붙여서, location 블럭을 정의한다. 내부 요청에 의해서만 접근할 수 있는데요. 아래와 같이 사용될 수 있습니다. location { try_files $uri @mylabel; } location @mylabel { return 404 "Not Found"} |
아래에서는 해당 정규식에 해당하는 경우, URL의 루트에 /api-server/public을
expires와 add_header지시어를 이용해 파일의 헤더를 수정
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /api-server/public;
expires max;
add_header Cache-Control public;
}
참고로 위에서 사용된 expires와 add_header는 헤더 필드에 “Expires” 와 “Cache-Control”을 추가해 주기 위해 사용
expires max의 의미는 expires와 Cache-Control을 10년으로 설정
아래는 웹에서 다운로드 받은 어떤 이미지의 cache-control
위에서 expires max라고 하면 아래와 같이 설정
Cache-Control을 public으로 하면, 어떤 응답에 의한 캐시건 Cache된다는 것
no-cache라고 하면, 캐시된 복사본을 사용자에게 보여주기 전에, 재검증을 위해 요청을 서버로 보냄
Header정보 세팅
A. 헤더정보 추가
아래와 같이 add_header지시어를 이용해서 expires커스텀한 헤더정보를 추가 가능
location =/test.jpg {
add_header expires modified +24h;
add_header custom_header "custom_header_test"
}
allow and deny
아래의 경우를 보면 182.16.100.0에 대해서만 접근을 허락하고,
나머지에서 접근하면 모두 deny
따라서 해당하지 않는 ip에서 접근할 경우에는 403 forbidden 을 return 처리
server {
listen 80;
server_name .internal.io;
location /private {
allow 182.16.100.0/8;
deny all;
}
}
server_name
ngx_http_core_module에 정의되어 있는 server_name은 가상 서버의 이름을 정할 때 사용
첫번째 이름이 primary서버 네임이 되는데요.
server {
server_name example.com www.example.com;
}
아래와 같이 하면, 앞에 test.example.com같은 이름과 단순한 example.com도 사용 가능
server {
server_name .example.com;
}
필요할 경우는 정규식을 server_name에 사용할 가능
이때는 위의 location에서 했던 것처럼, "~"을 앞에 붙여 줌
listen
listen은 IP를 위한 address와 port번호를 설정
UNIX도메인을 위한 path를 설정해 줄 수도 있음
아래와 같은 설정이 모두 가능. port번호가 적혀있지 않을때는 80포트를 listen
listen 127.0.0.1:8000;
listen localhost:3000;
listen 127.0.0.1;
listen 443; #for https
listen *:8000;
IPv6 주소도 아래와 같이 사용할 수 있음
listen [::]:8000;
listen [::1];
SSL관련한 설정
인증서와 private key와 timeout을 설정 가능
server {
ssl_certificate cert/example.com.chained.crt;
ssl_certificate_key private/example.com.key;
ssl_session_timeout 60;
}
참고 url
https://hhseong.tistory.com/218
[NGINX] nginx 설정 값 정리 #펌
출처 : https://developer88.tistory.com/299 1. NGINX 의 용도 주로 NodeJS같은 웹 애플리케이션 앞에 배치되어 사용되어 지는 NGINX는 주로 어떻게 사용되어 지는 것 일까요? 개인적으로 주요 용도는 아래 두가.
heeseong.co.kr
https://prohannah.tistory.com/136
Nginx 기본 환경 설정
Nginx 기본 환경 설정 Nginx는 환경 설정 텍스트 파일로 여러 가지 값을 지정해 Nginx 설정을 할 수 있도록 지원한다. Nginx 설치 시 기본적으로 설정하는 환경설정 값들을 알아보겠다. 참고 링크 아래
prohannah.tistory.com
https://phsun102.tistory.com/45
Nginx 설치 및 nginx.conf, default.conf 이해하기
1. Nginx란? 가볍고 높은 성능을 가진 웹 서버 (Web Server) 이다. HTTP Server로 활용되며 정적 파일들을 처리하기 위해 사용된다. Reverse Proxy Server로 활용된다. 80번 포트로 들어오는 내용을 3000, 4000,..
phsun102.tistory.com
'Linux' 카테고리의 다른 글
[CentOS 7] Nginx 설치 (0) | 2022.10.26 |
---|---|
profile, bashrc 설정 파일 (0) | 2022.03.17 |
Ubuntu20.04 Mysql 외부접속 허용 (0) | 2021.11.25 |
Linux에서 service 등록 하는 이유 (0) | 2021.09.30 |
init.d, systemd 비교 (0) | 2021.09.30 |