DevOps

Nginx와 nginx.conf 파일 설정

prden 2023. 6. 18. 13:34

1. Nginx란?

용도 

  클라이언트로부터 요청 받을 때 요청에 맞는 정적 파일 응답(Http Web Server)
  Reverse Proxy Server로 WAS 서버 부하를 줄일 수 있는 로드밸런서로 활용

 

특징 
  Nginx는 Event-Driven 구조로 동작(동접자수 1만명 넘어갈 때 효율적인 방안을 위해), 한 개 또는 고정된 프로세스만 생성  (새로운 프로세스와 쓰레드 생성하지 않음 )
  Nginx는 하나의 Master Process와 다수의 Worker Process로 구성, Master Process는 설정파일을 읽고, 유효성 검사 및 Worker Process관리

 

0. nginx의 /etc/nginx 디렉터리 내부, 정적 콘텐츠 가저오기 위한 설정
1) sites-available : sites-available 디렉터리는 모든 설정 파일을 저장하는 용도이며
2) sites-enabled : sites-enabled 디렉터리는 sites-available 디렉터리에 생성된 설정 파일 중에서 실제 적용할 설정 파일의 심볼릭 링크를 생성하는 용도이다.
nginx는 sites-enabled 디렉터리에 생성된 링크 파일의 심볼릭 링크를 통해 
sites-available 디렉터리의 설정 파일을 읽고, 이를 실행하게 된다.

 

https://icarus8050.tistory.com/57

 

[Nginx] Nginx 이해하기

Nginx? Nginx는 간단하게 말씀드리자면 경량 웹 서버입니다. 클라이언트로부터 요청을 받았을 때 요청에 맞는 정적 파일을 응답해주는 HTTP Web Server로 활용되기도 하고, Reverse Proxy Server로 활용하여 W

icarus8050.tistory.com

 

2. Jenkins, Github, Vue.js 배포

https://brunch.co.kr/@springboot/188

 

Vue.js + Nginx 웹서버 구축하기

Vue CLI, Nginx 사용해서 심플한 프론트엔드 웹서버 구축하기 | 추가 의견 -  2019/11/20 vue-cli 사용 방법이 버전 업그레이드 되면서 일부 변경되었습니다. 이 글에서 사용하는 명령어를 사용하기 보

brunch.co.kr

 

3. nginx.conf

find / -name nginx.conf

