Nginx가 어떤 역할을 하는지 알았다면, 이제는 실제로 사용해볼 차례입니다.
1. 운영 체체별 설치 방법
2. nginx.conf 파일 확인
1. 운영 체제 별 설치 방법
1. Ubuntu
sudo apt install nginx
2. CentOS
sudo yum install nginx
3. MAC(homebrew 설치 필요)
brew install nginx
4. Docker
docker pull nginx
docker run -d -p 80:80 --name nginx-container nginx
2. Nginx.conf 파일 확인
default.conf 전문
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/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 /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;
#}
}
1. 기본 서버 설정
server {
listen 80; // Nginx가 HTTP 요청을 처리할 기본 포트(80번)
listen [::]:80; // IPv6 기반의 요청을 처리하도록 설정
server_name localhost; // 요청된 도메인 이름에 따라 이 서버 블럭을 사용할지 결정
// localhost는 본인 로컬(127.0.0.1)을 의미
}
2. 정적 파일 제공
location / { // 루트경로(/)로 들어오는 요청을 처리
root /usr/share/nginx/html; // 정적 파일의 루트 디렉토리
index index.html index.htm; // 기본으로 로드할 파일 목록
}
3. 에러 페이지 설정
error_page 500 502 503 504 /50x.html; // 5xx에러 발생 시 50x 에러 페이지 등장
location = /50x.html {
root /usr/share/nginx/html; // '/50x.html' 경로에 대한 요청을 처리
}
4. PHP 관련 설정
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
5. 보안 설정
#location ~ /\.ht { // .ht로 시작하는 파일 이름에 대해 동작
# deny all; // 모든 클라이언트의 요청을 차단 (allow all 은 모든 요청 허용)
#}
.ht 파일 차단하는 이유
.htaccess는 Apache 웹 서버에서 사용되는 설정 파일이며 디렉토리 수준에서 설정을 오버라이딩 가능
그래서 Nginx도 보안 위험등의 이유로 차단한다.
'Nginx' 카테고리의 다른 글
| 왜 Nginx인가? 웹 서버와 애플리케이션 서버의 차이 (1) | 2025.01.24 |
|---|