
NGINX hosting config
- window에 NGINX를 설치 하셧다면(http://madfishdev.tistory.com/2) 설치된 폴더에 conf 폴더를 열어 파일들을 확인합니다.

- 이중 nginx.conf가 default로 nginx가 로드해서 사용하는 config 파일 입니다. 아래와 같은 Default 설정을 확인 할수 있습니다.
#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$ {
# 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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
- 전부다 알아 보기는 너무 복잡하고 뭐가 뭔지 알기 어렵습니다.
- 이 설정 중 우리는 browser에서 내 html 파일이나 js파일을 hosting해서 화면을 보고 싶은 것이니, 명확하게 눈에 띄는 'http {' 로 시작하는 블럭을 살펴 보면 됩니다.
- http 블럭내부에 server라는 블럭이 하나 보이고 그안에 몇가지 설정을 확인 할 수 있습니다.
- listen: 'listen 80' 이라는 항목은 현재 server가 open하고자 하는 port 입니다. http가 기본 80 port를 사용하기 때문에 80으로 설정되 있습니다.
- server_name: 'server_name localhost'는 현재 server로 유입되는 request(요청)이 localhost일 경우 현재 server block의 설정을 적용한다는 의미 입니다. locahost는 본인 pc에서 테스트 할때 사용할 수 있고, 만약 다른 url로 server를 운영하려면 이부분을 바꿔야 합니다.
- server_name의 설정으로 서버에서는 분기를 만들수 있습니다.
- server_name에는 wild card, regular express와 같은 pattern도 가능 합니다.
- 모든 domain을 처리 하려면 _를 넣어 주면 됩니다. 'server_name _' 와 같습니다.
- location: 가장 중요한 항목입니다. location은 현재 위에 설정된 80 port의 server_name의 domain을 가진 요청을 location에 설정된 항목을 serving해주게 됩니다. location에 파일 경로를 넣어 주면, 그 파일이 hosting되고, proxy_pass와 같이 중간 proxy 역할을 넣어주면 자연스럽게 proxy에 연결된 주소로 연결시켜 주게 됩니다.
- 아래 Default 설정의 의미를 살펴 보겠습니다.
- root는 현재 server machine(pc)의 경로(directory) 입니다.
- index index.html index.html은 nginx를 통해 접속시 첫 페이지(index page)를 보여줄 파일 이름입니다.
- html/index.html 이게 없다면 html/index.htm 파일이 사이트 접속시 보여지게 됩니다.
location / {
root html;
index index.html index.htm;
}
- 현재 nginx의 설정되는 directory의 path기준은 nginx.exe가 존재하는 경로 입니다.
- 아래 그림에서 볼수 있는 것과 같이 nginx.exe와 같이 html directory가 있습니다.
- 그리고 그 내부에 50x.html과 index.html 파일이 존재 함을 확인 할 수 있습니다.
- 저 index.html 파일이 아마 설정없이 nginx를 실행 시켯을때 보여지는 default 화면의 html 소스입니다.


- 50x.html는 에러 표기를 위한 html 소스 파일입니다. 위 conf 소스 중 아래와 같은 부분이 있습니다.
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
- error_page의미는 http response code중 500 502 503 504의 경우 /50x.html 페이지를 보여줘라는 의미 입니다.
- location = /50x.html 는 50x.html 파일의 위치를 설정합니다. 파일이름은 = /50x.html로 확정시켯으며, 그 위치는 root html로 설정되었습니다.
Static file hosting
- 위의 Default conf 설정의 설명만으로도 개발자가 직접 만든 파일을 hosting하기 위해서 어떻게 해야 하는지 감이 올 것입니다.
- 우리가 호스팅 해야하는 파일 index.html과 js를 c://www/index.html, c://www//script.js 라고 가정해보고 위 설정을 바꿔 보겠습니다.
- 아래 코드를 보면 server에 location 설정을 root c:\\www, index index.html로 변경시켰습니다.
#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 c:\\www;
index index.html;
}
#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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
- 이제부터 nginx를 재시작(config 재로드) 합니다.
- cmd 또는 powershell를 실행 시키고, nginx.exe가 있는 folder로 이동합니다.

- 아래와 같이 재시작 합니다.
nginx -s stop
start nginx
- 혹은 reload command를 이용합니다.
nginx -s reload
- 이제 localhost로 browser를 통하여 접근하게 되면, c:\\www\ 아래 파일을 들이 hosting되기 시작합니다.
'NGINX' 카테고리의 다른 글
NGINX uWSGI 연결하기 (0) | 2020.08.05 |
---|---|
NGINX 파일 HOSTING 하기(in linux ubuntu 18.04) (0) | 2020.03.27 |
NGIX 설치(on linux ubuntu 18.04) (0) | 2020.03.27 |
NGINX 설치(on windows) (0) | 2020.03.25 |