작은숲:위키노트/Nginx 가상 호스트 설정
Nginx에서는 가상 호스트 설정을 /etc/nginx/conf.d 디렉토리 아래에 호스트별로 저장한다. 이렇게 하는 것이 하나의 파일로 설정할 때보다는 유지보수하는데 편할 수도 있다.
아래 내용은 가상 호스트 example.com의 설정 파일인 /etc/nginx/conf.d/example.conf의 내용이다.
server {
listen 80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}server {
listen 80 default_server;
server_name example.com;
root /var/www/example;
index index.html index.php;
charset utf-8;
access_log /var/log/nginx/example-access.log main;
#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 /usr/share/nginx/html;
}
# deny access to .htaccess files, if Apache's document root concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
가상 호스트 설정은 server 블럭에 들어간다. 첫 번째 블럭은 www.example.com으로 접속한 경우 example.com으로 넘겨주는 것이다. 반대의 경우라면 server_name 지시자의 값만 바꾸면 된다.