- 보통 /etc/nginx/* 아래에 설정파일이 위치해있고, 로그파일은 /var/log/nginx/*에 위치해 있다. 

 

include 지시어는 특정 파일을 포함하는 기능 수행

- # /etc/nginx/conf.d 디렉토리 아래 있는 .conf 파일을 모두 읽어 들임 (특정 디렉토리의 모든 파일을 include)

include /etc/nginx/conf.d/*.conf;

아래와 같이 구성(Core, http, server, location, events..)

# worker 프로세스를 실행할 사용자 설정
# - 이 사용자에 따라 권한이 달라질 수 있다.
user  nginx;
# 실행할 worker 프로세스 설정
# - 서버에 장착되어 있는 코어 수 만큼 할당하는 것이 보통, 더 높게도 설정 가능
worker_processes  1;

# 오류 로그를 남길 파일 경로 지정
error_log  /var/log/nginx/error.log warn;
# NGINX 마스터 프로세스 ID 를 저장할 파일 경로 지정
pid        /var/run/nginx.pid;


# 접속 처리에 관한 설정을 한다.
events {
    # 워커 프로레스 한 개당 동시 접속 수 지정 (512 혹은 1024 를 기준으로 지정)
    # Worker Process가 동시에 처리할 수 있는 접속자 수를 정의한다. worker_process * worker_connections = 최대 접속자수
    worker_connections  1024;
}

# 웹, 프록시 관련 서버 설정
http {
    # mime.types 파일을 읽어들인다.
    include       /etc/nginx/mime.types;
    # MIME 타입 설정
    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 api 를 사용할지 말지 결정
    sendfile        on;
    #tcp_nopush     on;

    # 접속시 커넥션을 몇 초동안 유지할지에 대한 설정
    keepalive_timeout  65;

    # (추가) nginx 버전을 숨길 수 있다. (보통 아래를 사용해서 숨기는게 일반적)
    server_tokens off

    #gzip  on;

    # /etc/nginx/conf.d 디렉토리 아래 있는 .conf 파일을 모두 읽어 들임
    include /etc/nginx/conf.d/*.conf;
}

1) http 블록

 http 블록은 HTTP 부분과 관련된 모듈의 지시어와 블록을 정의하며, server와 location의 루트 블록이다. http, server, location 블록은 계층 구조를 가지고 있다. 많은 지시어가 각 블록에서 동시에 사용될 수 있는데, http 블록의 내용은 server 블록의 기본값이 되고, server 블록의 내용은 location 블록의 기본값이 된다. 만약 상위 블록에서 선언된 지시어를 하위 블록에서 다시 선언하면 상위의 지시어는 무시된다. http 블록 안에 한 개 이상의 server 블록을 선언할 수 있다.

2) server 블록

 server 블록은 하나의 호스트를 선언하는데 사용하며, http 블록 안에서만 사용할 수 있습니다. server 블록에는 한 개 이상의 location 블록을 선언할 수 있습니다.

3) location 블록

location 블록에는 server 블록 안에 정의되며, 특정 URL을 처리하는 방법을 정의합니다. 예를 들면 http://example.com/hello/1 과 http://example.com/world/1 접근하는 요청을 다르게 처리하고 싶을 때 사용합니다.

4) events블록

events 블록은 네트워크의 작동 환경을 설정하는 지시어를 제공. 이벤트 블록의 지시어는 이벤트 블록에서만 사용할 수 있고, http, server, local 블록과는 상속 관계를 갖지 않는다 아래의 지시어들은 반드시 events 블록 안에서만 사용해야 합니다. (accept_mutex, accept_mutex_delay, worker_connections)

 

another example 

user prdelit;
worker_processes auto;
worker_rlimit_nofile 204800;

pid		/var/run/nginx.pid;
events {
	worker_connections 20000;
	use epoll;
	multi_accept on;
	}
http {
	include		/etc/nginx/mime.types;
	defalut_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"';
	add_header X-Content-Type-Options nosniff;
	add_header X-XSS-Protection "1; mode=block";
	error_log  /var/log/ngix/error.log crit;
	
	## TCP options
	tcp_nodelay on;
	tcp_nopush on;
	
	##Timeouts
	send_timeout		10;
	keepalive_timeout	10;
	keepalive_requests	10;
	client_body_timeout	10;
	client_header_timeout10;
	client_body_buffer_size 5M;
	client_header_buffer_size 1k;
	client_max_body_size	10M;
	large_client_header_buffers 4 16k;
	output_buffers				1 32k;
	postpone_output				1460;
	## General Options
	sendfile 			on;
	server_tokens		off;
	recursive_error_pages on;
	ignore_invalid_headers	on;
	server_name_in_redirect	off;
	
	gzip			on;
	gzip_static 	on;
	gzip_buffers	16 8k;
	gzip_comp_level		7;
	gzip_http_version  1.1;
}

 

https://prohannah.tistory.com/136

 

Nginx 기본 환경 설정

Nginx 기본 환경 설정 Nginx는 환경 설정 텍스트 파일로 여러 가지 값을 지정해 Nginx 설정을 할 수 있도록 지원한다. Nginx 설치 시 기본적으로 설정하는 환경설정 값들을 알아보겠다. 참고 링크 아래

prohannah.tistory.com

5. Nginx 설정 

https://nginxstore.com/blog/nginx/%EA%B0%80%EC%9E%A5-%EB%A7%8E%EC%9D%B4-%EC%8B%A4%EC%88%98%ED%95%98%EB%8A%94-nginx-%EC%84%A4%EC%A0%95-%EC%97%90%EB%9F%AC-10%EA%B0%80%EC%A7%80/

 

'DevOps' 카테고리의 다른 글

PM2, Nginx //front app 배포  (0) 2023.06.13
Docker & Kubernetes  (0) 2023.01.29
AWS EC2 + Docker + AWS ECS를 통한 orchestration  (1) 2023.01.23
CI(Continuous Integration)/CD(Continuous Delivery)  (0) 2023.01.11
Infra 인프라  (0) 2022.11.19